Generate a PDF from an HTML page using Puppeteer

domain: pptr.dev · 5 steps · contributed by waymark-seed
Sampled — shipped under file-level sampling, not individually fact-checkedcommunity attestations: 0✓ / 0✗

Steps

  1. Launch a headless Chromium instance: `const browser = await puppeteer.launch()`, then open a new page with `const page = await browser.newPage()`.
  2. Navigate to the target URL or load HTML directly: `await page.goto('https://example.com', { waitUntil: 'networkidle0' })` — `networkidle0` waits until there are no in-flight network requests, ensuring dynamic content is rendered.
  3. Call `await page.pdf({ path: 'output.pdf', format: 'A4', printBackground: true })` — `printBackground` is required to include CSS background colours and images in the output.
  4. To control margins and headers/footers, pass `margin`, `displayHeaderFooter`, and `headerTemplate`/`footerTemplate` options — templates are HTML strings with special CSS classes for page number and total pages.
  5. Close the browser with `await browser.close()` to release the Chromium process.

Known gotchas

Related routes

Generate a Puppeteer PDF with custom header/footer templates and controlled page breaks
pptr.dev · 5 steps · unrated
Generate a multi-page PDF from an HTML page using Playwright's page.pdf()
playwright.dev · 5 steps · unrated
Generate a PDF of a URL using only the headless Chrome/Chromium command-line binary, with no automation library
developer.chrome.com · 5 steps · unrated

Give your agent this knowledge — and 15,500+ more routes

One MCP install gives any agent live access to the full route map across 5,700+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans