AWS Lambda 中的 HTML 到 PDF

问题描述 投票:0回答:1

HTML 转 PDF

我正在 AWS Lamda 上使用 Nodejs16 环境并将 HTML 转换为 PDF。

我使用了很多库,例如 puppeteer 和 chrome-aws-lambda。

puppeteer 正在本地或服务器上工作。但它不适用于 aws-lambda 或无服务器。


const chromium = require('chrome-aws-lambda');
const puppeteer = require('puppeteer-core');

exports.handler = async (event) => {

  try {
    const browser = await puppeteer.launch({
      executablePath: await chromium.executablePath,
      args: chromium.args,
      headless: true,
    });

    const page = await browser.newPage();

    await page.setContent("<html><body><h5>Hello World!</h5></body></html>")

    const pdf = await page.pdf({
        format: "A4",
        path: "output.pdf"
    });

    return {statusCode: 200};
  } catch (error) {
    console.error(error);
    return {statusCode: 500};
  }
};

出现此错误

Failed to launch the browser process!\n/tmp/chromium: error while loading shared libraries: libnss3.so: cannot open shared object file: No such file or directory\n\n\nTROUBLESHOOTING:
node.js amazon-web-services aws-lambda puppeteer html-to-pdf
1个回答
0
投票

您遇到的错误

(libnss3.so: cannot open shared object file: No such file or directory)
通常表示您的AWS Lambda函数运行的环境中缺少依赖项。 Lambda 环境更加简约,有时缺少 Puppeteer 等无头浏览器所需的某些共享库。

要解决此问题,您可以尝试将必要的依赖项与 Lambda 函数捆绑在一起。这是代码的修改版本,其中包含所需的文件:

const chromium = require('chrome-aws-lambda');
const puppeteer = require('puppeteer-core');
const fs = require('fs');

exports.handler = async (event) => {
  try {
    // Ensure the required directories exist
    fs.mkdirSync('/tmp/fonts', { recursive: true });
    fs.mkdirSync('/tmp/chromium', { recursive: true });

    // Copy fonts and chromium to /tmp
    fs.copyFileSync('/opt/fonts/NotoSansCJKjp-Regular.otf', '/tmp/fonts/NotoSansCJKjp-Regular.otf');
    fs.copyFileSync('/opt/chromium/headless_shell', '/tmp/chromium/headless_shell');

    const browser = await puppeteer.launch({
      executablePath: '/tmp/chromium/headless_shell',
      args: [
        '--disable-gpu',
        '--disable-dev-shm-usage',
        '--disable-software-rasterizer',
        '--disable-features=site-per-process',
        '--no-zygote',
        '--single-process',
        '--no-sandbox',
      ],
      headless: true,
    });

    const page = await browser.newPage();

    await page.setContent("<html><body><h5>Hello World!</h5></body></html>");

    const pdf = await page.pdf({
      format: "A4",
      path: "/tmp/output.pdf", // Save the file in /tmp directory
    });

    // Perform any additional operations with the PDF if needed

    await browser.close();

    return {
      statusCode: 200,
      body: JSON.stringify('PDF generated successfully'),
    };
  } catch (error) {
    console.error(error);
    return {
      statusCode: 500,
      body: JSON.stringify('Internal Server Error'),
    };
  }
};

确保在 Lambda 部署包的

(NotoSansCJKjp-Regular.otf)
目录中包含所需的字体文件
/opt/fonts/
。另外,请确保
headless_shell
二进制文件位于
/opt/chromium/
目录中。您可能需要根据您的具体设置调整这些路径。

请务必小心处理依赖项,因为 Lambda 环境对部署包的大小有一些限制。目标是仅包含应用程序正常运行所需的必要文件和库。

© www.soinside.com 2019 - 2024. All rights reserved.