DEV Community

Chuongtran
Chuongtran

Posted on

Improve performance generate pdf using puppeteer

My first post about generating pdf using puppeteer at here.

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

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

launch: 147.349ms
newPage: 89.669ms
setContent: 15.247ms
pdf: 161.551ms

Gotcha, setContent faster than goto

Thanks for your reading my post.

Top comments (0)