Haui's bytes

news, diary, journal, whatever

Concatenate PDF files

There are several ways of concatenating multiple PDF files into one single PDF file. Unfortunately, most of these ways just don’t work at all, or the resulting PDF is not what you’d expect it to be. However, there’s one convenient way using ghostscript that always worked pretty well for me:

gs -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=output.pdf *.pdf

Since this is a pretty complex command, we’ll hide its complexity for future use: Put the code shown below into a newly created file /usr/local/bin/mkpdf


[[ $# -lt 2 ]] && { echo "Usage: $0 output.pdf <inputfiles.pdf>" ; exit 1; }

gs -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile="$OUTPUT" "$@"

After setting up the appropriate file permissions (chmod 755 /usr/local/bin/mkpdf), mkpdf output.pdf <inputfiles.pdf> does exactly the same as the command mentioned above.