Monday, 5 March 2007

Merge PDF files on Linux

Sometimes you need to merge PDF files made by someone else in a single file together. Your Windows or Mac user fellows will do probably by using Adobe Acrobat or the like. On Linux you don't need the Adobe software (although you can have it if you like). You can do it by the command line -it's Linux after all, isn't it?


The first method is by using convert , directly from the ImageMagick toolkit. You probably already have this one installed on your favourite Linux distribution; if not, almost every known Linux distribution has a package for it. Even if you don't need to edit PDF files you should have it, it is a wonderful swiss army knife for command line image processing. The syntax for merging PDF files is simple:


convert file1.pdf file2.pdf file3.pdf out.pdf


(that is, the last file name is the output file name). This usually works quite correctly, but 1)it is slow 2)I found sometimes has issues with image resolution. So I looked for another solution, and I found pdftk. The name stands for "PDF ToolKit", and really it is. It is a free (open source under the GNU GPL), wonderful command line utility that with a bit of magic allows you to manage PDF files from the command line. It works on Linux, Windows and Mac. pdftk can merge PDF documents, split PDF pages into a new document, rotate PDF Pages or Documents, decrypt and encrypt, fill PDF forms, apply a background watermark or a foreground stamp, burst a PDF Document into single pages... whatever.


The syntax for merging with pdftk is almost as simple:


pdftk file1.pdf file2.pdf file3.pdf cat output out.pdf

You just need to add the magic "cat output" between the input pdfs and the output file name. In comparison with convert, it is truly fast and in my experience gives better results. And it may come handy for when I have to work with PDF files in other ways. Also, KDE users may find nice PDF Concat, a Kommander script that acts as a simple pdftk frontend to merge PDFs.

16 comments:

Henrique said...

Thanks for this post. A very simple command yet so useful :)

Unknown said...

If you don't have a posibility to install imagemagic (convert) or pdftk you can simply use what is by default on most POSIX OSs.

pdf2ps file1.pdf file1.ps
pdf2ps file2.pdf file2.ps
cat file1.ps file2.ps > file12.ps
ps2pdf file12.ps file12.pdf

ricardo85x said...

i need a way to keep the bookmarks.

any tips

Unknown said...

Great tip!!! Thank you very much for this help! Exactly what I needed.

Justin Gross said...

Knew about ps conversion tools but did not know this software existed. Fantastic. So easy to use too.

Justin Gross said...

Knew about ps conversion tools but did not know this software existed. Fantastic. So easy to use too.

tinge said...

Thanks so much for the post, very helpful, clear explanation for an enormously useful command!

btw - you're so right about ImageMagick's resolution problems, but pdftk cleared that right up!

Ammar said...

Thanks a lot for this post, I downloaded the pdftek.exe and used the command line to merge many pdf files, it's a snap!

Anonymous said...

Thank you very much!
pdftk worked perfectly where convert didn't do a good job! ;-)

Unknown said...

Thank you very much.
But I think I omitted 'cat' where 'cat output' should be and it worked well, as far as I remember.

Fábio Santos de Lima said...

Obrigado pela dica. Eu já havia feito um script para converter todas as minhas fotos com o convert mas não tinha percebido que ele fazia colagem de PDF também! Ta quebrando um galho aqui. Tenho que escanear um livro enorme e tou sem tempo. Com essa dica eu vou poder dividir o trabalho em duas sessões e depois juntar tudo num só arquivo.

Praveen said...

yeah... pdftk is really working good.
Thanks for this post...

blogterix said...

cool & helpful

thanks

Unknown said...

Hi. This service here allows you to easily edit, upload or download and how to fill PDF forms.
http://goo.gl/s6z2Up

You can fill out PDF form, save it, fax it, and email it.

Unknown said...

A new tool constructed for merging PDF documents doesn’t lag behind. You may combine pdfs online without extra efforts. In the best traditions of our platform, the procedure is self-explanatory and easy in usage. Our user-friendly interface attracts your attention to main moments and step-by-step leads you to the successful result.merge, altomerge, merging PDF documents, combine pdfs online

If you need any information just click here merge pdf

morris said...

it was a wonderful chance to visit this kind of site and I am happy to know. thank you so much for giving us a chance to have this opportunity.. combinepdf.net