Google
 

Thursday, November 27, 2008

Merging PDF files in Linux

Sometimes you might have to merge two or more pdf files into one single pdf document. While there are commercial softwares like Adobe Acrobat or something similar to do the job, it's much simpler in Linux using commandline. There are several softwares for the job and most of them require simple one line command!

The following commands merge first.pdf and second.pdf to merge.pdf. You can also merge more than two files using similar command.

Using Ghostscript
Ghostscript is a PostScript and PDF language interpreter and previewer. You can do quite handful tasks related to ps and pdf files with Ghostscript; merging happens to be one of them.

gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=merged.pdf -dBATCH first.pdf second.pdf

Using ImageMagick
ImageMagick is basically an image manipulation software but it also handles pdf files.

convert first.pdf second.pdf merged.pdf

Using PDFTk
If PDF is electronic paper, then pdftk is an electronic staple-remover, hole-punch, binder, secret-decoder-ring, and X-Ray-glasses. Pdftk is a command-line tool for doing everyday things with PDF documents.

pdftk first.pdf second.pdf cat output merged.pdf

Using pdf2ps and ps2pdf
pdf2ps and ps2pdf comes with Ghostscript. The former converts pdf file to ps and latter the reverse. We can use them to first convert pdf files to ps format, merge all ps files and then converting ps file back to pdf.

pdf2ps first.pdf first.ps
pdf2ps second.pdf second.ps
cat first.pdf second.ps > merged.ps
ps2pdf merged.ps merged.pdf


If you are looking for some GUI version, there's a Nautilus script which acts as frontend to PDFTk. If you are on KDE, Kommander script is also available (also depends on PDFTk)


NOTE: To install Ghostscript (already installed in most distros by default), ImageMagick or PDFTk in Linux,

sudo apt-get install ghostscript
sudo apt-get install imagemagick
sudo apt-get install pdftk

No comments:

Post a Comment