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:
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
World.pdf into one that is named as
pdftk Hello.pdf World.pdf cat output HelloWorld.pdf
Similarly, if you want to merge every PDF file present in the directory, you can use
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
Debian.pdf into one file named
pdfunite Ubuntu.pdf Debian.pdf Linux.pdf
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!
-sDEVICEyou can specify the function or the output device. I have used
pdfwritefunction 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
Moon.pdf into one named
gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=Twilight.pdf -dBATCH Earth.pdf Moon.pdf
Use ImageMagick to merge PDF files in Ubuntu
ImageMagick comes pre-installed in Ubuntu and it 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 file1.pdf file2.pdf file3.pdf outputfile.pdf
For example, I have merged two files named
BSD.pdf into one file named
convert BSD.pdf Linux.pdf Freedom.pdf
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'
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
<policy domain="coder" rights="read|write" pattern="PDF" />
Now, save changes and exit from the nano text editor and ImageMagick should work as intended:
2. convert-im6.q16: cache resources exhausted
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
<policy domain="resource" name="disk" value="4GiB"/>
Save the changes and now, you can merge PDFs using ImageMagick without any issues.
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.