Managing contract and other legal files get more straightforward once you know how to sign and edit PDFs digitally. However, it can quickly shift into a trivial difficulty once you comprehend how to merge PDF files in the Linux terminal or whatever free graphical application.
Many desktop and online selections display in search engine results for “how to merge two PDF files.” Some of the most traditional, well-known applications:
The problem is that none of those above are free and don’t provide a command-line interface (CLI). Here we will examine the alternatives:
- How to merge PDF files command line
- PDFtk (PDF Toolkit)
- PDFunite
- Convert ImageMagick Tool
- Ghostscript
Moreover, if you need to convert images to pdf, visit this website that entertains you with free tools for specific conversions.
How to merge PDF files command line
- First, open a Linux terminal.
- These commands are more manageable when you don’t have to type full file paths from your home directory repeatedly. Check if all PDF files are in the same directory.
- Also, before you continue and install a PDF merger tool below, verify if you already have a PDF tool installed. For this, you could type pdf and press the key Tab twice to examine what programs are shown. If at least one application shows up, verify with man command to learn what it does.
- For example, man pdftk.
If you have any PDF files inside a zip or tar.gz file, you need to extract them to the same folder.
ImageMagick: Merge PDF files
Let’s see how to use ImageMagick to merge PDF files on Linux. First, let’s install it.
Install ImageMagick on CentOS:
sudo yum install ImageMagick
Install ImageMagick on Debian / Ubuntu:
sudo apt install imagemagick
ImageMagick is originally for image optimization. ImageMagick incorporates a conversion tool that allows us to merge PDFs known as convert.
To use that command, you need to type the program command. After type, the PDF file’s name on the desired order that you wish to generate, the new PDF merged, and the file name for the final PDF.
Example:
convert file1.pdf file2.pdf mergedfile.pdf
To merge particular pages from PDFs files, define the pages beginning from 0. For example, to join pages 1-3 of a PDF file with a second file:
convert file1.pdf[0-2] fileB.pdf mergedresult.pdf
The order of files in the command defines the sequence in which images are merged in the merged.pdf.
If you see the following error while merging the PDF files:
convert: attempt to perform an operation not allowed by the security policy 'PDF' @ error/constitute.c/IsCoderAuthorized/408
Go to Solving the Security Policy Error section, where we have explained how to solve this issue.
We also can define the quality of the merged.pdf. To specify the quality rate, use the following command:
convert -density 300 fileA.pdf fileB.pdf fileC.pdf merged.pdf
-density specify the dpi that the PDF is rendered. Placing this to 300/600 will give pretty good quality for your PDF file.
Merge PDF and Image
You can also use image and pdf mutually in this command which delivers it even more powerful.
For example:
convert fileA.pdf imageA.jpg merged.pdf
If you face the following error while converting to PDF:
convert: attempt to perform an operation not allowed by the security policy 'PDF' @ error/constitute.c/IsCoderAuthorized/408
Go to the next section of this Post to resolve this error.
Solving the Security Policy Error
ImageMagick has some protection policies incapacitating some rights for security reasons.
You will need to update a config file to enable the action you need.
Go and Open /etc/ImageMagick-6/policy.xml with your preferred text editor, locate the line:
<policy domain="coder" rights="none" pattern="PDF" />
and replace “none” by “read|write”
Follow this instructions Step By Step to change:
Open the file in terminal and execute:
sudo nano /etc/ImageMagick-6/policy.xml
Locate and edit the line:
<policy domain="coder" rights="none" pattern="PDF" />
to :
<policy domain="coder" rights="read|write" pattern="PDF" />
References:
Ghostscript: Concatenate PDF Files
Also, besides the ImageMagick, we can using Ghostscript to combine pdf files.
Ghostscript is a clever CLI app for managing PDF, XPS, and PostScript files. We’ll practice -dNOPAUSE to disable show prompts at the end of each page. Also, the -sDEVICE property indicates the output device or function. You can view the complete list with the gs -h command.
gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=merged.pdf -dBATCH fileA.pdf fileB.pdf
Install Ghostscript on CentOS:
sudo yum install ghostscript
Install Ghostscript on Debian / Ubuntu:
sudo apt-get install ghostscript
Merge PDF using PDFunite
The most famous command, pdfunite, provided by Poppler, is natively available on Ubuntu-based Mint Cinnamon and Arch-based Manjaro. The command syntax below:
pdfunite file1.pdf file2.pdf mergedfile.pdf
Let’s check how to install the PDFunite in different distribution:
on CentOS:
sudo dnf install poppler-utils
on Debian based:
sudo apt install poppler-utils
Now you can start to merge PDF files.
PDFtk: Merge PDF Files
The PDF Tool kit has graphical and paid options, but you can do merge without any problem. To use the command line, you must add “cat output” among the original PDFs, and the filename for the new PDF file merged.
For example:
pdftk file1.pdf file2.pdf cat output mergedfile.pdf
Check the official documentation to install PDFtk on CentOS 6.
To install PDFtk on CentOS 7, check more about libgcj dependency issues and possible solutions from a docsplit GitHub thread.
Install PDFtk on Debian / Ubuntu:
sudo apt install pdftk-java
Reduce Size of PDF on Linux
Compress PDF on Linux is very easy because you can use the same tools that you are using to merge them, so you don’t need to install any extra application to reduce PDF size on Linux. However, when you create or join PDF files containing many images, the file size usually gets big.
You can use ImageMagick and Ghostscript to compress PDF on Linux.
Reduce PDF Size with Ghostscript
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/prepress -dNOPAUSE -dQUIET -dBATCH -sOutputFile=compressed_PDF_file.pdf input_PDF_file.pdf
The dPDFSETTINGS parameter is what defines the compression level and so the quality of your compressed PDF file.
The possibles values for dPDFSETTINGS are:
/prepress -> Higher quality (300 dpi) but bigger size
/ebook -> Medium quality (150 dpi) with moderate output file size
/screen -> Lower quality (72 dpi) but smallest possible output file size
The /prepress is the default option if you don’t specify any options.
Using ImageMagick: Reduce PDF File Size
convert -density 300 fileA.pdf fileB.pdf fileC.pdf merged.pdf
Like the Ghostscript, we can define the amount of dpi that we can use and then compress and decrease the PDF size.
The option -density specify the dpi that ImageMagick will use to create the PDF file.
How to merge PDF files using PDFsam
PDFsam is a simple, intuitive, and easy-to-use Graphical tool used to merge, split, rotate, edit and sign PDF documents. It’s available for Microsoft Windows, Mac OS too. Check here.
For example, we are going to utilize the tool to combine PDF documents in Ubuntu 20.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 the /etc/environment path using your favorite editor.
nano /etc/environment
Add the following line:
JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64/"
Save the file and exit.
Last, reload the file to make the changes take effect:
source /etc/environment
Next, download the Debian package file from pdfsam’s official site
wget https://github.com/torakiki/pdfsam/releases/download/v4.2.5/pdfsam_4.2.5-1_amd64.deb
After downloaded, install the tool to merge pdf:
dpkg -i pdfsam_4.2.5-1_amd64.deb
Sample Output
To start pdfsam, run
pdfsam
To merge documents, click on ‘Merge’ and drag and drop the files to be merged in the section provided.
Also, you can define other attributes in the ‘Merge Settings‘ region, and once done, click on the ‘Run‘ icon placed at the bottom.
Sejda SDK is actively developed and battle-tested as the PDF engine behind Sejda.com and PDFsam, regularly used by millions of users.
Java Library to merge PDF documents:
If you need to create an application that needs to merge pdf file, these are the best SDK available:
https://github.com/torakiki/sejda/releases/tag/v4.2.5
How to Merge Two PFD Online
You can use the online solution, and it’s a free service for documents up to 50 pages or 50 Mb and three tasks per hour. If you don’t want to install any application:
PDFescape Online PDF Editor is a free online PDF editor and form filler. It can be used in various web browsers.
Or you can use the Smallpdf solution on:
How to Merge PDF files in Ubuntu
All solution above from this article works for Ubuntu. However, if you prefer a graphical-based solution, you can find it on Ubuntu App Store, for example, the PDF Arranger.
PDF Arranger features:
- Merge multiple PDF documents
- Zoom in / out
- Remove pages from a PDF file.
- Crop PDF pages
- Reorganize pages in a PDF file
- Export chosen pages from a PDF
- Rotate pages in a PDF file
You can check the PDF Arranger on GitHub.
On Fedora from 30 and newer, you can install PDF Arranger from the repositories:
sudo dnf install pdfarranger
Combine PDF Files on Arch Linux
The pdfjam package on Arch Linux gives this functionality.
Merge multiple PDFs, sorted by the alpha that. The files are presented in the same folder:
pdfjoin *.pdf
Merge multiple images into a single PDF, sorted by alpha
pdfjoin *.jpg
If you would like to specify a specific order, list the PDFs files on your desire order as listed:
pdfjoin 1.pdf 2.pdf 3.pdf
Merge all files in a folder into a single PDF file:
pdfjoin *
Automate: How to Merge PDF on Linux
Script to combine PDF files
As you saw merging PDF files on Linux doesn’t require complex and lengthy commands. But, I was still trying to improve my productivity and figure out how to do it faster. Creating a shell script was my first idea, but I realize that it’s not worth it due to the simplicity of this process. So, I had a creative idea. Why not use an alias for that?!
Yes, I turn it further easier!
Create an alias to merge PDF files using PDFtk:
alias mf='pdftk *.pdf cat output'
you can add this line to your ~/.bashrc, so the alias will persist on your computer when you restart it.
To use this alias, you can type mf <output.pdf> like this example:
mf merged.pdf
Where <output.pdf> and merged.pdf is the new PDF file name with all pages together. In any folder you execute the mf alias, the pdftk will merge all PDF files in the current folder.
This alias achieves my necessities, but of course, you can modify it to adjust your preferences. Like, if you don’t want to specify the output for the new file name, you can include it on your alias definition.
Python Script: Merge PDF Files
Python provides us a module, PyPDF2, to manage pages in a PDF file and make it easier for us to merge PDF files using Python. It’s less than 20 lines!
The Python Script to merge PDF is available on Github.
You need to install the PyPDF2 module:
pip install PyPDF2
To use the script:
python bitslovers-pdf-merge.py file01.pdf file02.pdf
Conclusion
When I started to use Linux many years ago, we didn’t have too many alternatives to manage PDF files. Nowadays, it’s impressive how many options we can find to merge PDF files. And all of them it’s pretty straightforward to use. In addition, if you have preferences for a command line or graphical solution, have sure that you are not disappointed.
And if you need to build an application or automate any process that requires managing PDF files, it’s effortless to create a solution with what we have available.
I hope that all information from this article could help you in some way. Please help me to share this Post with more people on your social network. Leave a comment. I will love to see your opinion and suggestion.
Keep this going please, great job!