Halloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowHalloween DealsAmazon USOctober picks are moving - compare before checkoutAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

HTML to PDF with Puppeteer: Page Size, Margins, and Background Graphics

Configure Puppeteer’s page.pdf() for standard or custom paper sizes, four-sided margins, landscape output, print colors, and CSS backgrounds.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Puppeteer’s page.pdf() options to set paper size, margins, orientation, and background graphics. The key switches are format (or custom width and height), margin, and printBackground: true. Puppeteer generates PDFs using print CSS by default.

Set paper size, margins, and background graphics

Here is a complete example for an A4 PDF with explicit margins and CSS background graphics included:

As an Amazon Associate I earn from qualifying purchases.

const pdf = await page.pdf({
  format: 'A4',
  margin: {
    top: '20mm',
    right: '15mm',
    bottom: '20mm',
    left: '15mm',
  },
  printBackground: true,
});

page.pdf() returns the generated PDF as a buffer. Save it to a file with Node’s filesystem API if needed:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const fs = require('node:fs/promises');
await fs.writeFile('page.pdf', pdf);

The Puppeteer PDFOptions reference documents these settings and their defaults. Its version 25.12.0 reference lists Letter as the default format; defaults and option behavior can vary by Puppeteer version.

#1 Best Overall
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option

Choose a page size and orientation

Standard formats

Set format to a supported paper name such as 'A4' or 'Letter'. Letter measures 8.5 × 11 inches (21.59 × 27.94 cm); A4 measures 210 × 297 mm (about 8.2677 × 11.6929 inches). Pick the size required by the document’s destination rather than assuming one format fits all.

When format is supplied, it takes priority over width and height. Avoid specifying conflicting values: choose either a standard format or custom dimensions.

Custom dimensions

For a nonstandard sheet, set width and height using explicit CSS units, for example '100mm' and '150mm'. Puppeteer’s documentation allows dimensions as strings or numbers; unit-bearing strings make the intended measurement unambiguous.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Landscape

Use landscape: true to switch the page orientation. The documented default is false. Example: await page.pdf({ format: 'A4', landscape: true }).

Control the four margins

Pass a margin object with any or all of top, right, bottom, and left. The values may be strings or numbers. If the option is omitted, Puppeteer sets no margins. For print layouts, explicit units such as '12mm' or '0.5in' are easier to review and maintain than unitless values.

await page.pdf({
  format: 'Letter',
  margin: {
    top: '0.75in',
    right: '0.65in',
    bottom: '0.75in',
    left: '0.65in',
  },
});

Include backgrounds and preserve print colors

Background graphics are not printed by default. Set printBackground: true to include CSS background colors and images. This is separate from omitBackground: that option removes the default white page background to allow transparency; it does not enable CSS background printing.

PDF printing may adjust colors for print output. If exact CSS colors matter, Puppeteer’s PDF guidance points to the CSS property -webkit-print-color-adjust. Apply it intentionally in your print stylesheet, for example:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
@media print {
  html {
    -webkit-print-color-adjust: exact;
  }
}

Color adjustment and printBackground solve different problems: the first concerns print color treatment, while the second determines whether CSS backgrounds are included at all.

Decide whether Puppeteer options or CSS controls page size

Puppeteer uses the document’s print media styles when creating a PDF. If your stylesheet defines an @page size and you want that size to take precedence over the API’s format or dimensions, set preferCSSPageSize: true:

Rank #4
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers
@page {
  size: A4 landscape;
  margin: 20mm 15mm;
}
await page.pdf({
  format: 'Letter',
  preferCSSPageSize: true,
  printBackground: true,
});

With preferCSSPageSize: true, CSS page dimensions win over format, width, and height. In this example, the CSS-defined A4 landscape size wins despite format: 'Letter'. Without the preference, Puppeteer scales content to fit the paper size specified in the PDF options.

If you want screen styles rather than print styles, call await page.emulateMediaType('screen') before page.pdf(). Otherwise, print media is used.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Check the result when layout differs from expectations

  • Wrong paper size: Check whether format overrides your width and height, or whether preferCSSPageSize gives an @page rule priority.
  • Unexpected whitespace: Set all four margin values explicitly; an omitted margin option means no margins are set by Puppeteer, though print CSS may define page margins.
  • Missing color blocks or background images: Enable printBackground: true. Check print styles as well, since PDF generation uses print media by default.
  • Transparent page needed: Use omitBackground for the default white page background; do not use it as a substitute for printBackground.
  • Colors look different from the browser: Review print CSS and consider -webkit-print-color-adjust where faithful color rendering is required.
  • Screen-only styling disappeared: Either create appropriate print rules or call page.emulateMediaType('screen') before generating the PDF.

The option defaults and PDF API are version-sensitive. If your project pins a different Puppeteer release, check its matching API reference rather than assuming the 25.12.0 defaults apply.

Or skip the browser setup

If you need a PDF from a URL without managing Puppeteer and a browser instance, ScreenshotNeo provides a screenshot API and PDF capture. Its MCP server also gives AI agents tools for taking screenshots and capturing PDFs. Cookie banners, newsletter popups, and chat widgets are removed before capture; bot checks, blank pages, and failed loads are never billed.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.pdf

See the ScreenshotNeo documentation for API parameters. The service offers 1,000 screenshots a month free with no card; paid plans start at $5 for 3,000. Learn about ScreenshotNeo or sign up for the free plan.

Frequently Asked Questions

Does Puppeteer print CSS backgrounds by default?

No. Set printBackground: true in page.pdf() to include them.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What is the default PDF paper size in Puppeteer?

The Puppeteer 25.12.0 PDFOptions reference lists Letter as the default. Confirm the documentation for the version your project uses.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.