无法在Ubuntu 16.04 AWS EC2实例上使用puppeteer启动无头铬铬

问题描述

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

当我尝试在EC2 ubuntu 16.04服务器上运行相同的微服务时,无法启动无头铬。我使用过“ npm i puppeteer”,据我了解,它应该使铬无头依赖。

微服务运行正常,但问题出在操纵p的铬中。

错误

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.

此外,我在“ / 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;
}

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)

解决方法

Puppeteer Error: Chromium revision is not downloaded-此链接帮助我调试了需要在puppeteer中手动运行install.js的情况。

https://github.com/puppeteer/puppeteer/issues/3443-其他链接。

此外,apt-get install libgbm-dev