In the previous example we created a png screenshot. But we can also generate PDFs in various formats!
// instead of calling await page.screenshot we now call
await page.pdf({
path: 'codesnacks.pdf',
format: 'A4'
})
For completeness, here's the full code to generate a PDF of a webpage in the A4 format:
// npm i puppeteer
const puppeteer = require('puppeteer');
// we're using async/await - so we need an async function, that we can run
const run = async () => {
// open the browser and prepare a page
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://codesnacks.net/');
await page.pdf({
path: 'codesnacks.pdf',
format: 'A4',
});
// close the browser
await browser.close();
};
// run the async function
run();
Top comments (0)