DEV Community

Chuongtran
Chuongtran

Posted on

8 3

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.

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs