PDFOrienter
Intelligent, parallel PDF page rotation correction for Python.
PDFOrienter analyses every page of one or more PDF files, detects incorrect orientations, and fixes them in a single write pass — with no unnecessary re-processing.
Features
- Two-phase pipeline — detect all pages in parallel, then apply all corrections in a single write
- Smart strategy selection — uses fast text-direction analysis for text-based pages; falls back to Tesseract OSD only for image/scanned pages
- Dynamic parallelism — automatically uses 75 % of available CPU cores; scales from 4 to 64+ cores without any configuration change
- Detailed structured logging — per-page and per-file timing, rotation details, confidence scores, RAM and CPU usage
- Zero intermediate files — corrected PDFs are written once; originals are never modified
- Package-ready — clean modular design, typed, fully testable
Requirements
Python
Python 3.10 or newer.
System dependency — Tesseract
Tesseract must be installed on the host system before installing PDFOrienter.
Ubuntu / Debian
sudo apt-get update && sudo apt-get install -y tesseract-ocr
macOS (Homebrew)
brew install tesseract
Windows
Download and run the installer from the Tesseract UB Mannheim releases, then add the install directory to your PATH.
Installation
pip install pdforienter
For development (includes linting + test tools):
git clone https://github.com/your-org/pdforienter.git
cd pdforienter
pip install -e ".[dev]"
Quick Start
Command line
# Fix a single PDF
pdforienter invoice.pdf --output ./fixed
# Fix every PDF in a directory
pdforienter /scans/ --output /corrected
# Mix files and directories
pdforienter report.pdf /archive/ receipts.pdf --output ./out
Python API
from pdforienter import run_pipeline
from pdforienter.logging.writer import write_log
result = run_pipeline(
pdf_paths=["invoice.pdf", "report.pdf"],
output_dir="./corrected",
)
# Write the structured log file
log_path = write_log(result, "./corrected")
print(f"{result.total_pages_changed} pages corrected in {result.total_duration_seconds:.1f}s")
print(f"Log: {log_path}")
Log File
Every run produces a timestamped .log file in the output directory.
PDFOrienter Run Log — 2024-11-01 14:32:05
============================================================
[RUN SUMMARY]
Total files processed : 3
Total pages : 247
Pages rotated : 18
Text pages : 201
Scanned pages (OCR) : 46
Skipped pages : 0
Workers used : 6
Current RAM usage : 312.4 MB
Total time : 42.18s
------------------------------------------------------------
[FILE] /scans/invoice.pdf
Output : /corrected/invoice_corrected.pdf
Total pages : 12
Pages changed : 3
Text pages : 8
Scanned pages : 4
Skipped pages : 0
Detection time : 9.41s
Correction time : 0.23s
Total time : 9.64s
[PAGE DETAILS]
p 1 | text | OK | angle= 0° | conf= 98.2 | 0.11s | No rotation needed.
p 2 | scanned | CHANGED | angle= 90° | conf= 87.5 | 2.34s | Rotation of 90° detected (confidence 87.5).
...
Project Structure
pdforienter/
├── pdforienter/
│ ├── __init__.py # Public API: run_pipeline
│ ├── config.py # Tuneable constants (worker count, thresholds)
│ ├── models.py # Typed data classes (PageResult, FileResult, RunResult)
│ ├── cli.py # Command-line interface
│ ├── core/
│ │ ├── pipeline.py # Top-level orchestrator
│ │ ├── processor.py # Per-file orchestrator (Phase 1 + Phase 2)
│ │ ├── analyzer.py # Per-page worker (dispatched to subprocess)
│ │ ├── classifier.py # Text vs scanned page detection
│ │ ├── detector.py # Orientation detection (text + OSD strategies)
│ │ └── corrector.py # Single-pass rotation applier
│ ├── logging/
│ │ ├── formatter.py # RunResult → structured log string
│ │ └── writer.py # Write log file to disk
│ └── utils/
│ ├── fs.py # Filesystem helpers
│ └── resources.py # CPU / RAM telemetry
├── tests/
│ └── test_core.py
├── pyproject.toml
└── README.md
Configuration
All tuneable constants live in pdforienter/config.py.
| Constant | Default | Description |
|---|---|---|
MAX_WORKERS |
floor(cpu_count × 0.75) |
Worker processes for parallel page analysis |
OSD_CONFIDENCE_THRESHOLD |
10.0 |
Minimum Tesseract OSD confidence to trust a result |
TESSERACT_OSD_PSM |
0 |
Tesseract page segmentation mode (0 = OSD only) |
_RENDER_DPI (detector.py) |
150 |
DPI used when rasterising pages for OSD |
_MIN_CHAR_COUNT (classifier.py) |
20 |
Minimum characters to classify a page as text-based |
How It Works
Phase 1 — Parallel Detection
Each page is dispatched to a subprocess worker via ProcessPoolExecutor. Workers run concurrently up to MAX_WORKERS.
For each page:
- Classify — does the page have selectable text?
- Detect orientation
- Text page → analyse character direction vectors (fast, no OCR)
- Scanned page → rasterise at 300 DPI and run multi-pass Tesseract OSD
- Return a
PageResultwith the detected angle, confidence, and timing
Phase 2 — Single-Pass Correction
After all pages are analysed, a single write pass produces the corrected PDF. By default PDFOrienter bakes the rotation into each page's content so the output is genuinely upright with /Rotate=0 — it displays correctly in every viewer and tool, including those that ignore the /Rotate page attribute (image converters, some print drivers, OCR front-ends).
Vector text stays selectable after baking, but page-level annotations, links, and form fields are not preserved. If you need those, pass --no-bake (CLI) or bake=False (API) for lossless metadata-only rotation that sets /Rotate instead.
# Default: physically upright output, works everywhere
pdforienter scan.pdf --output ./fixed
# Lossless: keep annotations/forms, rely on viewer honouring /Rotate
pdforienter scan.pdf --output ./fixed --no-bake
Performance
Typical estimates on an 8-core server (6 workers) with mixed text/scanned PDFs:
| Scenario | Estimate |
|---|---|
| 2 000 pages, all text-based | ~1–2 minutes |
| 2 000 pages, mixed 50/50 | ~7–8 minutes |
| 2 000 pages, all scanned | ~15–17 minutes |
RAM usage: ~200–400 MB per Tesseract worker. 6 workers ≈ 2.5 GB peak. Well within a 16 GB server.
Running Tests
pytest tests/ -v
License
MIT
Metadata
Release files for pdforienter 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pdforienter-0.1.1.tar.gz | 27.6 kB | Without provenance | Details for pdforienter-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pdforienter-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for pdforienter-0.1.1-py3-none-any.whl |
Total release size: 51.6 kB
Release files / pdforienter-0.1.1.tar.gz
| Download URL | pdforienter-0.1.1.tar.gz |
|---|---|
| Size | 27.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3851b665cc08848f05f2f4a681adcb513021f2d9d7529de4d51b72bbf9069bac
|
|
BLAKE2b-256 checksum How to use checksums |
f337414155f8a93dbdf08abed4fed5ab1fbe48d19ab36e576b770870ffe79ab0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.11
|
Release files / pdforienter-0.1.1-py3-none-any.whl
| Download URL | pdforienter-0.1.1-py3-none-any.whl |
|---|---|
| Size | 24.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ef97e7d18397e6034ad8ab0b3cfdc3f3cc6bac10efb6f3d6a5398004f14311e3
|
|
BLAKE2b-256 checksum How to use checksums |
c3f79b769e850e0156b324462fbc762beed04cc125f942b600aba98f6568d016
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.11
|
Security for pdforienter 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.