alixaxel / chrome-aws-lambda

Chromium Binary for AWS Lambda and Google Cloud Functions
MIT License
3.17k stars 289 forks source link

[Question] What are the best options for pdf rendering only? #255

Open Hideman85 opened 2 years ago

Hideman85 commented 2 years ago

Hi, I'm having troubles sometimes with generating pdf...

Code ```js /** **************************************************************************************** * Use puppeteer for generating our pdf from the html document **************************************************************************************** */ console.info('Will start soon the browser') const browser = await chromium.puppeteer.launch({ args: chromium.args, defaultViewport: chromium.defaultViewport, executablePath: await chromium.executablePath, headless: true, ignoreHTTPSErrors: true, pipe: true // Use pipe instead of websocket to avoid load issue }) console.info('const browser = await chromium.puppeteer.launch(...)') const page = await browser.newPage() console.info('const page = await browser.newPage()') await page.setBypassCSP(true) console.info('await page.setBypassCSP(true)') await page.setContent(fileString, { waitUntil: ['load', 'domcontentloaded', 'networkidle0'] }) console.info('await page.setContent(...)') let pdfBuffer = await page.pdf({ printBackground: true, displayHeaderFooter: true, headerTemplate: header, footerTemplate: footer, margin: { top: `${styling.top + styling.headerSpacingOuter}mm`, bottom: `${styling.bottom + styling.footerSpacingOuter}mm`, left: `${styling.left}mm`, right: `${styling.right}mm` }, format: 'A4', preferCSSPageSize: true }) console.info('await page.pdf(...)') await browser.close() console.info('await browser.close()') ```
  errorType: 'Error',
  stack: 'Error: Protocol error (Page.printToPDF): Target closed.\n' +
    '    at /opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:208:63\n' +
    '    at new Promise (<anonymous>)\n' +
    '    at CDPSession.send (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:207:16)\n' +
    '    at Page.pdf (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Page.js:1220:43)\n' +
    '    at /var/task/index.js:649:30\n' +

I tried to add extra options from recommended ones and it behaves even crazier...

Options ```js const browser = await chromium.puppeteer.launch({ args: [ // From node_modules/chrome-aws-lambda/build/index.js '--allow-running-insecure-content', '--autoplay-policy=user-gesture-required', '--disable-component-update', '--disable-domain-reliability', '--disable-features=AudioServiceOutOfProcess,IsolateOrigins,site-per-process', '--disable-print-preview', '--disable-setuid-sandbox', '--disable-site-isolation-trials', '--disable-speech-api', '--disable-web-security', '--disk-cache-size=33554432', '--enable-features=SharedArrayBuffer', '--hide-scrollbars', '--ignore-gpu-blocklist', '--in-process-gpu', '--mute-audio', '--no-default-browser-check', '--no-pings', '--no-sandbox', '--no-zygote', '--use-gl=swiftshader', '--window-size=1920,1080', '--single-process', // Additional '--disable-gpu', '--disable-dev-shm-usage', '--disable-sync', '--disable-breakpad', '--disable-default-apps', '--disable-extensions', '--no-first-run', '--disable-component-extensions-with-background-pages', '--disable-client-side-phishing-detection', '--run-all-compositor-stages-before-draw' ], defaultViewport: chromium.defaultViewport, executablePath: await chromium.executablePath, headless: true, ignoreHTTPSErrors: true, pipe: true // Use pipe instead of websocket to avoid load issue }) ```
  errorType: 'TimeoutError',
  stack: 'TimeoutError: waiting for target failed: timeout 30000ms exceeded\n' +
    '    at Object.waitWithTimeout (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/common/helper.js:224:26)\n' +
    '    at Browser.waitForTarget (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Browser.js:296:45)\n' +
    '    at ChromeLauncher.launch (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/node/Launcher.js:105:31)\n' +
    '    at async /var/task/index.js:637:19\n' +
  message: 'waiting for target failed: timeout 30000ms exceeded',
  name: 'TimeoutError',

I'm using the latest version 10.1.0 with a lambda layer created out of this:

{
  "name": "puppeteer-layer",
  "version": "1.0.0",
  "dependencies": {
    "chrome-aws-lambda": "~10.1.0",
    "puppeteer-core": "~10.1.0"
  }
}

I would really like to know what am I missing and how I can make the rendering more stable :wink:

shelltr commented 2 years ago

Have you tried downgrading to 9.1.0? I also had the same issues with pdf generation until I tried downgrading. Haven't seen any errors since then.

Hideman85 commented 2 years ago

No I did the reverse way, I upgraded to beta 12x and for now it looks to work. I look over the web and it says most of the time this issue rise when chrome crash for rendering the pdf. The v12 of puppeteer has more meaningful error messages and some fixes in addition it come with a more recent chrome with lot of fixes too. Hopefully I will not have a crash anymore or at least a error that mean something that I can deal with :)