How to merge PDF files

A book that could be bound or created from separate pdf documents

I had several PDF files that I wanted to put together into a booklet. In other words, I wanted a single PDF file that I could send to different people and devices and use it as a collection instead of each one individually. So I was looking for a way to merge PDF files and do it without having to buy software or pay for somebody to do it.

A quick search for how to merge PDF files resulted in several results. I found this article where 5 ways are described in detail for Linux. I’m running Linux (Ubuntu) on the computer I wanted to do this, although this also works on my MacBook (Mac OS). On Windows, you should be able to use this method but you will probably need the Window Subsystem for Linux or Cygwin, which I used to run many years ago when I used Windows on a day-to-day basis (not anymore).

The only option for me that didn’t require me to install something I didn’t already have, was to use Ghostcript (I don’t recall installing it, so it might come on your Linux distro already — in any case, installing it should be trivial. I installed it on Mac OS with brew install gs). It was listed in the above article as option #4, so I wrote this article to save you some time!

So this is how you do it. Let’s say you have two files: file1.pdf and file2.pdf. You simply run this command on your command line and you’re set!:

gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=book.pdf -dBATCH file1.pdf file2.pdf

Where the output file is book.pdf.

If you want to merge more than two files, you simply provide the full list, e.g. add a file3.pdf etc. at the end of the example above. You could also merge two files and then use the output file as an input file that you want to use to merge it to other files, which is useful if you don’t have all files at hand currently or if you’re creating a collection of documents that are created over time (like the book of sheet music that keep adding pages to).

If you agree this is a nice short article, perhaps you have time for more reading here.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rogelio Flores Zubillaga

Rogelio Flores Zubillaga


I create web and mobile apps for a living. Play soccer for fun. Wish it were the other way around. I write about software, life, and the universe.