nixos-config/home/local/bin/merge-pdf
Alexander Kobjolke 0447507607 merge-pdf: Convert to PostScript before combining
Some PDF files may contain empty BBox elements which confuses the pdf
parser so that those files get ignored. This commit circumvents this
problem by first converting all files to PostScript.
2022-04-03 00:02:03 +02:00

74 lines
1.2 KiB
Text
Executable file

#! /usr/bin/env nix-shell
#! nix-shell -i bash -p ghostscript -p coreutils
#
# Local Variables:
# mode: sh
# End:
#
workdir=
keep_workdir=false
output=-
function usage {
cat <<EOF
usage: merge-pdf [options] files...
options:
-o FILE write result to FILE (default: -)
EOF
}
function cleanup {
if [ x"${keep_workdir}" != x"true" ]; then
if [ -d "${workdir}" ]; then
rm -rf "${workdir}"
fi
fi
}
trap "cleanup" EXIT
while getopts ":ho:k" o; do
case "$o" in
h)
usage
exit 0
;;
o)
output="$OPTARG"
;;
k)
keep_workdir=true
;;
*)
usage
exit 1
;;
esac
done
shift $((OPTIND - 1))
workdir=$(mktemp -d)
echo >&2 "[INFO] Putting temporary files into ${workdir}"
files=()
for f; do
fname=$(basename "$f" .pdf)
of="${workdir}/${fname}.ps"
echo >&2 "[PS ] \"${f}\" -> \"${of}\""
pdf2ps "$f" "${of}" >&/dev/null
files+=("$of")
done
echo >&2 "[PDF ] combining ${#files[@]} file(s) into ${output}"
gs -dNOPAUSE -sDEVICE=pdfwrite \
-dPDFSETTINGS=/prepress \
-sPAPERSIZE=a4 \
-sFIXEDMEDIA \
-sOUTPUTFILE="${output}" \
-dBATCH \
"${files[@]}" >&/dev/null