4

I have a microservice in which one of the API is generating PDFs (3 pdfs based on type passed as a parameter). I have used puppeteer package to generate the PDF. Works as charm on my local system.

When I try to run the same microservice on EC2 ubuntu 16.04 server, it is unable to launch chromium headless. I have used "npm i puppeteer" and as per my understanding, it should bring chromium headless on its own as dependency.

The microservice is running fine, but the issue is in fetching chromium by puppeteer.

Error

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.

Also, I am unable to find ".local-chromium/linux-{version}/linux-chrom" inside "/node_modules/puppeteer/"

So, my understanding is that chrome never got installed

I am comfortable with docker as well. I am running other microservices in docker only. So, if anyone can suggest some workaround using docker, that can also work.

Concerned codepart from the microservice

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;
}

Output of 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)
Plasmatiger
  • 197
  • 2
  • 12

1 Answers1

4

Puppeteer Error: Chromium revision is not downloaded - This link helped me to debug that I need to run install.js in puppeteer manually.

https://github.com/puppeteer/puppeteer/issues/3443 - This link for everything else.

Also, apt-get install libgbm-dev

Plasmatiger
  • 197
  • 2
  • 12
  • 1
    For people looking into docker - https://medium.com/@christopher.talke/using-node-puppeteer-with-docker-without-wanting-to-smash-your-keyboard-ed78e9529a8b Also, the article has a reference at the beginning of a simple deployment. So, that will be also helpful – Plasmatiger Sep 24 '20 at 11:15