DEV Community


πŸ“· More on Screenshots with JavaScript in Node.js (creating PDFs)

benjaminmock profile image Benjamin Mock ・1 min read

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'
Enter fullscreen mode Exit fullscreen mode

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('');

  await page.pdf({
    path: 'codesnacks.pdf',
    format: 'A4',

  // close the browser
  await browser.close();

// run the async function
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

Editor guide