To save a webpage as a PDF with Selenium and Chrome, open the page in a headless Chrome WebDriver session, call Selenium’s driver.print_page(), decode its base64 result, and write the bytes to a file. If you do not need WebDriver automation, Chrome can also print a URL directly from its headless command line.
Save a webpage as PDF with Selenium in Python
Selenium’s print-page operation prints the page currently open in the browser and returns the PDF as base64-encoded content. Decode that content before writing it to disk. Chromium printing through this WebDriver operation requires headless mode.
As an Amazon Associate I earn from qualifying purchases.
import base64
from pathlib import Path
from selenium import webdriver
from selenium.webdriver.common.print_page_options import PrintOptions
options = webdriver.ChromeOptions()
options.add_argument("--headless=new")
driver = webdriver.Chrome(options=options)
try:
driver.get("https://example.com")
pdf_base64 = driver.print_page(PrintOptions())
Path("page.pdf").write_bytes(base64.b64decode(pdf_base64))
finally:
driver.quit()
- Install Selenium in the Python environment you will run the script from, and ensure Chrome is installed.
- Set the URL in
driver.get()and choose the output filename inPath("page.pdf"). - Run the script. It opens the URL, requests a PDF of the current page, decodes the returned data, and writes
page.pdfin the current working directory. - Open the PDF and check that the content and page breaks meet your needs.
The Selenium Print Page guide describes this API and its print settings. The Python WebDriver API describes driver.print_page() as making a best effort to return a PDF based on the supplied parameters; it is not a guarantee that every site’s dynamic content will be ready or included.
Recommended Free Tools
Set print layout with Selenium
Use Selenium’s PrintOptions to control print layout. The documented settings cover orientation, page ranges, paper size, margins, scale, background rendering, and shrink-to-fit. For example, configure the options before printing:
#1 Best Overall
- 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
print_options = PrintOptions()
print_options.orientation = "landscape"
pdf_base64 = driver.print_page(print_options)
Path("page.pdf").write_bytes(base64.b64decode(pdf_base64))
Set only the properties your output requires; the available values and behavior are documented in the Selenium print-page reference. Paper size, margins, scale, and whether backgrounds print can change pagination and appearance, so inspect a generated PDF rather than assuming screen layout and print layout will match.
Use Chrome headless without Selenium
If you only need Chrome to load a URL and save a PDF—not to automate interactions or manage the browser through WebDriver—use Chrome’s command-line printing:
Rank #2
- 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)
chrome --headless --print-to-pdf https://example.com
Chrome writes output.pdf in the current working directory by default. To suppress the print header and footer, use --no-pdf-header-footer. To set a maximum wait before capture, use the documented timeout flag, for example:
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Fix the driver behind crashes, sound loss and screen glitches3Repair Windows errors before they cause bigger problemschrome --headless --print-to-pdf --no-pdf-header-footer --timeout=5000 https://example.com
The timeout value is in milliseconds; Chrome’s example uses 5000. It is a capture-time limit, not proof that every page has finished loading. Consult the Chrome Headless command-line reference for the documented flags, and check behavior against your installed Chrome version.
Rank #3
- FAST DOCUMENT SCANNING — Document scanner with feeder allows you to speed through stacks with a 50-sheet Auto Document Feeder (ADF); Efficient office scanner to help you scan more productively
- INTUITIVE, HIGH-SPEED SOFTWARE — Quickly scan with this desktop document scanner; Epson ScanSmart Software lets you easily preview scans, email files, upload to the cloud, and more; Plus, automatic file naming saves even more time
- SEAMLESS INTEGRATION — Easily incorporate your data into most document management software with the included TWAIN driver; Office document scanner integrates seamlessly with business workflows
- EASY SHARING — Duplex scanner allows you to scan straight to email or popular cloud storage2 services like Dropbox, Evernote, Google Drive, and OneDrive for simple storage and sharing
- SIMPLE FILE MANAGEMENT — Scanner allows the creation of searchable PDFs with Optical Character Recognition (OCR) and convert scans to editable Word or Excel files effortlessly; Designed for home and office document scanning
Choose Selenium or the Chrome command line
| Need | Selenium WebDriver | Chrome headless command line |
|---|---|---|
| Automate browser actions before printing | Use WebDriver to navigate and interact, then print the current page. | Directly captures a URL; the documented command is not a WebDriver automation workflow. |
| Control print layout | PrintOptions exposes orientation, page ranges, page size, margins, scale, background rendering, and shrink-to-fit. |
The referenced documentation describes flags including header/footer omission and capture timeout. |
| Choose how output is saved | Returns base64-encoded PDF content; your code decodes and writes it to the desired path. | Writes output.pdf to the current working directory by default. |
| Handle page readiness | Your automation can wait for the site-specific content it needs before printing. | --timeout sets a capture wait limit; a fixed delay is not a universal guarantee of readiness. |
Wait for dynamic content before printing
A page can render its initial HTML before the content you care about appears. If the site uses client-side rendering or delayed requests, wait for a meaningful condition before calling print_page(), such as the presence of the element containing the content. The exact selector and wait condition depend on the target site; no general wait duration ensures all dynamic content, lazy-loaded assets, or personalized state will be present.
Chrome’s command-line timeout only sets the maximum wait before capture even if a page is still loading. For either approach, inspect the PDF for missing sections, incomplete images, or unexpected page breaks. If the content is absent, adjust readiness handling or print layout and generate it again.
Rank #4
- Scanner type: Document
- Connectivity technology: USB
- With Auto Scan Mode, the scanner automatically detects what you're scanning
- Digitize documents and images
Check Chrome and ChromeDriver compatibility
The Selenium Chrome documentation says Selenium 4 is compatible with Chrome 75 and newer and advises matching Chrome and ChromeDriver major versions. Treat that as the documentation’s compatibility guidance, not a guarantee for every current installation: check the browser and driver versions available in your environment and follow the current Selenium guidance for them.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Troubleshooting
- Printing fails in a visible Chromium session: Selenium’s print-page documentation requires Chromium to be headless for this operation. Add a headless argument to
ChromeOptions, as in the example. - The PDF file is empty or invalid: Selenium returns encoded content, not ready-to-write PDF bytes. Decode it with
base64.b64decode()before writing. - The script cannot start Chrome: Check that Chrome is installed and that the Chrome and ChromeDriver major versions match, as Selenium’s Chrome guide advises.
- Content is missing from the PDF: The page may not have finished rendering the required content when printing began. Wait for a site-specific readiness condition, then print again and inspect the result.
- The layout is wrong or pages break unexpectedly: Review orientation, paper size, margins, scale, background rendering, and shrink-to-fit in
PrintOptions; generate another PDF and verify it. - The CLI PDF includes unwanted headers or captures too early: Use
--no-pdf-header-footerto omit headers and footers, and adjust--timeoutwhen appropriate. A timeout alone does not establish that a particular site’s content is ready. - The CLI output is not where expected: The documented default is
output.pdfin the current working directory. Run Chrome from the intended directory or consult the installed version’s command-line behavior.
Or skip the browser setup
For a direct screenshot or PDF API request, ScreenshotNeo accepts a URL in one GET request. Its clean-shot flow accepts cookie or consent banners like a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each step can be turned off. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, and responses include X-Page-Verdict and X-Billed headers. ScreenshotNeo also offers an MCP server with take_screenshot, get_page_info, and capture_pdf tools for AI agents and MCP clients.
For its PDF output, request the PDF format using the options documented in the ScreenshotNeo API documentation. The following one-call cURL example saves a PDF response; adapt the target URL and PDF options to the documented API parameters:
Best Value
- OUR MOST ADVANCED SCANSNAP. Large touchscreen, fast 45ppm double-sided scanning, 100-sheet document feeder, Wi-Fi and USB connectivity, automatic optimizations, and support for cloud services. Upgraded replacement for the discontinued iX1600
- CUSTOMIZABLE. SHARABLE. Select personalized profiles from the touchscreen. Send to PC, Mac, mobile devices, and clouds. QUICK MENU lets you quickly scan-drag-drop to your favorite computer apps
- STABLE WIRELESS OR USB CONNECTION. Built-in Wi-Fi 6 for the fastest and most secure scanning. Connect to smart devices or cloud services without a computer. USB-C connection also available
- PHOTO AND DOCUMENT ORGANIZATION MADE EFFORTLESS. Easily manage, edit, and use scanned data from documents, receipts, photos, and business cards. Automatically optimize, name, and sort files
- AVOIDS PAPER JAMS AND DAMAGE. Features a brake roller system to feed paper smoothly, a multi-feed sensor that detects pages stuck together, and skew detection to prevent paper damage and data loss
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
ScreenshotNeo’s free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000. See ScreenshotNeo and sign up free for 1,000 screenshots a month with no card.
Frequently Asked Questions
Does Selenium return a PDF file from print_page()?
It returns base64-encoded PDF content. Decode the value before writing the PDF bytes to a file.
Can I print a webpage with Chrome without Selenium?
Yes. Chrome’s headless command-line option can print a URL directly to a PDF.
Quick Recap
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.




