Skip to content

Instantly share code, notes, and snippets.

@pranavgade20
Created November 13, 2022 09:19
Show Gist options
  • Save pranavgade20/4e0839509646ba6c8ad3b2c5f01727c6 to your computer and use it in GitHub Desktop.
Save pranavgade20/4e0839509646ba6c8ad3b2c5f01727c6 to your computer and use it in GitHub Desktop.
A script to merge pdf files and add chapter annottions using ghostscript
#!/bin/bash
# usage: merge-pdf out.pdf `ls -v *.pdf`
merge-pdf() {
cumulative=1
for f in "${@:2}"
do
cnt=$(gs -q -dNODISPLAY -dNOSAFER -c "($f) (r) file runpdfbegin pdfpagecount = quit")
echo "[/Page $cumulative /Title ($f) /OUT pdfmark"
cumulative=$((cnt+cumulative))
done > pdfmark.txt
gs -q -dBATCH -dNOPAUSE -dNOSAFER -sDEVICE=pdfwrite -sOutputFile="$1" -dPDFSETTINGS=/prepress pdfmark.txt "${@:2}"
rm pdfmark.txt # cleanup
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment