Sometimes, you may have 2 or more PDF files that you need to combine or merge into one so that you can have everything in one place. This will minimize time wasted trying to look for your PDF files which may be in different folders and other directories.
Recently I have tested a few tools to merge pdf files on Linux that I will introduce to you today. I have tested all the tools on the Ubuntu 18.04 version.
1) Using pdfunite
Pdfunite is a handy tool that can be used for merging to or more PDF documents. It is part of poppler-utils package, so when you install poppler utils, you end up with pdfunite.
To install pdfunite, we’ll install the poppler-utils package as shown
apt install poppler-utils
Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: fontconfig-config fonts-dejavu-core libcairo2 libfontconfig1 liblcms2-2 libpixman-1-0 libpoppler58 libxcb-render0 libxcb-shm0 libxrender1 Suggested packages: liblcms2-utils poppler-data The following NEW packages will be installed: fontconfig-config fonts-dejavu-core libcairo2 libfontconfig1 liblcms2-2 libpixman-1-0 libpoppler58 libxcb-render0 libxcb-shm0 libxrender1 poppler-utils
Next. update the system
In the downloads folder, I have 2 PDF documents
We are going to merge them using pdfunite command.
To merge the PDF documents, the syntax will be as shown
pdfunite file1.pdf file2.pdf merged_output.pdf
Using our files as shown earlier, the command will be
pdfunite linux_for_beginners.pdf linux_for_beginners_2.pdf merged_output.pdf
You will get the following warning in the output, but don’t worry. The files have already been merged.
As you can see, the output file merged_output.pdf has been created
Note: Files to be merged need to be in the same directory where pdfunife is executed
2) Using pdftk tool
Pdftk is yet another tool that can be used to merge PDF documents in Ubuntu 18.04. To install pdftk on terminal in Ubuntu 18.04, first add the pdftk repository
More info: https://launchpad.net/~malteworld/+archive/ubuntu/ppa Press [ENTER] to continue or Ctrl-c to cancel adding it. Hit:1 http://repo.mysql.com/apt/ubuntu bionic InRelease Get:2 http://security.ubuntu.com/ubuntu bionic-security InRelease [83.2 kB] Hit:3 http://ke.archive.ubuntu.com/ubuntu bionic InRelease Get:4 http://ke.archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB] Hit:5 http://ppa.launchpad.net/dawidd0811/neofetch/ubuntu bionic InRelease Hit:6 http://linux.teamviewer.com/deb stable InRelease Get:7 http://ppa.launchpad.net/malteworld/ppa/ubuntu bionic InRelease [15.3 kB] Hit:8 http://linux.teamviewer.com/deb preview InRelease Get:9 http://ke.archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB] Hit:10 http://ppa.launchpad.net/webupd8team/java/ubuntu bionic InRelease
Next, update your Ubuntu System
After successfully updating your system, install the pdftk tool as shown below
apt install pdftk
After the installation of pdftk is complete, you can now invoke the pdftk command to merge files. The syntax is as shown
pdftk file1.pdf file2.pdf cat output merged_output.pdf
using our earlier files, the command will be as shown
pdftk Linux_for_beginners.pdf Linux_for_beginners_2.pdf cat output merged_output.pdf
3) Using Convert tool
To use the convert command, first install it on terminal using the command below
apt install imagemagick
The following packages will be upgraded: imagemagick 1 upgraded, 0 newly installed, 0 to remove and 494 not upgraded. Need to get 14.2 kB of archives. After this operation, 0 B of additional disk space will be used. Get:1 http://ke.archive.ubuntu.com/ubuntu bionic-updates/main amd64 imagemagick amd64 8:126.96.36.199+dfsg-16ubuntu6.3 [14.2 kB] Fetched 14.2 kB in 2s (8,300 B/s) (Reading database ... 142831 files and directories currently installed.) Preparing to unpack .../imagemagick_8%3a188.8.131.52+dfsg-16ubuntu6.3_amd64.deb ... Unpacking imagemagick (8:184.108.40.206+dfsg-16ubuntu6.3) over (8:220.127.116.11+dfsg-16ubuntu5) ... Setting up imagemagick (8:18.104.22.168+dfsg-16ubuntu6.3) ...
Upon successful installation, you can now merge your documents.
The syntax for merging 2 PDF files is as shown
convert file1.pdf file2.pdf merged_output.pdf
Using our files , the command will be
convert Linux_for_beginners.pdf Linux_for_beginners_2.pdf output.pdf
To Merge specific PDF pages into a single PDF file, you'll need to pass our files with specific page numbers in square brackets to the convert command. For example to merge the 5th page from the first document with 7th and 10th pages in the second document, the syntax will be
convert file1.pdf file2.pdf[6,9] merged_pages.pdf
Using our files , the command will be
convert Linux_for_beginners.pdf Linux_for_beginners_2.pdf[6,9] merged_pages.pdf
And Yes, it's very possible to join page ranges.
For Example, you can join the first 5 pages from first file with first 10 pages from the other file. The syntax for this case will be
convert file1.pdf[0-4] file2.pdf[0-9] merged_page_ranges.pdf
Using our files , the command will be
convert Linux_for_beginners.pdf[0-4] Linux_for_beginners_2.pdf[0,9] merged_page_ranges.pdf
4) Using Ghostscript
We can also use ghostscript (gs) to merge PDf documents in Ubuntu 18.04
apt install ghostscript
Example of using ghostscript to merge 2 PDF documents
gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=combine.pdf -dBATCH file1.pdf file2.pdf
In the above example, let's expound further on the attributes
-sOUTPUTFILE denotes the output file -dBATCH denotes the pdf documents to be merged
Using our documents earlier, the command will be
gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=combine.pdf -dBATCH Linux_for_beginners.pdf Linux_for_beginners_2.pdf
GPL Ghostscript 9.22 (2017-10-04) Copyright (C) 2017 Artifex Software, Inc. All rights reserved. This software comes with NO WARRANTY: see the file PUBLIC for details. Processing pages 1 through 29. Page 1 Page 2 Loading NimbusSans-Regular font from /usr/share/ghostscript/9.22/Resource/Font/NimbusSans-Regular... 5056280 3684544 3235592 1819445 3 done. Page 3 Page 4 Page 5 Page 6 Page 7 Page 8
5) Using pdfsam
Pdfsam is a simple, intuitive and easy to use GUI tool that is used to merge, split, rotate, edit and sign PDF documents. In this example, we are going to use the tool to merge PDF documents in Ubuntu 18.04. But first, let's install it
apt install openjdk-8-jre libopenjfx-jni libopenjfx-java openjfx
After successful installation, define the JAVA_HOME variable in /etc/environment path using your favorite editor.
Append the following line.
Save and Exit
reload the file
Next, download the Debian package file from pdfsam's official site
Once downloaded, install the deb file using the dpkg command as shown.
dpkg -i pdfsam_3.3.6-1_all.deb
(Reading database ... 146399 files and directories currently installed.) Preparing to unpack pdfsam_3.3.6-1_all.deb ... Unpacking pdfsam-basic (3.3.6-1) over (3.3.6-1) ... Setting up pdfsam-basic (3.3.6-1) ... Processing triggers for gnome-menus (3.13.3-9) ... Processing triggers for desktop-file-utils (0.23-1) ... Processing triggers for mime-support (3.60) ...
To start pdfsam, run
To merge documents, click on 'Merge' and drag and drop the files to be merged in the section provided.
Feel free to specify other attributes in the 'Merge Settings' section and once done click on the 'Run' icon located at the bottom. Once done, you'll hear a 'ding' sound notifying you that the merging process is complete.
As seen in the above examples, merging PDF files on Linux is very easy and straight forward on the terminal. Give it a try and let us know how it went. Your Feedback is much welcome in our comment section. Thank you and feel free to share this tutorial on your social handles.