How belval/pdf2image works
Purpose and Public API
pdf2image is a Python 3.7+ library that converts PDF pages into Pillow (PIL.Image) images or persisted image paths. It wraps Poppler command-line utilities rather than implementing PDF rendering. Public imports and conversion entry points are exposed through:
• pdf2image/__init__.py
• pdf2image/pdf2image.py
Primary APIs are convert_from_path, convert_from_bytes, pdfinfo_from_path, and pdfinfo_from_bytes.
Input and Metadata Stage
Path-based conversion begins by querying Poppler pdfinfo for page count and document metadata, then validates page ranges and conversion options. Byte-based APIs adapt in-memory PDFs into the same path-oriented pipeline, typically through temporary files.
• pdf2image/pdf2image.py
• pdf2image/parsers.py
This boundary separates PDF/source handling from rendering and makes Poppler availability a runtime requirement.
Poppler Process Orchestration
The core module builds and launches subprocess commands for pdfinfo, pdftoppm, or optionally pdftocairo. It maps API options—DPI, output format, crop box, passwords, transparency, grayscale, sizing, JPEG settings, timeout, and explicit Poppler installation path—to CLI arguments.
• pdf2image/pdf2image.py
• pdf2image/generators.py
External executables are resolved from PATH or poppler_path; Windows-specific process behavior is handled at this boundary.
Parallel Page Rendering
For multi-page documents, the conversion layer partitions page ranges across a caller-controlled thread count and starts independent Poppler processes. Each process renders its assigned pages into a specified output directory or a managed temporary directory.
• pdf2image/pdf2image.py
• pdf2image/generators.py
The concurrency model is process orchestration from Python, while rendering remains in native Poppler utilities. Disk output is an important performance and memory boundary.
Output Materialization
Rendered files are either loaded with Pillow and returned as a list of image objects, or returned as file paths when paths_only=True. Format selection controls both the Poppler executable/flags and image-loading behavior. Temporary output is retained only as needed to construct in-memory images.
• pdf2image/pdf2image.py
This stage provides the main memory-management tradeoff: image objects are convenient but can exhaust memory for large PDFs; path-only/output-folder workflows keep results on disk.
Parsing and Failure Semantics
Poppler stdout/stderr is parsed into Python metadata and normalized into library-specific errors for missing executables, page-count failures, malformed PDFs, password failures, and conversion timeouts. Strict mode can surface PDF syntax errors detected during rendering.
• pdf2image/parsers.py
• pdf2image/exceptions.py
• pdf2image/pdf2image.py
Packaging and Runtime Dependencies
The distributable Python package is defined by setuptools configuration and includes typing metadata. Runtime integration depends materially on Pillow and a separately installed Poppler distribution providing pdfinfo, pdftoppm, and optionally pdftocairo.
• setup.py
• setup.cfg
• pdf2image/py.typed
Main entry points: pdf2image.py Source validation · parsers.py Poppler output parsers · __init__.py Public imports · setup.py Package distribution
Components 18
Python API
- Public imports · Exports the supported conversion and metadata APIs.
pdf2image/__init__.py - Conversion APIs · Path- and byte-based PDF conversion entry points.
pdf2image/pdf2image.py - Metadata APIs · pdfinfo-based document metadata entry points.
pdf2image/pdf2image.py
Conversion Pipeline
- Byte input adapter · Writes in-memory PDFs into the path-oriented workflow.
pdf2image/pdf2image.py - Source validation · Obtains page count and validates ranges and options.
pdf2image/pdf2image.py - CLI command builder · Maps Python options to Poppler command arguments.
pdf2image/generators.py - Poppler orchestration · Resolves executables and launches subprocesses.
pdf2image/pdf2image.py - Page partitioning · Splits page ranges across caller-selected workers.
pdf2image/pdf2image.py - Rendered files · Uses caller output folders or managed temporary storage.
pdf2image/pdf2image.py - Output materialization · Returns Pillow images or persisted file paths.
pdf2image/pdf2image.py - Poppler output parsers · Parses metadata and rendering diagnostics.
pdf2image/parsers.py - Library errors · Normalizes executable, PDF, password, and timeout failures.
pdf2image/exceptions.py
Runtime Dependencies
- pdfinfo · Supplies page count and PDF metadata.
- pdftoppm / pdftocairo · Native PDF page renderers; Cairo is optional.
- Pillow · Loads rendered files into PIL.Image objects.
- Package distribution · Defines the distributable package and typing metadata.
setup.py - Package metadata
setup.cfg - Typing marker · Marks bundled type information.
pdf2image/py.typed
Connections 19
- Public imports → Conversion APIs: exports
- Public imports → Metadata APIs: exports
- Conversion APIs → Byte input adapter: bytes input
- Byte input adapter → Source validation: temporary PDF path
- Conversion APIs → Source validation: path input
- Metadata APIs → Source validation: queries
- Source validation → CLI command builder: validated options
- Source validation → pdfinfo: page count / metadata
- pdfinfo → Poppler output parsers: stdout / stderr
- Poppler output parsers → Library errors: normalizes failures
- CLI command builder → Page partitioning: render plan
- Page partitioning → Poppler orchestration: worker page ranges
- Poppler orchestration → pdftoppm / pdftocairo: launches
- pdftoppm / pdftocairo → Rendered files: writes page images
- Rendered files → Output materialization: rendered files
- Output materialization → Pillow: loads image objects
- Poppler orchestration → Poppler output parsers: captures diagnostics
- Package distribution → Package metadata: uses
- Package distribution → Typing marker: includes