Skip to main content

selenium-print-external

Like https://github.com/bubblegumsoldier/selenium-print but you can bring your own driver, like undetected-chromedriver.

selenium-print-external is a lightweight Python package that enables printing the current page of an existing Selenium WebDriver instance to PDF.

This package is designed for users who manage their own Selenium WebDriver lifecycle (e.g., using selenium, undetected-chromedriver, or other WebDriver managers) and simply need a utility to trigger the browser's "Print to PDF" functionality on the currently loaded page.

It differs from libraries like selenium-print because it does not create or manage the WebDriver instance itself. You provide the driver, you navigate it, and then you use this library to print.

Features

  • Uses an existing, user-provided Selenium WebDriver instance.
  • Leverages the browser's built-in "Print to PDF" via Chrome DevTools Protocol (CDP).
  • Minimal dependencies (primarily selenium).
  • Avoids internal driver initialization, preventing common setup issues.
  • Returns PDF as raw bytes or saves directly to a file.

Installation

pip install selenium-print-external

You also need selenium installed, and whichever WebDriver package you are using (like selenium itself, undetected-chromedriver, etc.).

pip install selenium
# Optional, if using undetected-chromedriver
pip install undetected-chromedriver

Usage

  1. Initialize your WebDriver using your preferred method (e.g., selenium, undetected-chromedriver).
  2. Navigate the driver to the desired web page.
  3. Wait for any dynamic content to load if necessary.
  4. Instantiate ExternalSeleniumPrinter with your driver.
  5. Call print_current_page_to_pdf() to get the PDF bytes or save to a file.
  6. Manage your driver's lifecycle (e.g., driver.quit()) yourself.
import undetected_chromedriver as uc
from selenium_print_external import ExternalSeleniumPrinter
import time
import os

# --- User manages driver ---
options = uc.ChromeOptions()
# options.add_argument('--headless') # Headless often needed for servers
driver = uc.Chrome(options=options, use_subprocess=False)

try:
    # Navigate to the desired page
    url = "https://www.example.com"
    print(f"Navigating to {url}...")
    driver.get(url)

    # Optional: Wait for page elements or a specific condition
    print("Waiting for page to potentially load...")
    time.sleep(3) # Simple wait, replace with explicit waits if needed

    # --- Use the library to print ---
    print("Initializing printer...")
    printer = ExternalSeleniumPrinter(driver)

    # Option 1: Save to file
    output_pdf_path = "example_uc.pdf"
    print(f"Printing current page to {output_pdf_path}...")
    printer.print_current_page_to_pdf(output_path=output_pdf_path)
    print(f"PDF saved: {os.path.exists(output_pdf_path)}")

    # Option 2: Get raw bytes
    # print("Printing current page to bytes...")
    # pdf_bytes = printer.print_current_page_to_pdf()
    # print(f"Received {len(pdf_bytes)} bytes.")
    # with open("example_uc_bytes.pdf", "wb") as f:
    #     f.write(pdf_bytes)
    # print("PDF from bytes saved.")

finally:
    # --- User manages driver ---
    print("Quitting driver...")
    driver.quit()

Notes

  • This library relies on the Page.printToPDF Chrome DevTools Protocol command. Ensure your WebDriver instance supports CDP commands (most modern Chrome/Chromium drivers do).
  • The user is responsible for all driver setup, navigation, waiting strategies, and teardown (driver.quit()). This library only performs the print action.

Metadata

Release files for selenium-print-external 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for selenium-print-external 0.1.2
File Size Uploaded
selenium_print_external-0.1.2.tar.gz 3.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for selenium-print-external 0.1.2
File Interpreter ABI Platform
selenium_print_external-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 8.1 kB

Release files / selenium_print_external-0.1.2.tar.gz

Download URL selenium_print_external-0.1.2.tar.gz
Size 3.7 kB
Tags Source
SHA-256 checksum
How to use checksums
f2a40ca3850ef34e7c838f3f39aa0d4e1fc0990457e6c3db9454a744ad906e6b
BLAKE2b-256 checksum
How to use checksums
892c1e92b039d84e8eaadee0530da92babb8ee19f61739bb12cab33d111aaeca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.4 CPython/3.12.9 Darwin/24.3.0

Release files / selenium_print_external-0.1.2-py3-none-any.whl

Download URL selenium_print_external-0.1.2-py3-none-any.whl
Size 4.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dec535ff50ed9db030a3004b032d351f2274b042c7f73af5822fe57ae28cdf78
BLAKE2b-256 checksum
How to use checksums
769c27500c4dbd027238e0bdfc84e73d54766c9ddb874d5aa2eb11e3388ce4c0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.4 CPython/3.12.9 Darwin/24.3.0

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page