Halloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan 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

How to Use Playwright to Capture Web Pages as PDFs With Background Colors

Set printBackground to true to include backgrounds in Playwright PDFs. Learn how print media, color adjustment, and page-size options affect the result.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Set printBackground: true in Playwright’s page.pdf() options to include page background graphics in a PDF. It defaults to false. Playwright uses print CSS by default, so switch to screen media before generating the PDF if you need the page’s screen styles instead. For color fidelity, add -webkit-print-color-adjust: exact to the page’s CSS. Playwright’s Page API documents these controls.

Capture a page as a PDF with background colors

This JavaScript example opens a page, waits for it to load, and saves a PDF with background graphics enabled:

As an Amazon Associate I earn from qualifying purchases.

const { chromium } = require('playwright');

(async () => {
  const browser = await chromium.launch();
  try {
    const page = await browser.newPage();
    await page.goto('https://example.com', { waitUntil: 'networkidle' });

    await page.pdf({
      path: 'page.pdf',
      printBackground: true,
    });
  } finally {
    await browser.close();
  }
})();

Install Playwright and its browser before running the script; see the official setup guide. Replace the example URL with the page you need. page.pdf() returns a PDF buffer; the path option saves it directly to a file. The capture options and defaults are documented in the Page API.

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

Choose print or screen styles

PDF generation uses print media by default. That means @media print rules may apply, while screen-only styles may not. If the PDF should resemble the page as it appears on screen, set screen media before calling page.pdf():

#1 Best Overall
Sale
Epson Workforce ES-50 Compact & Lightweight Mobile Document Scanner | For Computers including PC and Mac Receipt and Document Scanner USB Powered
  • PORTABLE SCANNER FOR USE ON-THE-GO — The fastest and lightest mobile single-sheet-fed compact document scanner in its class¹
  • QUICK DOCUMENT SCANNING ― This Epson ultra-fast scanner scans a single page as quickly as 5.5 seconds²; Windows and Mac compatible
  • VERSATILE PAPER HANDLING ― Portable scanner scans documents up to 8.5 x 72 in; Also easily digitizes receipts and ID cards to make accounting, bookkeeping, and organizing simpler
  • INTUITIVE, HIGH-SPEED SOFTWARE — Epson ScanSmart Software³ is a smart tool allowing you to easily scan, review, and save; Stay organized easily with the help of this Epson scanner
  • EASY SETUP — USB-powered connect to your computer for quick and simple scanning; No batteries or external power supply required to operate portable document scanner; Standard Connectivity: USB 2.0
await page.emulateMedia({ media: 'screen' });
await page.pdf({ path: 'page.pdf', printBackground: true });

Use print media when the website’s print layout is the intended result. Use screen media when you need its screen styles. Playwright’s emulateMedia() also accepts 'print' or null; the default PDF rendering is already print media.

Preserve the page’s colors

Enabling printBackground requests background graphics, but printing may still adjust colors. To request exact print colors, add this rule to the page stylesheet:

Rank #2
Sale
Brother DS-640 Compact Mobile Document Scanner, (Model: DS640)
  • FAST SPEEDS - Scans color and black and white documents a blazing speed up to 16ppm (1). Color scanning won’t slow you down as the color scan speed is the same as the black and white scan speed.
  • ULTRA COMPACT – At less than 1 foot in length and only about 1. 5lbs in weight you can fit this device virtually anywhere (a bag, a purse, even a pocket).
  • READY WHENEVER YOU ARE – The DS-640 mobile scanner is powered via an included micro USB 3. 0 cable allowing you to use it even where there is no outlet available. Plug it into you PC or laptop and you are ready to scan.
  • WORKS YOUR WAY – Use the Brother free iPrint&Scan desktop app for scanning to multiple “Scan-to” destinations like PC, Network, cloud services, Email and OCR. (2) Supports Windows, Mac and Linux and TWAIN/WIA for PC/ICA for Mac/SANE drivers. (3)
  • OPTIMIZE IMAGES AND TEXT – Automatic color detection/adjustment, image rotation (PC only), bleed through prevention/background removal, text enhancement, color drop to enhance scans. Software suite includes document management and OCR software. (4)
