A simple python script that reads a PDF and turns it into a PDF containing only a single image per page. Can also process an entire folder at once. All vector text is lost. It is intended as a simple compression tool for PDF scans or bad vector PDFs (as some handwritten vector PDFs tend to be huge for reasons beyond me).
python3 pdflat.py --dpi [DPI] --width [A4/letter/XXcm/XXin] --output [./out-dir/] ./in-dir/*.pdfDefining a width is recommended. This ensures that a PDF that is defined as A0 will not have bigger resolution. All pages in the output will then have the exact same width (and therefore the same resolution horizontally).
A dpi value between 150 and 300 seems to work quite well in practice.
python3 pdflat.py --dpi 300 --quality 90 /path/to/input.pdf
python3 pdflat.py --dpi 200 --quality 75 --width a4 --height a4 /dir/*.pdf
python3 pdflat.py --dpi 150 --quality 50 --width a4~ --output outdir a.pdf b.pdfOne can also use the --inplace flag to overwrite PDFs in place.
The program works with Bash wildcards, for example: python3 pdflat.py --dpi 300 --quality 90 --output ./docs-comp.pdf ./docs/*.pdf.
The programs is a single standalone python script. There are two dependencies:
pymupdf
pillow
Rasterize a file and save it to the output (input and output path can be the same):
python3 pdflat.py --dpi 300 --quality 90 --output out.pdf input.pdfRasterize two files at the same time. Saved to [name]_compressed.pdf in the same folder as the original:
python3 pdflat.py --dpi 300 --quality 90 a.pdf b.pdfRasterize all PDFs in the folder in and save them in the folder out:
python3 pdflat.py --dpi 300 --quality 90 --output out/ in/*.pdfRasterize a file and ensure all pages are the same width as A4 paper (preserving the original aspect ratio). Override the input PDFs in place:
python3 pdflat.py --dpi 300 --quality 90 --width a4 --inplace input.pdfEnsure all pages are exactly A4-landscape paper size and override the input PDFs in place:
python3 pdflat.py --dpi 300 --quality 90 --width a4~ --height a4~ --inplace in/*.pdf--dpicontrols rasterization resolution.--qualitycontrols JPEG quality (0-100, higher = better quality, larger files).--widthand--heightset the output canvas size. Options are:- Standard paper sizes:
a1-a10,letter,legal,tabloid(portrait by default, for landscape add~as a suffix, e.g.a4~). - Custom length with unit (e.g.
210mm,8.5in,595px).
- Standard paper sizes:
--outputsets the output PDF path (or a directory).--inplaceoverwrites the input PDF- Cannot be used in combination with
--output.
- Cannot be used in combination with
The script can be run as a flake using the nix package manager.
(Be careful when running untrusted code from the internet!)
nix run github:LeoSchae/pdflat -- --dpi [...]