Concatenating PDFs from CLI
The widely known and easiest way to combine PDF documents from the command line is to use pdftk:
$ pdftk file1.pdf [file2.pdf ...] cat output dest.pdf
pdftk can do a lot more than just pdf concatenation. It can also split pages, rotate pages, apply watermarks and even repair broken PDFs. The downside is it removes outlines or bookmarks from the output document. So if you need the bookmarks intact you can’t use pdftk.
If you only need simple concatenation with bookmarks, use iText. iText is a powerful Java library for PDF generation and manipulation. Thankfully we don’t need to write our own Java code since hidden underneath is a simple PDF concatenation tool:
$ java com.lowagie.tools.ConcatPdf f1.pdf [f2.pdf ...] \ dest.pdf
Besides ConcatPdf, iText also has other tools such as SplitPdf and HandoutPdf.
iText in Ubuntu How To
To install iText in Ubuntu/Debian, from Synaptic install the package “libitext-java”. If you prefer the command line:
$ sudo apt-get install libitext-java
Before using iText like the above example you need to set up the CLASSPATH:
$ export CLASSPATH=$CLASSPATH:/usr/share/java/itext.jar
Or you can also set the CLASSPATH directly:
$ java -cp /usr/share/java/itext.jar \ com.lowagie.tools.ConcatPdf f1.pdf [f2.pdf ...] dest.pdf