We have written a Print PDF lambda function that generates a PDF file using puppeteer from HTML content. The lambda function uses the packages chrome-aws-lambda and puppeteer-core to generate PDF content from HTML. The puppeteer error is occurring in our QA when images are included in any HTML content, either as a background-image or an tag. When no image is included, the PDF gets generated successfully.
We are using the following: npm install [email protected] [email protected] --save-prod
Below is the exception "errorType": "Error", "errorMessage": "Navigation failed because browser has disconnected!", "stack": [ "Error: Navigation failed because browser has disconnected!", " at /var/task/node_modules/puppeteer-core/lib/cjs/puppeteer/common/LifecycleWatcher.js:51:147", " at /var/task/node_modules/puppeteer-core/lib/cjs/vendor/mitt/src/index.js:51:62", " at Array.map ()", " at Object.emit (/var/task/node_modules/puppeteer-core/lib/cjs/vendor/mitt/src/index.js:51:43)", " at CDPSession.emit (/var/task/node_modules/puppeteer-core/lib/cjs/puppeteer/common/EventEmitter.js:72:22)", " at CDPSession._onClosed (/var/task/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:247:14)", " at Connection._onClose (/var/task/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:128:21)", " at WebSocket. (/var/task/node_modules/puppeteer-core/lib/cjs/puppeteer/node/NodeWebSocketTransport.js:17:30)", " at WebSocket.onClose (/var/task/node_modules/ws/lib/event-target.js:124:16)", " at WebSocket.emit (events.js:400:28)" ] }
below is the code snippet:
async function getBrowser() {
if (puppeteer) {
return await puppeteer.launch({ headless: true });
}
// if running in lambda return chromium lambda
if (chromium) {
return await chromium.puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless,
ignoreHTTPSErrors: true
});
}
}
export const html2pdf = async (html = '', url = '', format = 'Legal') => {
let myBrowser = await getBrowser();
const page = await myBrowser.newPage();
const loaded = page.waitForNavigation({
waitUntil: 'load'
});
if (html) {
await page.setContent(html, { waitUntil: 'networkidle0' });
} else if (url) {
await page.goto(url, { waitUntil: 'networkidle0' });
}
// await page.setViewport({ isMobile: true, width: 375, height: 667 });
await loaded;
const pdf = await page.pdf({ format: format });
await page.close();
await myBrowser.close();
return pdf;
};
according to some stackoverflow posts we have increased the ephemeral storage and memory to their perspective max: 10240 MB . Any suggestions as to a solution or how to debug? Thanks
It looks like there's never a navigation because setContent doesn't navigate.
Which means you're closing the browser before page.waitNavigation finishes.
I would just remove the waitForNavigation, page.goto should have that covered anyway.