Cross-platform document printing with PDF fallback using only standard library
Project description
Universal Printer
A dependency-free Python library for cross-platform document printing with PDF fallback. Works on Windows, macOS, and Linux using only the Python standard library.
Features
- Cross-platform: Works on Windows, macOS, and Linux
- Dependency-free: Uses only Python standard library
- PDF fallback: Generates minimal PDFs when printing fails
- Simple API: Easy to use with just a few lines of code
- Flexible input: Print strings, file paths, or any text content
Installation
pip install universal-printer
Quick Start
from universal_printer import DocumentPrinter
# Create a printer instance
printer = DocumentPrinter()
# Print a simple string
success, message, pdf_path = printer.print_document("Hello, World!")
if success:
print("Document printed successfully!")
else:
print(f"Printing failed: {message}")
if pdf_path:
print(f"PDF fallback saved to: {pdf_path}")
Usage Examples
Print text content
from universal_printer import DocumentPrinter
printer = DocumentPrinter()
# Print simple text
text = """
This is a sample document.
It contains multiple lines.
Perfect for testing the printer!
"""
success, message, pdf_path = printer.print_document(text)
Print an existing file
# Print an existing text file
success, message, pdf_path = printer.print_document("/path/to/your/file.txt")
Specify a printer
# Print to a specific printer
success, message, pdf_path = printer.print_document(
"Hello, World!",
printer_name="Your Printer Name"
)
Print to PDF directly
# Print to PDF (useful for "Microsoft Print to PDF" on Windows)
success, message, pdf_path = printer.print_document(
"Hello, World!",
printer_name="Microsoft Print to PDF",
pdf_filename="my_document.pdf"
)
Disable PDF fallback
# Disable PDF fallback if printing fails
success, message, pdf_path = printer.print_document(
"Hello, World!",
fallback_to_pdf=False
)
API Reference
DocumentPrinter
The main class for printing documents.
Methods
print_document(content, printer_name=None, fallback_to_pdf=True, pdf_filename=None)
Print the given content.
Parameters:
content(str): Text content to print, or path to an existing fileprinter_name(str, optional): Name of the printer to usefallback_to_pdf(bool): Whether to create a PDF if printing fails (default: True)pdf_filename(str, optional): Custom filename for PDF fallback
Returns:
tuple: (success: bool, message: str, pdf_path: str or None)
Platform-Specific Behavior
Windows
- Uses
rundll32.exewith shell32.dll for printing - Supports "Microsoft Print to PDF" printer
- Falls back to Notepad for .txt files if needed
macOS/Linux
- Uses the
lpcommand (CUPS) - Supports print-to-file for PDF generation
- Requires CUPS to be installed and configured
PDF Fallback
- Generates minimal but valid PDF files
- Uses Helvetica font at 12pt
- Places text starting at coordinates (50, 750)
- Handles basic text escaping for PDF format
Requirements
- Python 3.7 or higher
- No external dependencies (uses only standard library)
License
MIT License - see LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Changelog
0.1.0
- Initial release
- Cross-platform printing support
- PDF fallback functionality
- Standard library only implementation
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 universal_printer-0.1.0.tar.gz.
File metadata
- Download URL: universal_printer-0.1.0.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ff0132fd17e0b247a5705d8ca72e2bcd7f88c40086930555fecadb04a08c57d3
|
|
| MD5 |
37afe355ec6e03fb6bb53401db1cf05a
|
|
| BLAKE2b-256 |
14ca405eef941a04475bc635854e9a829f15d2b35765114c1cd51a0149ff8b12
|
File details
Details for the file universal_printer-0.1.0-py3-none-any.whl.
File metadata
- Download URL: universal_printer-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dee3c3e870d7d2bee6c57e12c116ea57ae4914f879fb043be4207c541520fc75
|
|
| MD5 |
dac52ddd784ebf9ac4a2bf4cd45883f2
|
|
| BLAKE2b-256 |
ff768b6b9513d495e9f072dca9f76b24369d1510bb449e746f1920f3465b6f3a
|