Skip to main content

No project description provided

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

selenium_print_external-0.1.2.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

selenium_print_external-0.1.2-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file selenium_print_external-0.1.2.tar.gz.

File metadata

  • Download URL: selenium_print_external-0.1.2.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.9 Darwin/24.3.0

File hashes

Hashes for selenium_print_external-0.1.2.tar.gz
Algorithm Hash digest
SHA256 f2a40ca3850ef34e7c838f3f39aa0d4e1fc0990457e6c3db9454a744ad906e6b
MD5 dd388c91299a9dccab062e75b400f836
BLAKE2b-256 892c1e92b039d84e8eaadee0530da92babb8ee19f61739bb12cab33d111aaeca

See more details on using hashes here.

File details

Details for the file selenium_print_external-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for selenium_print_external-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 dec535ff50ed9db030a3004b032d351f2274b042c7f73af5822fe57ae28cdf78
MD5 c247c2591abcd20a60e83f011ea4e8ec
BLAKE2b-256 769c27500c4dbd027238e0bdfc84e73d54766c9ddb874d5aa2eb11e3388ce4c0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page