Often we need to work with multiple files and need to combine or merge them into single file. In this article, we will look at how to combine multiple PDF files into one in Linux. There are several third-party libraries available for this purpose. We will look at how to combine multiple PDF files into one using PDFtk and Poppler utilities.
How to Combine Multiple PDF Files into One in Linux
Here are the steps to merge multiple PDF files in Linux.
1. Using PDFtk
PDFtk is a popular utility that allows you to split as well as merge PDF files. It is available as free version with paid premium features. PDFtk is available in command-line as well as graphical interface.
Open terminal and run the following command to install PDFtk.
$ sudo apt-get install pdftk
Redhat, CentOS, Fedora
In this case, you need to first install EPEL repository.
$ sudo yum install epel-release OR $ sudo dnf install epel-release
Then run the following command to install PDFtk.
$ sudo yum install pdftk OR $ sudo dnf install pdftk
Now that you have installed PDFtk, let us try to merge PDF files with it. Let us say you have files test1.pdf, test2.pdf and test3.pdf PDF files, and you want to merge them into new-test.pdf file. Here is the command to do it.
$ sudo pdftk test1.pdf test2.pdf test3.pdf cat output new-test.pdf
Please note, if you do not specify full file paths for input/output files PDFtk will look for the files in your present working directory. Here is an example to merge PDF files located in different folders.
$ sudo pdftk /home/ubuntu/test1.pdf /home/data/test2.pdf cat output /home/user/new-test.pdf
You can also use wildcard characters to merge PDF files as shown below. Here is the command to combine all PDF files in your present working directory.
$ sudo pdftk *.pdf cat output new-test.pdf
2. Using Poppler
Poppler is a PDF rendering library available for free in Linux. Here is the command to install it, depending on your Linux distribution.
$ sudo apt-get install poppler-utils
$ sudo yum install poppler-utils
Once you have installed Poppler, you can use the pdfunite command to combine multiple PDF files into one, as shown below. In the following command, the last filename is automatically used as the output file.
$ sudo pdfunite test1.pdf test2.pdf test3.pdf newfile.pdf
In the above command, test1.pdf test2.pdf and test3.pdf are combine to form newfile.pdf. Since last the file is automatically used as output file, if you miss mentioning it, then the last file in your command will be overwritten with the contents of other files.
Also, if you do not mention full file paths, pdfunite will look for the files in your present working directory.
Here is a command to combine PDF files located in different directories.
$ sudo pdfunite /home/ubuntu/test1.pdf /home/test2.pdf /home/data/output.pdf
You can also use wildcard characters to combine multiple files. Here is the command to combine all PDF files in /home/ubuntu to output.pdf.
$ sudo pdfunite *.pdf output.pdf
In this article, we have learnt how to combine multiple PDF files. If you need to automate the above combination of PDF files, you can add the above command in a shell script, or even run it as cron jobs in Linux.