How to Merge PDF Files Using the Command Line on Ubuntu?

Merging PDF files on Ubuntu refers to the process of combining multiple PDF documents into a single file. The purpose of merging PDF files is to make it easier to manage, share, or print multiple documents as a single file. The merging PDF files can be useful in a variety of situations, such as when you need to submit multiple documents as a single file for a job application or when you want to print multiple pages in a booklet format.

This article will demonstrate various commands to merge PDF files on Ubuntu.

Method 1: Using pdftk Command to Merge PDF Files on Ubuntu

One of the most popular and easy-to-use command-line tools for merging PDF files on Ubuntu is “pdftk”. Here’s an example of how to use it to merge multiple PDF files into a single file:

Prerequisite: Install the “pdftk” Command

To Install the “pdftk” command line tool, the APT package manager is utilized with the “install” utility as below:

$ sudo apt install pdftk

Syntax “pdftk” Command:

The syntax for using the “pdftk” command to merge PDF files is as follows:

$ pdftk file1.pdf file2.pdf file3.pdf cat output merged.pdf

This command takes the PDF files “file1.pdf“, “file2.pdf“, and “file3.pdf” and concatenates them together, creating a new file called “merged.pdf” that contains all of the pages from the original files. The “cat” keyword is used to specify the concatenated files.

Example 1: Merge All PDF Files

To merge all PDF files in the current directory, utilize the “pdftk” command by specifying the extension. The “cat” keyword is used with the “output” to specify that the files should be concatenated. After that, specify the merged file name as “finaltest.pdf” in the following command:

$ pdftk *.pdf cat output finaltest.pdf

This command takes all PDF files and ties them into a new file called “finaltest.pdf“.

Example 2: Merge Specific PDF Files

You can specify the file name of each file as “test1.pdf” and “test2.pdf” to merge by adding them:

$ pdftk test1.pdf test2.pdf cat output final.pdf

The output shows that test1.pdf and test2.pdf have been merged into the single file “final.pdf”.

Method 2: Using ghostscript Command to Merge PDF Files on Ubuntu

Another command-line tool, “ghostscript” can be utilized to merge pdf files on Ubuntu. The step-by-step procedure is mentioned below merge PDF files:

Prerequisite: Install ghostscript Command

To install the ghostscript command line tool, utilize the following command:

$ sudo apt install ghostscript

Syntax of ghostscript Command

The syntax for using the Ghostscript command in Linux is as follows:

$ gs [options] [file...]

In the above syntax, “gs” is the command for running ghostscript, “options” are any optional parameters you want to include, and “file” is the name of the input file(s) you want to process.

Example 1: Merge All the PDF files

To merge all the PDF files in the current directory, use the following command. This command has “sPAPERSIZE”, “dNOPAUSE”, “dBATCH”, and various arguments to configure the script of PDF files:

$ gs -q -sPAPERSIZE=letter -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=merged.pdf *.pdf

It takes all PDF files and merges them into a new file called “merged.pdf“.

Example 2: Merge Specific PDF Files

You can specify the file name “test1.pdf” and “test2.pdf” to merge them into the single file:

$ gs -q -sPAPERSIZE=letter -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=merged.pdf test1.pdf test2.pdf

The output shows that test1.pdf and test2.pdf have been merged into the single file “merged.pdf”.

Note: If you want to merge PDF files on other Linux distributions, click here to read our post.

Conclusion

Linux offers the “pdftk” and “ghostscript” commands to merge PDF files into one file on Ubuntu. Before merging, it is necessary to install the utilities that are provided above. Using these commands, you can concatenate a specific number or all PDF files into one file. This article has explained two command line tools for merging PDF files on Ubuntu.