html {
  -webkit-print-color-adjust: exact;
}

The two settings address different things: printBackground: true enables background graphics in the PDF, while -webkit-print-color-adjust: exact addresses print color adjustment. If you cannot change the website’s CSS, you can inject a style before capture:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
await page.addStyleTag({
  content: 'html { -webkit-print-color-adjust: exact; }',
});
await page.pdf({ path: 'page.pdf', printBackground: true });

Color rendering can depend on the page and browser environment; the API documents the setting but does not guarantee identical output in every deployment.

Rank #3
Sale
Canon imageFORMULA R10 - Portable Document Scanner, USB Powered, Duplex Scanning, Document Feeder, Easy Setup, Convenient, Perfect for Mobile Users, White
  • STAY ORGANIZED – Easily convert your paper documents into digital formats like searchable PDF files, JPEGs, and more.Power Consumption : 2.5W or less (Energy Saving Mode: 0.7W). Suggested Daily Volume : 500 scans..Does it contain liquid: no
  • CONVENIENT AND PORTABLE –lightweight and small in size, you can take the scanner anywhere from home offices, classrooms, remote offices, and anywhere in between
  • HANDLES VARIOUS MEDIA TYPES – Digitize receipts, business cards, plastic or embossed cards, reports, legal documents, and more
  • FAST AND EFFICIENT – No technical hurdles or complicated setups here; easily scan both sides of a document at the same time, in color or black-and-white, at up to 12 pages-per-minute, and with a 20 sheet automatic feeder
  • BROAD COMPATIBILITY – Works with both Windows and Mac devices, be it laptop or computer

Control PDF size, layout, and page range

Playwright offers PDF options for paper format, dimensions, orientation, margins, page ranges, and scale. Choose how the PDF should be sized before changing scale to solve clipping or fit issues.

Option Behavior
format Paper format; defaults to Letter and takes priority over explicit width and height. Documented formats include Letter, Legal, Tabloid, Ledger, and ISO A-series sizes.
width, height Set paper dimensions. Unlabeled values are treated as pixels; supported units include px, in, cm, and mm.
landscape Set to true for landscape orientation.
margin Set margins; the documented default is zero.
pageRanges Choose which pages to include in the output.
scale Scale page rendering. The default is 1; the documented range is 0.1 to 2.
preferCSSPageSize Defaults to false. Set to true when CSS @page sizing should take precedence over PDF format, width, or height options.

For example, to render landscape A4 pages while preserving backgrounds:

Rank #4
IRIScan Express 4 Black Compact Portable USB Simplex Document Scanner, 8 PPM for Contracts, Invoices and Business Cards, Compatible with Windows, Readiris PDF Included
  • IRIScan Express, portable scanner : scans color and black and white documents a blazing speed up to 8ppm simplex. Color scanning won’t slow you down as the color scan speed is the same as the black and white scan speed.
  • IRIScan Express mobile scanner is powered via an included micro USB 2. 0 cable allowing you to use it even where there is no outlet available. Plug it into you PC or laptop and you are ready to scan. USB cable provided. AC Adapter not provided and not needed.
  • IRIScan flatbed scanner uses a simplex scanning mode allows for quick and straightforward scanning of single-sided documents. IRIScan with its full portable features is the ideal document scanners for computers.
  • IRIScan document scanner : Versatile scanning capabilities, including scanning to Word, PDF, and Excel formats with companion software provided Readiris OCR
  • Receipt scanner and card scanner with Additional features include scanning business cards directly to Outlook, photo scanning, and receipt scanning for efficient document management
await page.pdf({
  path: 'page.pdf',
  format: 'A4',
  landscape: true,
  printBackground: true,
});

