Skip to content

Instantly share code, notes, and snippets.

@phrz
Last active December 5, 2024 21:40
Show Gist options
  • Save phrz/7993bd2b0e3a6242509f52a45918a252 to your computer and use it in GitHub Desktop.
Save phrz/7993bd2b0e3a6242509f52a45918a252 to your computer and use it in GitHub Desktop.
Compress PDFs for electronic faxing, where the attachment size limits are very small, by making them monochrome, pixelated, 200 DPI. Parallelized.
#!/bin/zsh
# brew install qpdf parallel imagemagick
set -e
tmpdir=$(mktemp -d)
trap 'rm -rf -- "$tmpdir"' EXIT
echo "Splitting PDF"
qpdf --warning-exit-0 --no-warn --split-pages=5 "$1" "$tmpdir/%d-out.pdf" # five page chunks
echo "Converting pages"
ls "$tmpdir"/*out.pdf | parallel --progress -j0 magick -monochrome -density 200 {} -compress Group4 {.}-comp.pdf
echo "Combining PDF"
qpdf --empty --pages "$tmpdir"/*-out-comp.pdf -- "${1%.*}_faxcompress.${1##*.}"
rm -rf -- "$tmpdir"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment