Merge pdf files in Ubuntu
How To

How to Merge PDF Files in Ubuntu Terminal

Sagar Sharma
Sagar Sharma

Table of Contents

In Linux, you are bound to find multiple ways of doing one thing. And same goes for merging PDF files in the command line.

And in this guide, I will show you how you can use the following utilities to merge PDF files:

  • pdftk
  • poppler
  • ghostscript
  • ImageMagick

So let's start with the first one.

Use pdftk to merge PDF files in Ubuntu

The pdftk (pdf toolkit) is one of the best CLI utilities which is specifically designed to manipulate PDF files.

But it does not come pre-installed in Ubuntu and can be installed using the given command:

sudo apt install pdftk-java

Once done, you just have to follow the simple command structure to merge PDF files:

pdftk file1 file2 file3 cat output mergedfile.pdf

For example, here, I have merged two files named Hello.pdf and World.pdf into one that is named as HelloWorld.pdf:

pdftk Hello.pdf World.pdf cat output HelloWorld.pdf
use pdftk in ubuntu to merge pdf files

Similarly, if you want to merge every PDF file present in the directory, you can use *.pdf instead of naming each file manually and it will take care of the rest:

pdftk *.pdf cat output mergedfile.pdf

Use poppler to merge PDF files in Ubuntu

Let me be clear here. Popper is not a utility but a PDF rendering library itself. From the set of a bunch of utilities, you will be using pdfunite to merge pdf files.

But first, let's install Poppler in Ubuntu using the given command:

sudo apt install poppler-utils

Once done, you just have to use append file names and the name of the output file to the pdfunite utility as shown:

pdfunite file1 file2 file3 output.pdf

For reference, I will be merging two PDF files named Ubuntu.pdf and Debian.pdf into one file named Linux.pdf:

pdfunite Ubuntu.pdf Debian.pdf Linux.pdf
merge pdf files in ubuntu command line using poppler

Use ghostscript to merge PDF Files in Ubuntu

Ghostscript is a feature-rich PDF manipulation tool that provides various ways to edit your PDF files. And one of them is merging PDF files.

But it does not come pre-installed with Ubuntu and you'd need to follow the given command for installation:

sudo apt install ghostscript

Once done, you can use the given command syntax to merge the PDF file:

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

Look too complex? let me break down the used options first:

  • -dNOPAUSE: Will disable the prompt for asking you to continue on every page. I mean if you have 200 pages of PDF, you are not going to allow this utility to allow for every page manually!
  • -sDEVICE=pdfwrite: Using -sDEVICE you can specify the function or the output device. I have used pdfwrite function to manipulate PDF files.
  • -sOUTPUTFILE=outputfile.pdf: Here, you can name the output of the merged PDF file.
  • -dBATCH: Here, you will have to specify the name of the PDF files that need to be merged.

Now, let's jump to the implementation.

Here, I will be merging two PDF files named Earth.pdf and Moon.pdf into one named Twilight.pdf:

gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=Twilight.pdf -dBATCH Earth.pdf Moon.pdf
merge PDF files in Ubuntu command line using ghostscript

Use ImageMagick to merge PDF files in Ubuntu

You don't need to install ImageMagick on Ubuntu. It comes pre-installed and offers a hell lot of tools and merging PDF files is one of them.

To merge pdf files, you just have to append the PDF file names and the name of the output file with the convert command:

convert file1.pdf file2.pdf file3.pdf outputfile.pdf

For example, I have merged two files named Linux.pdf and BSD.pdf into one file named Freedom.pdf:

convert BSD.pdf Linux.pdf Freedom.pdf
merge PDF files in Ubuntu command line using ImageMagick

But I faced two issues while merging PDF files using ImageMagick. And I have addressed them below.

1. attempt to perform an operation not allowed by the security policy `PDF'

convert-im6.q16: attempt to perform an operation not allowed by the security policy `PDF'

By default, ImageMagick won't allow you to perform merging operations because of their PDF policy.

Which means you have to manually change those policies. To modify the policy, first, you'll have to open the policy.xml file using the given command:

sudo nano /etc/ImageMagick-6/policy.xml

And look for the following line:

<policy domain="coder" rights="none" pattern="PDF" />

Once found, change it the rights="none" to rights=read|write:

<policy domain="coder" rights="read|write" pattern="PDF" />
how to solve convert-im6.q16: attempt to perform an operation not allowed by the security policy `PDF'

Now, save changes and exit from the nano text editor and ImageMagick should work as intended:

2. convert-im6.q16: cache resources exhausted

convert-im6.q16: cache resources exhausted in imagemagick

You will face this issue, especially while dealing with huge PDF files as the default resource allocated to process files in ImageMagick may not be sufficient.

So in this case, you will have to change the default resource value (1GiB) to 4GiB or more. Let me show you how.

First, open the policy.xml file using the following:

sudo nano /etc/ImageMagick-6/policy.xml

Now, look for the given line:

<policy domain="resource" name="disk" value="1GiB"/>

Once found, you have to change the value="1Gib" to value="4Gib":

<policy domain="resource" name="disk" value="4GiB"/>
How to solve convert-im6.q16: cache resources exhausted in imagemagick

Save the changes and now, you can merge PDFs using ImageMagick without any issues.

Wrapping Up

In my opinion, you should go with the first option pdftk as everything you'd ever need to modify PDF files and at the same time, it is pretty easy to use.

I hope you will find this guide helpful and if you have any queries or suggestions, let me know in the comments.