【问题标题】:pdf generation using html-pdf npm package , deploy on AWS as a GET api使用 html-pdf npm 包生成 pdf,在 AWS 上部署为 GET api
【发布时间】:2019-09-03 17:23:27
【问题描述】:

我可以在我的 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:加载共享库时出错:libfontconfig.so.1: cannot open shared object file: No such file or directory\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。请让我知道任何可以帮助我解决此问题的宝贵建议。提前致谢

【问题讨论】:

    标签: node.js amazon-web-services pdf serverless html-pdf


    【解决方案1】:

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

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

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

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

      【解决方案2】:

      在 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

      【讨论】:

      • 感谢发帖!你节省了我的时间。
      猜你喜欢
      • 2019-04-16
      • 1970-01-01
      • 1970-01-01
      • 2020-12-01
      • 2017-04-30
      • 2015-07-17
      • 1970-01-01
      • 1970-01-01
      • 2022-10-05
      相关资源
      最近更新 更多