Skip to main content

a4-printer-interface

a4-printer-interface is a focused Python library for discovering local printer queues and printing PDF documents or images on Windows, X11, and macOS.

Every input page is normalized for a portrait A4 sheet before it is submitted:

  • Landscape pages are rotated to portrait.
  • Pages of any size are scaled proportionally to fit A4.
  • Pages whose aspect ratio differs from A4 are centered with white padding.
  • Color and true grayscale rendering are supported.

The source PDF is never modified. PDFium renders each page at 300 DPI so that layout and grayscale results do not depend on a desktop PDF viewer.

Images are EXIF-orientation aware and become a temporary, single-page portrait A4 PDF before printing. Images are rotated so their long edge matches the long edge of the paper, then scaled as large as possible while fitting entirely within the A4 canvas and centered on it. Multi-frame images use their first frame.

Requirements

  • Python 3.10 or newer.
  • A printer configured in Windows, CUPS on X11, or the macOS printing system.
  • The CUPS client commands lpstat and lp on X11. They are built into macOS.
  • An A4-capable printer queue.

The lightweight runtime uses pypdfium2, Pillow, and ReportLab. Pillow-supported image formats such as PNG, JPEG, TIFF, BMP, and WebP can be printed. Windows also installs pywin32 for direct GDI printing. The package does not install a GUI toolkit or require an external PDF viewer.

Installation

From the repository root:

python -m pip install a4_printer_interface

Discover printers

from a4_printer_interface import PrinterManager

manager = PrinterManager()
for printer in manager.list_printers():
    print(printer.get_name(), printer.get_uuid())

Names are matched case-insensitively. UUIDs are deterministic identifiers derived from the operating-system queue and its backend identity.

office_queues = manager.get_printer_by_name("Office Printer")
printer = manager.get_printer_by_uuid("8fd0d51f-12c8-5b50-a2f1-4f64641ae77c")

Create a new PrinterManager when you need to refresh the operating system's printer list.

Check printer activity

Use get_busy() to check whether the selected queue is currently executing a print job:

if printer.get_busy():
    print("The printer is currently printing.")

The method returns False for an idle printer or for jobs that are only queued, paused, completed, or waiting for intervention. It raises RuntimeError when the native print service cannot provide the queue status.

Print a document

Use the built-in DefaultSettings for color printing:

from a4_printer_interface import DefaultSettings, PrinterManager

printer = PrinterManager().list_printers()[0]
printer.print("report.pdf", DefaultSettings())

Use the built-in GrayscaleSettings for grayscale printing:

from a4_printer_interface import GrayscaleSettings

printer.print("report.pdf", GrayscaleSettings())

Images use the same API and settings:

printer.print("photo.jpg", DefaultSettings())

In grayscale mode, PDFium renders the document directly to grayscale pixels before it reaches the driver. The native print job is also marked as monochrome. In color mode, the original page colors are preserved.

print() rejects files that are neither PDFs nor Pillow-supported images, malformed or password-protected PDFs, missing files, unavailable queues, and jobs rejected by the native print system. A successful return means the operating system accepted the job; it does not mean that physical printing has finished.

Print the built-in test page

The package includes a low-ink, single-page color test PDF with small cyan, magenta, yellow, and black patches, a black cross grid, and English and Chinese sample text. Print it in color with the default settings:

printer.print_test_page()

The optional settings argument can force a grayscale test:

printer.print_test_page(GrayscaleSettings())

Platform backends

  • Windows discovers queues with the print spooler and sends normalized A4 page images directly to the selected printer through GDI. Printer defaults are not changed.
  • X11 and macOS discover CUPS queues with lpstat. They receive a temporary, normalized A4 PDF through lp; the temporary file is deleted after submission.

Command line

List available queues after installation:

a4-printers

The equivalent development command is:

python -m a4_printer_interface

Development

Run the test suite:

python -m unittest discover -s tests -v

Tests inspect normalized PDF page sizes and rendered pixels. Operating-system printer services are mocked, so the tests never submit a real print job. CI runs them on Windows, Ubuntu, and macOS with Python 3.10 and 3.13.

License

MIT

Download files

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

Source Distribution

a4_printer_interface-0.1.1.tar.gz (33.9 kB view details)

Uploaded Source

Built Distribution

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

a4_printer_interface-0.1.1-py3-none-any.whl (27.5 kB view details)

Uploaded Python 3

File details

Details for the file a4_printer_interface-0.1.1.tar.gz.

File metadata

  • Download URL: a4_printer_interface-0.1.1.tar.gz
  • Upload date:
  • Size: 33.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.14.6 Windows/11

File hashes

Hashes for a4_printer_interface-0.1.1.tar.gz
Algorithm Hash digest
SHA256 4290becbdc946b9c364b859e3fb8e88428546ea44cd01c3e0ae63ed5c124bb40
MD5 de6d30554b1dae4bb1d1eeb8db6dbed1
BLAKE2b-256 b063b0fdd3f061eadff1f9487f817a6cad86035278886db3da44af47522451eb

See more details on using hashes here.

File details

Details for the file a4_printer_interface-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for a4_printer_interface-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 93287b97acb0b1b86f31cadb0b62537c6293f49eeaafb82cba4fcf6f84ef9f7c
MD5 30dfff60ddd02e3f6af703e81f51665f
BLAKE2b-256 6df10955b7d609907d99e0d533362bb0568c6b03133aae7e8f9b84095a94441b

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 files

0.1.0

2 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