无法在 Ubuntu 16.04 AWS EC2 实例上使用 puppeteer 启动 chromium headless

Pla*_*ger 5 amazon-web-services node.js headless-browser docker puppeteer

我有一个微服务,其中一个 API 正在生成 PDF(基于作为参数传递的类型的 3 个 pdf)。我使用 puppeteer 包来生成 PDF。在我的本地系统上发挥作用。

当我尝试在 EC2 ubuntu 16.04 服务器上运行相同的微服务时,它无法启动 chromium headless。我使用过“npm i puppeteer”,根据我的理解,它应该将 chromium headless 作为依赖项。

微服务运行良好,但问题在于 puppeteer 获取 chromium。

错误

Error: Could not find browser revision 800071. Run "PUPPETEER_PRODUCT=firefox npm install" or "PUPPETEER_PRODUCT=firefox yarn install" to download a supported Firefox browser binary.
Run Code Online (Sandbox Code Playgroud)

另外,我无法在“/node_modules/puppeteer/”中找到“.local-chromium/linux-{version}/linux-chrom”

所以,我的理解是 chrome 从未安装过

我对 docker 也很满意。我只在 docker 中运行其他微服务。因此,如果有人可以建议一些使用 docker 的解决方法,那也是可行的。

来自微服务的相关代码部分

async function createPDF(baseFile, inp) {
var templateHtml = fs.readFileSync(
    path.join(process.cwd(), `utilities/${baseFile}.html`),
    "utf8"
  );

  var template = handlebars.compile(templateHtml);

  var html = template(inp);

  var milis = new Date();
  milis = milis.getTime();

  var pdfPath = path.join(process.cwd(), `${baseFile}.pdf`);

  var options = {
    width: "1100px",
    height: "1380px",
    // format: "A3",
    headerTemplate: "<p></p>",
    footerTemplate: "<p></p>",
    displayHeaderFooter: false,
    margin: {
      top: "10px",
      bottom: "10px",
    },
    // printBackground: true,
  };

  const browser = await puppeteer.launch({
    args: ["--no-sandbox", "--disable-setuid-sandbox"],
    headless: true,
  });

  var page = await browser.newPage();

  await page.goto(`data:text/html;charset=UTF-8,${html}`, {
    waitUntil: "networkidle0",
  });

  //   await page.addStyleTag({
  //     content:
  //       "@page:first {margin-top:10px; margin-right:10px; margin-bottom:30px; margin-left:10px;}",
  //   });

  const pdf = await page.pdf(options);
  await browser.close();
  return pdf;
}
Run Code Online (Sandbox Code Playgroud)

npm i puppeteer的输出

 npm i puppeteer

> puppeteer@5.3.1 install /home/ubuntu/vendor-module/node_modules/puppeteer
> node install.js

(node:18339) UnhandledPromiseRejectionWarning: /home/ubuntu/vendor-module/node_modules/puppeteer/lib/cjs/puppeteer/install.js:138
                    catch {
                          ^

SyntaxError: Unexpected token {
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:616:28)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at download (/home/ubuntu/vendor-module/node_modules/puppeteer/install.js:35:7)
(node:18339) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
Run Code Online (Sandbox Code Playgroud)

Pla*_*ger 5

Puppeteer 错误:未下载 Chromium 修订版- 此链接帮助我调试我需要在 puppeteer 中手动运行 install.js。

https://github.com/puppeteer/puppeteer/issues/3443 - 此链接用于其他所有内容。

另外,apt-get install libgbm-dev

  • 对于研究 docker 的人 - https://medium.com/@christopher.talke/using-node-puppeteer-with-docker-without-wanting-to-smash-your-keyboard-ed78e9529a8b 另外,这篇文章有一个参考简单部署的开始。所以,这也会有帮助 (2认同)