LeoSchae/pdflat

Flatten and compress pdf

★ 0Forks 0PythonGitHub ↗Compare

README

PDFlat (flatten and compress pdfs)

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).

Recommended use

python3 pdflat.py --dpi [DPI] --width [A4/letter/XXcm/XXin] --output [./out-dir/] ./in-dir/*.pdf

Defining 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.

Usage

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.pdf

One 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.

Requirements

The programs is a single standalone python script. There are two dependencies:

pymupdf
pillow

Examples

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.pdf

Rasterize 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.pdf

Rasterize all PDFs in the folder in and save them in the folder out:

python3 pdflat.py --dpi 300 --quality 90 --output out/ in/*.pdf

Rasterize 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.pdf

Ensure 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

Notes

  • --dpi controls rasterization resolution.
  • --quality controls JPEG quality (0-100, higher = better quality, larger files).
  • --width and --height set 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).
  • --output sets the output PDF path (or a directory).
  • --inplace overwrites the input PDF
    • Cannot be used in combination with --output.

Running as a nix flake

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 [...]

Contributors

LeoSchae

Issues