繁体   English   中英

使用 html-pdf npm 包生成 pdf,作为 GET api 部署在 AWS 上

[英]pdf generation using html-pdf npm package , deploy on AWS as a GET api

我能够在我的 mac 上本地使用 nodejs 中的 html-pdf 生成 pdf。 我在 AWS 上使用无服务器部署我的 GET api 的那一刻,一切都失败了。 pdf 没有生成,我收到 400 个错误的请求和消息作为html-pdf: Received the exit code '127'\\n/var/task/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs: error while loading共享库:libfontconfig.so.1:无法打开共享对象文件:没有这样的文件或目录\\n

我更改了 html-pdf 包的版本,尝试安装 fontmanager 但没有任何帮助。

//pdf generation class
{
.....
const pdfSettings = {
                "border": '1cm',
                "header": {
                    "height": "15mm"
                }
            };

            const PDFBuffer = new Promise((resolve, reject) => {
                pdf.create(pdfContent, pdfSettings).toBuffer(
                    (err, buffer) => {
                        err ? reject(err) : resolve(buffer);
                    });
            });

            const buffer = await PDFBuffer;
            return actions.downloadPDF(contactId, buffer);
}

//function which returns pdf in binary to api gateway
downloadPDF(pdfName, pdfBuffer) {

    let responseObj = {
      statusCode: 200,
      isBase64Encoded: true,
      headers: {
        'Content-type': 'application/pdf',
        'accept-ranges': 'bytes',
        'Content-Disposition': 'attachment; filename=' + pdfName + '.pdf'
      },
      body: pdfBuffer && JSON.stringify(pdfBuffer.toString('base64'))
    }
    return responseObj;
  }

我应该能够使用 AWS 中的 GET api 生成 pdf。 请让我知道任何可以帮助我解决此问题的宝贵建议。 提前致谢

在 aws ubuntu 上使用 html-pdf 遇到了同样的问题,这就是我所做的。

使用示例测试 html-pdf 包:

root@APP-AP-1A-01:/home/user01/server-a7# node node_modules/html-pdf/bin/index.js node_modules/html-pdf/examples/businesscard/businesscard.html /tmp/test-prod.pdf
/home/user01/server-a7/node_modules/html-pdf/bin/index.js:30
    if (err) throw err
             ^

Error: html-pdf: Received the exit code '127'
/home/user01/server-a7/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs: error while loading shared libraries: libfontconfig.so.1: cannot open shared object file: No such file or directory

    at ChildProcess.respond (/home/user01/server-a7/node_modules/html-pdf/lib/pdf.js:121:31)
    at ChildProcess.emit (events.js:210:5)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:272:12)

^ 在运行 Ubuntu (4.4.0-1072-aws) 的 Aws 上看到此错误,这是由于缺少依赖的库 phantomjs; npm -i没有在服务器上很好地安装/更新它。

以下是步骤:

  1. ldd - 打印共享对象依赖项(注意它已标记,libfontconfig.so.1 => 未找到)
  2. 安装缺少的库(apt-get install fontconfig)
  3. 使用 ldd 再次检查依赖项
  4. 运行示例,这次它从 html 创建了 pdf

以 root 用户身份运行或使用 sudo :-

root@APP-AP-1A-01:/home/user01/server-a7# ldd /home/user01/server-a7/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs
        linux-vdso.so.1 =>  (0x00007ffea155d000)
        libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007fd639645000)
        libfontconfig.so.1 => not found
        libfreetype.so.6 => /usr/lib/x86_64-linux-gnu/libfreetype.so.6 (0x00007fd63939b000)
        libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fd639197000)
        librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fd638f8f000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fd638d72000)
        libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fd6389f0000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fd6386e7000)
        libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fd6384d1000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fd638107000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fd63985f000)
        libpng12.so.0 => /lib/x86_64-linux-gnu/libpng12.so.0 (0x00007fd637ee2000)

root@APP-AP-1A-01:/home/user01/server-a7# ls -l /usr/lib/x86_64-linux-gnu/libfontconfig.so.1
ls: cannot access '/usr/lib/x86_64-linux-gnu/libfontconfig.so.1': No such file or directory

root@APP-AP-1A-01:/home/user01/server-a7# apt-get install fontconfig
Reading package lists... Done
:
The following additional packages will be installed:
  fontconfig-config fonts-dejavu-core libfontconfig1
The following NEW packages will be installed:
  fontconfig fontconfig-config fonts-dejavu-core libfontconfig1
:
Do you want to continue? [Y/n] y
Get:1 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu xenial/main amd64 fonts-dejavu-core all 2.35-1 [1,039 kB]
Get:2 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu xenial-updates/main amd64 fontconfig-config all 2.11.94-0ubuntu1.1 [49.9 kB]
Get:3 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu xenial-updates/main amd64 libfontconfig1 amd64 2.11.94-0ubuntu1.1 [131 kB]
Get:4 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu xenial-updates/main amd64 fontconfig amd64 2.11.94-0ubuntu1.1 [178 kB]
Fetched 1,398 kB in 1s (928 kB/s)
:
:
Regenerating fonts cache... done.
Processing triggers for libc-bin (2.23-0ubuntu11.2) ...

root@APP-AP-1A-01:/home/user01/server-a7# ldd /home/user01/server-a7/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs
        linux-vdso.so.1 =>  (0x00007ffcdf49c000)
        libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f65a4723000)
        libfontconfig.so.1 => /usr/lib/x86_64-linux-gnu/libfontconfig.so.1 (0x00007f65a44e0000)
        libfreetype.so.6 => /usr/lib/x86_64-linux-gnu/libfreetype.so.6 (0x00007f65a4236000)
        libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f65a4032000)
        librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f65a3e2a000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f65a3c0d000)
        libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f65a388b000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f65a3582000)
        libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f65a336c000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f65a2fa2000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f65a493d000)
        libexpat.so.1 => /lib/x86_64-linux-gnu/libexpat.so.1 (0x00007f65a2d79000)
        libpng12.so.0 => /lib/x86_64-linux-gnu/libpng12.so.0 (0x00007f65a2b54000)

root@APP-AP-1A-01:/home/user01/server-a7# node node_modules/html-pdf/bin/index.js node_modules/html-pdf/examples/businesscard/businesscard.html /tmp/test-prod.pdf

root@APP-AP-1A-01:/home/user01/server-a7# ls -lrt /tmp/test-prod.pdf
-rw-r--r-- 1 root root 11250 Jul 10 10:05 /tmp/test-prod.pdf

在 /tmp/test-prod.pdf 中创建了 pdf

通过使用 puppeteer 库,我能够找到更好的解决方案。 选择不同图书馆的原因

  • 不推荐使用 html-pdf
  • puppeteer 有更好的选择
  • puppeteer 具有异步/等待功能
  • 尽管要使用 serverless 和 serverless-plugin-optimize 在 AWS 中进行这项工作,我确实面临许多挑战。 在实现这种类似的场景时注意以下几点

    API网关发送任何二进制文件(pdf / jpeg / jpg)作为响应

  • 二进制媒体类型应在 API 网关资源设置选项中设置为*/* ,如果在serverless.yaml 中的provider 下通过serverless添加apiGateway: binaryMediaTypes: - */*
  • 如果您有机会使用 serverless-plugin-optimize 来减小 lambda 大小,请为此包 chrome-aws-lambda 使用“外部”选项,参考链接https://www.npmjs.com/package/serverless-plugin-optimize
  • 暂无
    暂无

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM