Created
August 7, 2022 14:44
-
-
Save fd0/64d207aae1399fdca333e6365dc90138 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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