Merging Multiple PDFs into one file on Ubuntu (linux)

  • Posted on: 25 March 2009
  • By: shivanj

Today I wanted to merge two PDF files into one final PDF document. On Ubuntu, this was harder than I thought. I have done this on Windows using Adobe and it was pretty simple. I decided that I will make this post so that I can always find the command when I am looking for it.

This is a command-line approach. I did not find a nice GUI approach, but if there is anyone out there who has found one, please feel free to leave a comment.

[adsense:468x60:0686225696]
Here are the steps to install the necessary packages:

sudo apt-get install gs

sudo apt-get install pdftk

Now here is the command that does the actual merging:

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

You can merge more PDFs by simply adding on a third pdf etc. It merges them in the order you put it. So second.pdf will merge after the last page of first.pdf

output.pdf will contain the final PDF that you want. You should probably have all the PDFs in one directory and run the command from there although this is not necessary if you know what you are doing and you know your directory structure.

Go forth and merge!

Comments

I don't think you need GS to do it. I installed pdftk a while back to do the same thing, though I don't remember if it pulled in GS as a dependency. Take a look at the 'cat' operation in the pdftk man page.

If there's a way to do it with stock GS, that would be very useful, as pdftk depends on gcj, which can be a pain if you're building from source.

--Andrew

Actually GS is not a dependency of pdftk. I looked at the 'cat' operation but could not quite figure out how to use that command. Can you give a sample command to use it?

Try couturier, it allows you to merge PDF or/and images into one single PDF document.

http://sites.google.com/site/couturierapp