Skip to content

Instantly share code, notes, and snippets.

@gsportelli
Created January 16, 2024 09:29
Show Gist options
  • Save gsportelli/e6ccd247d13bfdd06e68e61f9a094ce2 to your computer and use it in GitHub Desktop.
Save gsportelli/e6ccd247d13bfdd06e68e61f9a094ce2 to your computer and use it in GitHub Desktop.
pdfcompare
#!/usr/bin/env bash
PDF_1="$1"
PDF_2="$2"
OUT_FILE="$3"
OUT_TMPDIR=$(mktemp -d)
cleanup() {
rm -rf "$OUT_TMPDIR"
}
trap 'cleanup' EXIT
pdftk "$PDF_1" burst output "$OUT_TMPDIR/file_1-page_%03d.pdf" || exit 1
pdftk "$PDF_2" burst output "$OUT_TMPDIR/file_2-page_%03d.pdf" || exit 1
for f1 in $OUT_TMPDIR/file_1-page_*.pdf; do
f2="${f1/file_1-page_/file_2-page_}"
f_diff="${f1/file_1-page_/diff-page_}"
f_out="${f1/file_1-page_/out-page_}"
compare "$f1" "$f2" -quiet -compose src "$f_diff" 2> /dev/null
pdftk "$f_diff" background "$f1" output "$f_out" || continue
done
pdftk "$OUT_TMPDIR"/out-page_*.pdf cat output "$OUT_FILE" || exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment