Julien Lamarche
Julien Lamarche

Reputation: 1050

How to increase timeout for pupetteer's Page.pdf when getting "TimeoutError: waiting for Page.printToPDF failed"?

I'm getting "TimeoutError: waiting for Page.printToPDF failed" when trying to convert to PDF a large invoice:

Unhandled Rejection at: Promise Promise { <rejected>
 TimeoutError: waiting for Page.printToPDF failed: timeout 30000ms exceeded                                                      

  at Object.waitWithTimeout (/var/www/montbeau/releases/20210929161149/node_modules/puppeteer/lib/cjs/puppeteer/common/helper.js:224:26)

  at Page.createPDFStream (/var/www/montbeau/releases/20210929161149/node_modules/puppeteer/lib/cjs/puppeteer/common/Page.js:2045:49)

  at Page.pdf (/var/www/montbeau/releases/20210929161149/node_modules/puppeteer/lib/cjs/puppeteer/common/Page.js:2057:37)

  at /var/www/montbeau/releases/20210929161149/scripts/invoice_to_pdf.js:175:20

  at runMicrotasks (<anonymous>)                                                                                                                                                                                          
  at processTicksAndRejections (internal/process/task_queues.js:97:5)                                
  } reason: TimeoutError: waiting for Page.printToPDF failed: timeout
  30000ms exceeded                                                      

at Object.waitWithTimeout (/var/www/montbeau/releases/20210929161149/node_modules/puppeteer/lib/cjs/puppeteer/common/helper.js:224:26)

at Page.createPDFStream (/var/www/montbeau/releases/20210929161149/node_modules/puppeteer/lib/cjs/puppeteer/common/Page.js:2045:49)

at Page.pdf (/var/www/montbeau/releases/20210929161149/node_modules/puppeteer/lib/cjs/puppeteer/common/Page.js:2057:37)

at /var/www/montbeau/releases/20210929161149/scripts/invoice_to_pdf.js:175:20...

How do I increase the timeout?

Upvotes: 7

Views: 9448

Answers (2)

Harsh
Harsh

Reputation: 76

As Julien mentioned, Set timeout to 0.

const page = await browser.newPage();
await page.pdf({
    printBackground: true,
    displayHeaderFooter: false,
    timeout: 0,
});

Upvotes: 6

Julien Lamarche
Julien Lamarche

Reputation: 1050

In createPDFStream method of puppeteer/lib/cjs/puppeteer/common/Page.js, timeout appears as an option:

2007         const { scale = 1, displayHeaderFooter = false, headerTemplate = '', footerTemplate = '', printBackground = false, landscapdscape = false, pageRanges = '', preferCSSPageSize = false, margin = {}, omitBackground = false, timeout = 30000, } = options;
[...]
2045         const result = await helper_js_1.helper.waitWithTimeout(printCommandPromise, 'Page.printToPDF', timeout);

So despite timeout not being a documented option at https://github.com/puppeteer/puppeteer/blob/main/docs/api.md#pagepdfoptions, it does seem as though it is an option for the .pdf method.

Upvotes: 3

Related Questions