1. Overview

There are several occasions where we wanted to merge PDF files, so as to organize them to reduce clutter or to share it with someone else. For that, Linux offers different tools. Below are some of the most commonly used ones:

Although these tools have many features, we’ll focus on their file merging capability for this tutorial. Let’s take a look at each of them one by one.

2. pdfunite

This is a simple and straightforward tool to merge several PDF files.

Let’s assume we’ve two PDF files to merge:

$ ls -lh
total 64K
-rw-r--r-- 1 bluelake bluelake 29K Jan 10 12:44 simple1.pdf
-rw-r--r-- 1 bluelake bluelake 29K Jan 10 12:44 simple2.pdf

We can run the command below to merge these files:

$ pdfunite simple1.pdf simple2.pdf simple.pdf
$ ls -lh
total 116K
-rw-r--r-- 1 bluelake bluelake 29K Jan 10 12:44 simple1.pdf
-rw-r--r-- 1 bluelake bluelake 29K Jan 10 12:44 simple2.pdf
-rw-r--r-- 1 bluelake bluelake 49K Jan 10 12:48 simple.pdf

As we can see, the usage is pretty straightforward. In order to create a merged PDF file, we supplied the input PDF filenames and an output filename to which the resultant PDF is written. Here, the simple1.pdf and simple2.pdf is merged to give simple.pdf.

One thing to note here is, we need to ensure we’ve given an output filename at the end, otherwise, it’ll overwrite the last filename mentioned in the list.

3. gs

This tool can work with PDF files and postscript files, and generate an output to be sent to a printer or a file. We’ll use the pdfwrite device feature of this tool to merge PDF files.

This command supports -sDEVICE option using which we can send the output to PDF files.

Let’s check how we can do this by an example:

$ gs -q -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=merged.pdf simple1.pdf simple2.pdf simple3.pdf
$ ls -l
-rw-r--r-- 1 bluelake bluelake  84948 Jan 10 13:27 merged.pdf
-rw-r--r-- 1 bluelake bluelake  29663 Jan 10 12:44 simple1.pdf
-rw-r--r-- 1 bluelake bluelake  29663 Jan 10 13:00 simple2.pdf
-rw-r--r-- 1 bluelake bluelake  60748 Jan 10 12:59 simple3.pdf

From the above results, we can see that the command has generated a merged.pdf file. And it’ll have all the pages from simple*.pdf files.

Lets check the different parameters used:

  • -q: quiet mode
  • -dBATCH: instructs to exit after processing all files
  • -dNOPAUSE: disables prompting after processing each file
  • -sDEVICE: selects the output device for saving to a pdf file
  • -sOutputFile: pass the name for the final output file

This tool is capable of writing the output in different formats using the supported output devices. To get the list of devices, we can run gs -h command. From this list, we can supply those to the -sDEVICE option to generate the respective formatted output.

4. convert

Normally, we use the convert tool to edit or make changes to an image such as, resize, crop, blur, and other various image processing functions. It has a myriad of options to do these operations on different image file formats. However, we can use it to merge pdf files as well.

Let’s take a look at its usage:

$ convert -density 300x300 -quality 100 simple1.pdf simple2.pdf simple3.pdf out.pdf
$ ls -lh
-rw-r--r-- 1 bluelake bluelake 568K Jan 10 19:15 out.pdf
-rw-r--r-- 1 bluelake bluelake  29K Jan 10 12:44 simple1.pdf
-rw-r--r-- 1 bluelake bluelake  29K Jan 10 13:00 simple2.pdf
-rw-r--r-- 1 bluelake bluelake  60K Jan 10 12:59 simple3.pdf

In the above command, we gave three simple*.pdf files. And for that, we got an out.pdf file as output. The out.pdf will have all the contents merged.

The options used in the command are:

  • -density: horizontal and vertical density of the image
  • -quality: required compression value

One problem with this is, the final PDF doesn’t keep the hyperlinks in the original PDF files.

5. qpdf

qpdf is a tool mainly used to restructure a PDF file. Whenever we need to take a few pages from different PDF files, arrange them in a particular order and merge them into a single PDF fileqpdf is the tool for that.

But for now, let’s check how we can use qpdf to merge PDF files:

$ qpdf --empty --pages simple1.pdf simple2.pdf simple3.pdf -- new.pdf
$ ls -l
-rw-r--r-- 1 bluelake bluelake  106891 Jan 11 11:04 new.pdf
-rw-r--r-- 1 bluelake bluelake   29663 Jan 10 12:44 simple1.pdf
-rw-r--r-- 1 bluelake bluelake   29663 Jan 10 13:00 simple2.pdf
-rw-r--r-- 1 bluelake bluelake   60748 Jan 10 12:59 simple3.pdf

We can see that the command created a new.pdf file. It will be having all the contents from simple*.pdf files in the order specified at the command line.

Let’s look at the different arguments used:

  • –empty: to start with an empty document
  • –pages: specify the documents

6. Conclusion

In this tutorial, we had a look at different tools for merging PDF files. As mentioned earlier, these tools have many features which we didn’t explore in this tutorial.

Comments are closed on this article!