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

Use Playwright’s Chromium browser and its page.pdf() method to print a webpage to a PDF. Install Playwright and its browser binaries, navigate to the page, then save the output with path="page.pdf". By default, the PDF uses print CSS and does not include background graphics.

Install Playwright and Chromium

  1. Install the Python package: python -m pip install playwright.
  2. Install Playwright browsers: python -m playwright install.
  3. Save the script below as save_page.py, replacing the example URL with the fully qualified address of the page you want to capture.

The official Playwright Python guide documents package and browser installation. PDF generation through page.pdf() is supported by Chromium; launch Chromium explicitly for this workflow. The Page API link used here points to the Next documentation, marked “Version: Next.” Check the API for your installed Playwright release before relying on options that may not exist in older stable versions.

Save a webpage as a PDF

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch()
    page = browser.new_page()
    page.goto("https://example.com")
    page.pdf(path="page.pdf", format="A4", print_background=True)
    browser.close()

Run it with python save_page.py. The PDF is written to page.pdf in the current working directory. This example uses A4 paper and includes background graphics; remove format or print_background if you want the API defaults instead. The documentation describes the API behavior, but output depends on the page; inspect the resulting file for layout, fonts, and dynamic content.

Choose the PDF rendering and page layout

Print CSS or screen CSS

page.pdf() renders with print CSS media by default. This is usually appropriate for a printable document. If the page’s screen styling is what you need, switch media before creating the PDF:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.
page.emulate_media(media="screen")
page.pdf(path="page.pdf", format="A4", print_background=True)

Some sites define different layouts for printing, so changing media can substantially alter the result.

Paper size, dimensions, and CSS page rules

  • format accepts named sizes such as "A4" and "Letter". The API documents A4 as 8.27 by 11.7 inches and Letter as 8.5 by 11 inches.
  • You can set width and height instead. Unlabeled values are interpreted as pixels; supported units include px, in, cm, and mm, for example width="210mm".
  • If both format and width or height are supplied, format takes priority.
  • By default, prefer_css_page_size is false, so content is scaled to fit the selected paper size. Set it to true when the page’s CSS @page size should take priority over API size options.

Margins, scale, and page ranges

Margins default to none. Set a margin such as margin={"top": "15mm", "bottom": "15mm", "left": "12mm", "right": "12mm"} to add page framing. The scale option defaults to 1 and accepts values from 0.1 to 2. Use page_ranges to restrict output to chosen pages when the document is longer than needed; consult the installed release’s API documentation for its accepted range syntax.

Rank #2
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.

Backgrounds, colors, headers, and footers

print_background defaults to false. Set it to True to include background graphics and colors. Printed colors may still be adjusted by browser print styling; the API documentation points to the CSS property -webkit-print-color-adjust when exact colors are required. The API also has header and footer options; check its documentation and the target page’s print layout when using them, since page content and print styling affect the result.

Save the PDF as bytes instead of a file

Omit path to receive PDF bytes, which you can pass to other Python code or write yourself:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.
from pathlib import Path
from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch()
    page = browser.new_page()
    page.goto("https://example.com")
    pdf_bytes = page.pdf(format="A4", print_background=True)
    Path("page.pdf").write_bytes(pdf_bytes)
    browser.close()

The Page API documents that page.pdf() returns PDF bytes and that path saves directly to disk.

When the page downloads a PDF attachment

page.pdf() prints the rendered webpage; it is not the method for saving a PDF file offered by a page’s download link or button. For a page-initiated download, wait for the download event and save the resulting Download before closing the browser context:

Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch()
    context = browser.new_context(accept_downloads=True)
    page = context.new_page()
    page.goto("https://example.com")
    with page.expect_download() as download_info:
        page.get_by_role("link", name="Download PDF").click()
    download = download_info.value
    download.save_as("downloaded.pdf")
    context.close()
    browser.close()

Replace the locator with one that matches the actual page. Playwright notes that downloads belonging to a context are deleted when that context closes, so save the file first. See the download documentation.

Common problems and fixes

  • Browser executable is missing: Install the browser binaries with python -m playwright install. If you only need Chromium, install that browser explicitly with python -m playwright install chromium.
  • The PDF lacks background colors or images: Set print_background=True. If colors still differ, check the page’s print CSS and consider -webkit-print-color-adjust.
  • The result looks unlike the browser window: The PDF uses print media by default. Call page.emulate_media(media="screen") before page.pdf() if screen CSS is needed.
  • The page is clipped or scaled unexpectedly: Review format, width, height, prefer_css_page_size, margins, and scale together. Remember that format takes precedence over width and height, and CSS page size takes precedence when prefer_css_page_size=True.
  • Content is missing because it loads late: A navigation completing does not establish that every site-specific component or font is ready. Wait for the selector or condition that indicates the needed content is present before printing, then inspect the PDF. The correct readiness condition depends on the website.
  • A PDF link does not produce the expected file: Determine whether it triggers a download or displays a PDF in the browser. Use the download event flow for an attachment; use page.pdf() only when you want to print the rendered page.
  • An API option is rejected: Compare the option with the documentation for the Playwright version installed in your environment. The cited Page API is the Next documentation, and options can vary by release.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Async Python and reliability considerations

Playwright also provides an asynchronous API. Keep the calls awaited and close the browser as part of the script’s lifecycle:

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.
Best Value
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.
import asyncio
from playwright.async_api import async_playwright

async def main():
    async with async_playwright() as p:
        browser = await p.chromium.launch()
        page = await browser.new_page()
        await page.goto("https://example.com")
        await page.pdf(path="page.pdf", format="A4", print_background=True)
        await browser.close()

asyncio.run(main())

For automated or repeated captures, define what counts as page readiness for your target site rather than assuming every page loads the same way. Browser installation, page behavior, fonts, dynamic content, and print styles can affect the file. The cited documentation establishes the API, not guaranteed fidelity for every website or platform.

Or skip the browser setup

If you need a PDF through an API rather than running Playwright locally, ScreenshotNeo accepts one GET request for a URL and can return a PDF. Its consent-banner, newsletter-popup, and chat-widget cleanup can be turned off; bot checks, blank pages, failed loads, timeouts, and cache hits are not billed, with response headers indicating the page verdict and billing status. Its MCP server provides capture_pdf alongside screenshot and page-info tools for AI agents. The free plan includes 1,000 shots a month with no card; paid plans start at $5 for 3,000. See the ScreenshotNeo API documentation.

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

Sign up for ScreenshotNeo’s free plan: 1,000 screenshots a month, no card required.

Frequently Asked Questions

Can Playwright generate a PDF with Firefox or WebKit?

The cited Playwright PDF documentation identifies Chromium as the supported engine for PDF generation; use Chromium for this workflow.

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

Can I choose a subset of pages in the PDF?

Yes. The Page API provides a page_ranges option for limiting output to selected pages.

Does enabling tagged output or an outline guarantee an accessible PDF?

No. Those options are documented, but their presence alone does not establish accessibility or navigation quality. Inspect the generated file in the viewer and workflow where it will be used.

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.