When the page’s CSS @page size should control the paper size, use preferCSSPageSize: true. With the default setting, content is scaled to fit the paper size chosen by the PDF options. See the Page API for the full option definitions.

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

Use the Python or Java API

Playwright’s Python API uses snake_case option names. For example:

Best Value
Canon Canoscan Lide 300 Scanner (PDF, AUTOSCAN, Copy, Send)
  • Scanner type: Document
  • Connectivity technology: USB
  • With Auto Scan Mode, the scanner automatically detects what you're scanning
  • Digitize documents and images
await page.pdf(path="page.pdf", print_background=True)

Its page-size preference option is prefer_css_page_size. The Java API uses setters, including setPrintBackground(true) and setPreferCSSPageSize(true). Consult the official Python Page API and Java Page API for language-specific signatures. These documentation sections are marked “next” and may change.

Troubleshoot missing backgrounds or unexpected layout

  • Backgrounds are missing: Confirm that printBackground: true is present in the PDF options. It defaults to false.
  • The PDF layout differs from the browser view: PDF generation uses print media by default. Call await page.emulateMedia({ media: 'screen' }) before page.pdf() when screen styles are intended.
  • Colors look altered: Add -webkit-print-color-adjust: exact to the page stylesheet or inject it with page.addStyleTag(). This is separate from enabling background graphics.
  • The CSS page size is ignored: Set preferCSSPageSize: true if the stylesheet’s @page size should override the PDF paper options.
  • Content is clipped or scaled unexpectedly: Check whether format is overriding width and height; verify margins and orientation; then adjust scale within the documented 0.1–2 range.

Or skip the browser setup

ScreenshotNeo can return a web page as a PDF with one API request. Its clean-shot process accepts cookie and consent banners and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each step can be turned off. Bot checks, blank pages, and failed loads are never billed. It also provides an MCP server for AI agents to capture screenshots and PDFs.

Here is the cURL call, with the PDF output format specified as a request parameter:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.com -d format=pdf -o page.pdf

See the ScreenshotNeo API documentation for authentication and PDF options. ScreenshotNeo includes 1,000 screenshots a month free with no card; paid plans start at $5 for 3,000. Sign up for the free plan.

Frequently Asked Questions

Does printBackground preserve background colors exactly?

It enables background graphics, but print color adjustment is a separate behavior. Add -webkit-print-color-adjust: exact when exact print colors are required.

Can Playwright use CSS @page sizing for PDFs?

Yes. Set preferCSSPageSize: true so the CSS page size takes precedence over PDF paper-size options.

Quick Recap

SaleBestseller No. 3
Canon imageFORMULA R10 - Portable Document Scanner, USB Powered, Duplex Scanning, Document Feeder, Easy Setup, Convenient, Perfect for Mobile Users, White
Canon imageFORMULA R10 - Portable Document Scanner, USB Powered, Duplex Scanning, Document Feeder, Easy Setup, Convenient, Perfect for Mobile Users, White
BROAD COMPATIBILITY – Works with both Windows and Mac devices, be it laptop or computer; This product is not intended for scanning photographs on photo paper / photographic media
$153.00
Bestseller No. 4
IRIScan Express 4 Black Compact Portable USB Simplex Document Scanner, 8 PPM for Contracts, Invoices and Business Cards, Compatible with Windows, Readiris PDF Included
IRIScan Express 4 Black Compact Portable USB Simplex Document Scanner, 8 PPM for Contracts, Invoices and Business Cards, Compatible with Windows, Readiris PDF Included
Find our Software here : irislink.com/start; IRIScan Express is only compatible Windows platform and not macintosh
$129.00
Bestseller No. 5
Canon Canoscan Lide 300 Scanner (PDF, AUTOSCAN, Copy, Send)
Canon Canoscan Lide 300 Scanner (PDF, AUTOSCAN, Copy, Send)
Scanner type: Document; Connectivity technology: USB; With Auto Scan Mode, the scanner automatically detects what you're scanning
$75.00

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.

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

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
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

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.