How to Merge PDF Files in Bash?


Introduction

On various occasions, we desired to combine PDF files to organise them, lessen clutter, or share them with others. Linux provides a variety of utilities for merging pdf files. Some of the most popular ones are listed below −

  • pdfunite

  • pdftk

  • gs

  • convert

  • qpdf

These application tools have a lot of functions, and for this article, we will concentrate on their file-merging functionality. Let's examine some of them individually.

pdfunite

"pdfunite" is the tool's name in the Poppler Utils package that merges PDF files. The use of pdfunite is rather easy.

Use the DNF command below to install the poppler-utils package if it isn't already installed −

dnf install poppler-utils

Give all the PDF filenames you want to merge in the arguments and give the output filename as the last argument. Assume we need to combine two PDF files. In my system, I have two pdf files named Linux.pdf and ShellIntro.pdf.

I want to merge these pdf files and want to save the output into a separate file named as Merge.pdf.

[root@localhost ~]# ls -lh
total 4.4M
-rw-------. 1 root root 1.3K Sep 22 10:15 anaconda-ks.cfg
-rw-r--r-- 1 root root 4.3M Oct 8 06:29 Linux.pdf
rw-r--r-- 1 root root 125K Oct 8 06:29 ShellIntro.pdf
[root@localhost ~]#

To combine these files, we can use the command shown below −

pdfunite Linux.pdf ShellIntro.pdf Merge.pdf
[root@localhost ~]# ls -lh
total 9.0M
-rw-------. 1 root root 1.3K Sep 22 10:15 anaconda-ks.cfg
-rw-r--r-- 1 root root 4.3M Oct 8 06:29 Linux.pdf
-rw-r--r-- 1 root root 4.6M Oct 8 07:29 Merge.pdf
-rw-r--r-- 1 root root 125K Oct 8 06:29 ShellIntro.pdf
[root@localhost ~]#

As we can see, the usage is quite simple. We provided the input PDF filenames and an output filename to which the combined PDF is written in order to build a merged PDF file.

pdftk

It is another tool to merge pdf files in linux. Let us take the previous example, we have two pdf files as we had used with pdfunite tool.

[root@localhost ~]# ls -lh
total 4.4M
-rw-------. 1 root root 1.3K Sep 22 10:15 anaconda-ks.cfg
-rw-r--r-- 1 root root 4.3M Oct 8 06:29 Linux.pdf
-rw-r--r-- 1 root root 125K Oct 8 06:29 ShellIntro.pdf
[root@localhost ~]#

Let us see how we can merge these two pdfs.

[root@localhost ~]# pdftk Linux.pdf ShellIntro.pdf cat output mergedfile.pdf
[root@localhost ~]# ls -lh
total 8.9M
-rw-------. 1 root root 1.3K Sep 22 10:15 anaconda-ks.cfg
-rw-r--r-- 1 root root 4.3M Oct 8 06:29 Linux.pdf
-rw-r--r-- 1 root root 4.5M Oct 8 07:52 mergedfile.pdf
-rw-r--r-- 1 root root 125K Oct 8 06:29 ShellIntro.pdf
[root@localhost ~]#

qpdf

The main purpose of the utility qpdf is to reorganise PDF files. qpdf is the tool to use if we need to take a few pages from many PDF files, arrange them in a specific sequence, and combine them into a single PDF file.

But for the time being, let's look at how we can combine PDF files using qpdf −

[root@localhost ~]# qpdf --empty --pages Linux.pdf ShellIntro.pdf -- new.pdf
[root@localhost ~]# ls -lh
total 14M
-rw-------. 1 root root 1.3K Sep 22 10:15 anaconda-ks.cfg
-rw-r--r-- 1 root root 4.3M Oct 8 06:29 Linux.pdf
-rw-r--r-- 1 root root 4.5M Oct 8 07:52 mergedfile.pdf
-rw-r--r-- 1 root root 4.5M Oct 8 08:11 new.pdf
-rw-r--r-- 1 root root 125K Oct 8 06:29 ShellIntro.pdf
[root@localhost ~]#

The command produced a new.pdf file, as can be seen. All of the contents from Linux.pdf and ShellIntro.pdf will be present in the order indicated on the command line.

Let's examine the many arguments presented −

-empty: to begin a document with nothing in it

–pages: Name the documents.

Conclusion

In this article, we looked at various PDF file-merging tools. We have discussed three major tools which are used to merge PDF files in Linux. We can merge any numbers of pdf files into a single one by using these tools.

Updated on: 21-Nov-2022

421 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements