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
- Initialize your WebDriver using your preferred method (e.g.,
selenium,undetected-chromedriver). - Navigate the driver to the desired web page.
- Wait for any dynamic content to load if necessary.
- Instantiate
ExternalSeleniumPrinterwith your driver. - Call
print_current_page_to_pdf()to get the PDF bytes or save to a file. - 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.printToPDFChrome 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file selenium_print_external-0.1.1.tar.gz.
File metadata
- Download URL: selenium_print_external-0.1.1.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
219cd2c60c9874ed26655b3e9ba7d29535f24b38e81b54657b2737153a593638
|
|
| MD5 |
32ae4f90bcc9782d829b0eb360592126
|
|
| BLAKE2b-256 |
5bf8229b527c1f83167bdc592d049c2e96b57fec24874437a3cb8508a23ec49e
|
File details
Details for the file selenium_print_external-0.1.1-py3-none-any.whl.
File metadata
- Download URL: selenium_print_external-0.1.1-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.12.9 Darwin/24.3.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e821d850630f755f9e37b151b2c5da6a3927e8e2841b4224d906de22aacc0fd1
|
|
| MD5 |
fe005f2753b3b101ebba8542ce5ca6ef
|
|
| BLAKE2b-256 |
2c61dc7c649e10c2db0175ee029ac9a9bdb7047ca3d472b9ba444fa5a58b2736
|