From 04475076070bce27b971523effdb03438d55e1c6 Mon Sep 17 00:00:00 2001 From: Alexander Kobjolke Date: Sun, 3 Apr 2022 00:02:03 +0200 Subject: [PATCH] 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. --- home/local/bin/merge-pdf | 70 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/home/local/bin/merge-pdf b/home/local/bin/merge-pdf index c5b4e62..a2d0de5 100755 --- a/home/local/bin/merge-pdf +++ b/home/local/bin/merge-pdf @@ -1,8 +1,74 @@ #! /usr/bin/env nix-shell -#! nix-shell -i bash -p ghostscript +#! nix-shell -i bash -p ghostscript -p coreutils # # Local Variables: # mode: sh # End: +# -gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=- -dBATCH "$@" +workdir= +keep_workdir=false +output=- + +function usage { + cat <&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