How to generate a pdf using puppeteer? Check out at there.
At this post, I want to share about tip how to improve performance generate pdf.
We can use page.setContent to improve performance.
Using goto
console.time('launch')
const browser = await puppeteer.launch({args: ['--no-sandbox'],headless: true })
console.timeEnd('launch')
console.time('newPage')
const page = await browser.newPage();
console.timeEnd('newPage')
console.time('goto')
await page.goto(`data: text/html ,${finalHtml}`, {waitUntil: 'networkidle0'})
console.timeEnd('goto')
console.time('pdf')
await page.pdf(options)
console.timeEnd('pdf')
Log time
launch: 168.766ms
newPage: 87.764ms
goto: 1018.925ms
pdf: 109.687ms
Using setContent
console.time('launch')
const browser = await puppeteer.launch({args: ['--no-sandbox'],headless: true})
console.timeEnd('launch')
console.time('newPage')
const page = await browser.newPage()
console.timeEnd('newPage')
console.time('setContent')
await page.setContent(finalHtml);
console.timeEnd('setContent')
console.time('pdf')
await page.pdf(options);
console.timeEnd('pdf')
Log time
launch: 147.349ms
newPage: 89.669ms
setContent: 15.247ms
pdf: 161.551ms
Gotcha, setContent faster than goto 15ms vs 1018ms