Friday, April 1, 2011

How to Merge Multiple PDF files into single PDF file on Ubuntu

Download: Fast, Fun, Awesome


Multiple PDF files can be merged into single PDF using two different ways: ghostscript or pdftk

A. Use Ghostscript to merge PDF files
Steps:
1. Install two pacakeges GhostScript and PDFtk tools.
 $ sudo apt-get install gs pdftk

2. Use following command to combine multiple files into single PDF file. The output file name is "singleCombinedPdfFile.pdf". The input file names are all files in the current directory, bcoz we used "*.pdf".

$ gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=singleCombinedPdfFile.pdf -dBATCH *.pdf

If you want to join PDF files in specific order then you can also use file names.
$ gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=singleCombinedPdfFile.pdf -dBATCH 1.pdf 2.pdf 3.pdf

B. Use pdftk (PDF toolkit) to merge multiple PDF files into Single PDF file
1.  To merge PDF files by using names of the source PDF files:
$ pdftk one.pdf  two.pdf  three.pdf  cat  output  123-combined.pdf

2. To merge PDF files using wildcard when number of files are large and its not feasible to input filenames of all files:
pdftk *.pdf cat output combined.pdf


3. Select specific pages from Multiple PDFs and create new PDF document:
$ pdftk A=one.pdf B=two.pdf cat A1-7 B1-5 A8 output combined.pdf