PDF repair and reshaping tools.
A scanned workbook came back with 7 sane 8.5 x 11 pages and 11 pages that were
30+ inches tall. The cause shows up immediately in probe:
7 8.50x11.00 0 0 1
2167x2829px jpeg 1.3MB 257x257 dpi
8 29.64x37.23 0 0 1
2132x2679px png 3.3MB 69x70 dpi <-- embedded at 1px = 1pt
The scanner wrote those pages at 72 DPI, i.e. one image pixel per PDF point, so a ~2100 x 2800 px scan shot at ~250 DPI became a 30 x 39 inch page. The pixels were always fine; only the paper size was wrong.
python -m venv .venv
.venv/Scripts/pip install -e . # Windows
# .venv/bin/pip install -e . # POSIXReports page geometry and, crucially, the effective DPI of every placed image: pixel width divided by the width it is actually drawn at. Anything reading ~72 DPI is the bug above.
pdfforge probe scan.pdfRescales every page onto uniform paper, preserving aspect ratio.
pdfforge normalize scan.pdf -o fixed.pdf
pdfforge normalize scan.pdf --size a4
pdfforge normalize scan.pdf --dry-run # print the plan, write nothingKey properties:
- Lossless. Pages are re-placed as Form XObjects; embedded image streams are copied byte-for-byte. Nothing is resampled or recompressed — only the matrix mapping the image onto paper changes.
- Never deforms. Scans are rarely exactly 8.5 x 11.
--mode fit(default) scales to contain the whole page and adds a blank margin;--mode fillcovers the paper and lets the page box crop the overflow. Both preserve the source aspect ratio exactly. - Leaves good pages alone. Pages already at the target size are copied
verbatim rather than round-tripped.
--forceoverrides. - Keeps landscape landscape. A landscape source lands on landscape paper
instead of being shrunk into a portrait letterbox.
--no-auto-orientdisables.
Options:
| flag | meaning |
|---|---|
-s, --size |
letter, legal, tabloid, a3, a4, a5, or WxH[in|mm|pt] |
-m, --mode |
fit (contain, default) or fill (cover + crop) |
--tolerance |
points of slop before a page counts as wrong-sized (default 1.0) |
--no-auto-orient |
don't turn the target sideways for landscape pages |
--force |
re-place every page instead of copying conforming ones |
-n, --dry-run |
print the plan without writing |
Fixes the pixels rather than the geometry: rotates sideways scans upright, removes skew, trims the scanner surround, then lays the result out on uniform paper.
pdfforge clean scan.pdf -o fixed.pdf
pdfforge clean scan.pdf --no-auto-rotate # skip the OCR pass; much faster
pdfforge clean scan.pdf --gray -q 75 # smaller outputUnlike normalize, this re-encodes the imagery — that is why it is a
separate command rather than a flag.
Auto-rotate. Tesseract's own orientation detection (--psm 0) is
unreliable on handwriting; on this workbook it confidently reported
180-degree flips for five upright pages, with confidences no lower than its
one correct call. So clean instead OCRs the page at 0/90/270 degrees and
keeps whichever reads best, scoring `characters recognised at confidence
= 60
. On the same pages the correct orientation won by 2.4x to 6x every time. 180-degree flips stay opt-in behind--upside-down`, since a flip does not change page shape and is the case OCR most often gets wrong.
Needs tesseract on PATH. Without it, pass --no-auto-rotate.
Deskew. Coarse-to-fine search for the rotation maximising the sharpness of the horizontal projection profile — level baselines and ruled lines make row sums swing hardest between line and gap.
Border trim. Each edge is walked inward until it looks like paper.
Paper is detected as bright and desaturated: brightness alone fails
because the coloured fringe a scanner lays past the edge of a page is often
brighter than the page in greyscale. Trimming is capped per edge
(--max-trim, default 12%) so a dark page cannot be eaten alive.
Order is rotate, trim, deskew — and it matters. Deskew pads the corners with page white, and that padding reads as perfect paper, so trimming afterwards finds nothing. Trimming first also stops the hard edge of the scanner surround from biasing the skew estimate.
Resolution defaults to each page's native scan density, so the render neither upsamples the scan nor discards detail. Only the output density is capped (400 DPI).
| flag | meaning |
|---|---|
--dpi |
force a render resolution instead of the native one |
--format / -q |
jpeg (default) or png; JPEG quality (default 85) |
--gray |
convert to greyscale |
--no-auto-rotate |
skip orientation detection (no tesseract needed) |
--upside-down |
also consider 180-degree flips |
--no-deskew, --no-trim |
disable a stage |
--max-trim |
most of each edge trimming may remove (default 0.12) |
18 pages: 11 rescaled, 7 copied
distinct page sizes: 12 -> 1
Effective DPI on the broken pages went from a uniform 72 to 236-277, matching the 220-265 of the pages that were already correct.
clean on the same file, 53 MB in:
18 pages: 1 rotated, 18 deskewed, 17 trimmed
wrote Bryan-clean.pdf (7.7 MB)
The one rotation is a landscape data chart that had been scanned sideways; it now lands on 11 x 8.5 paper. Skew corrections ranged from 0.05 to 1.7 degrees.
.venv/Scripts/pip install pytest
.venv/Scripts/python -m pytest