Skip to content

Instantly share code, notes, and snippets.

@fd0
Created August 7, 2022 14:44
Show Gist options
  • Save fd0/64d207aae1399fdca333e6365dc90138 to your computer and use it in GitHub Desktop.
Save fd0/64d207aae1399fdca333e6365dc90138 to your computer and use it in GitHub Desktop.
#!/bin/bash
shopt -s dotglob
# use bash strict mode
set -euo pipefail
IFS=$'\n\t'
if [[ "$#" != "2" ]]; then
echo "usage: $0 input.pdf output.pdf"
exit 2
fi
input="$(readlink -f "$1")"
output="$(readlink -f "$2")"
workdir=$(mktemp -d --tmpdir='' compress-pdf-XXXXXXXXX)
cd "${workdir}"
"gs" -o text.pdf -sDEVICE=pdfwrite -dNOPAUSE -dQUIET -dBATCH -dFILTERIMAGE "$input"
pdfimages -j "$input" .
for i in *.jpg; do
convert -alpha off -auto-threshold otsu -monochrome -quality 100 "$i" "$i.png"
done
img2pdf --output images.pdf *.png
pdftk text.pdf multistamp images.pdf output "$output"
echo "output written to $output"
rm -rf "$workdir"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment