Skip to content

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

Open

Description

@Hideman85

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

Code
  /** ****************************************************************************************
   *           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
  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 😉

Activity

  1. shelltr commented on Feb 5, 2022

    @shelltr

    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.

  2. Hideman85 commented on Feb 5, 2022

    @Hideman85
    Author

    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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions