Merge multiple PDFs using Ghostscript

Merging multiple PDF is not a common task, but it is good to know how to do it once the requirement arises. Specially if you want to merge documents from within a language like PHP. Although there are a few PHP classes that do the job, many throw out errors due to incompatibility between different versions of PDF and the compression used thereof. The last good (and free) solution I found was that of using Ghostscript to merge the PDF’s, and calling the command from PHP.

A simple Ghostscipt command to merge two PDFs in a single file is shown below.

gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=combine.pdf -dBATCH 1.pdf 2.pdf
pdf