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)