This concise article will demonstrate how to combine or separate PDF files using both command-line and graphical user interface (GUI) tools.
Whether you are a beginner or an experienced Linux user, you will find these methods accessible and efficient. Let’s begin.
1 – Utilizing PDFTK
PDFTK (PDF Toolkit) is a versatile command-line utility designed for manipulating PDF documents. It offers a wide range of operations on PDF files, including the ability to split, merge, encrypt, and decrypt them, among other functionalities.
For a comprehensive guide on installing and effectively using PDFTK to merge or split PDF documents within a Linux environment, please refer to this guide.
Read: The Best Available Online PDF to Word Converter
2 – Employing the QPDF Tool
QPDF is a lightweight yet powerful program engineered for performing content-preserving and structural modifications on PDF documents. It enables the selective copying of objects between PDF documents and provides precise control over the page order and selection within PDF files. This functionality makes QPDF, which has minimal dependencies, an excellent tool for both splitting and merging PDF documents.
Developers working with PDF generation applications will find QPDF’s capabilities particularly advantageous. It can even be used to programmatically create PDF documents from scratch. However, it’s important to note that QPDF is not intended as a PDF viewer or a converter to other file formats, as it primarily focuses on the structural aspects of PDF files and disregards the semantic interpretation of content streams.
a – QPDF Installation
To install the lightweight QPDF utility, execute the following sudo command in your terminal:
sudo apt install qpdf
Read: Best pdf converters in 2023
b – QPDF Command Syntax
The fundamental syntax for invoking QPDF from the command line is:
qpdf [ options ] input_filename [ output_filename ]
This command structure typically transforms the `input_filename` PDF file into a new PDF file named `output_filename`. While the output document retains the full functionality of the input file, its internal structure might be reorganized. Various options, detailed below, allow for extensive transformations on PDF files. The `–empty` parameter can replace the `input_filename` attribute when you intend to create a new PDF and add pages from other files using the `–empty` switch.
Read: How To Reduce The Size Of Your PDF Files
The following example demonstrates calling `qpdf` with the `–empty` switch:
Each of these commands will generate a new, separate PDF file.
If `@filename` is included anywhere in the command line, QPDF interprets each line within the specified file as a command-line argument. The `@` switch effectively enables QPDF to process a large number of arguments, particularly useful when arguments are lengthy or numerous.
Specifying “-” as the `output_filename` argument directs QPDF to write the output to standard output. To overwrite the input file directly with the modified output, use the `–replace-input` option and omit the `output_filename`.
c – Merging Files with QPDF
QPDF offers robust capabilities for merging and splitting PDF files by selectively incorporating pages from one or more source documents. When merging, the first input file specified is treated as the primary file, serving as the foundation upon which pages from other input files are added based on the command arguments.
--pages input-file [ --password=password ] [ page-range ] [ ... ] --
The syntax allows for multiple input files to be specified, each with an optional password for password-protected documents and a defined page range. The “–” delimiter signals the end of page selection options.
To merge multiple PDF files into a single document, use the following command:
[merge pdf linux command]:
qpdf --empty output_merged.pdf --pages input_file1.pdf input_file2.pdf
In this command, `input_file1.pdf` and `input_file2.pdf` are combined to create a merged PDF document named `output_merged.pdf`.
To merge all PDF files located in the current directory into a single output file, execute:
[pdf merge linux]:
qpdf --empty output_file.pdf --pages *.pdf --
Read: Review of PDF Wiz – The complete PDF converter
d – Collation Option in QPDF
The `–collate` option modifies the behavior of `–pages`, causing input files to be collated rather than simply concatenated as directed by page ranges. For example, consider `odd.pdf` containing odd-numbered pages and `even.pdf` containing even-numbered pages of a document. The command:
qpdf --collate --pages odd_file.pdf even_file.pdf -- all.pdf
will collate the pages from these files. The output `all.pdf` will contain pages interleaved: page 1 from `odd_file.pdf`, followed by page 1 from `even_file.pdf`, then page 2 from `odd_file.pdf`, page 2 from `even_file.pdf`, and so on, until all pages from both files are included. This collation process supports any number of files or page ranges. If files have unequal page counts, files with fewer pages will be skipped once all their pages have been incorporated.
e – Specific Page Selection
To extract specific pages, such as pages 1 through 7, from an input file named `input_file.pdf` while preserving all associated metadata, use the command:
qpdf input_file.pdf --pages 1-7 -- outfile.pdf
To extract pages 1 through 5 from `infile.pdf` but discard the original metadata, you can use:
qpdf --empty --pages infile.pdf 1-5 -- outfile.pdf
3 – PDFUNITE Tool
PDFUNITE is a utility included within the `poppler-utils` package. Installing `poppler-utils` automatically provides PDFUNITE, allowing you to immediately begin merging PDF files upon installation.
a – PDFUNITE Syntax
PDFUNITE employs a straightforward syntax:
pdfunite [options] Inputfile1.pdf Inputfile2.pdf .. MergedFile.pdf
In this syntax, `Inputfile1.pdf`, `Inputfile2.pdf`, etc., represent the source PDF files to be merged. The `MergedFile.pdf` argument, placed at the end of the command, specifies the name of the output merged file.
b – Merging Files using PDFUNITE
To merge PDF files into a single PDF document, utilize the following command:
(Ubuntu pdf merge command):
pdfunite InputFile1.pdf InputFile2.pdf InputFile3.pdf merged_File.pdf
Ensure that input files are located in the directory where PDFUNITE is executed. For PDF files in different directories, provide their absolute paths in the command.
4 – Utilizing PDFSEPARATE
Similar to PDFUNITE, PDFSEPARATE is also part of the `poppler-utils` package.
a – PDFSEPARATE Syntax
The syntax for PDFSEPARATE is as follows:
pdfseparate [options] InputFile.pdf OutputFile_Pattern
PDFSEPARATE processes `InputFile.pdf`, dividing it into multiple PDF files based on `OutputFile_Pattern`, with each output file containing a single page.
The `OutputFile_Pattern` must include the wildcard `%d`, which PDFSEPARATE replaces with the page number for each output file. Note that the input PDF file should not be password-protected.
b – PDFSEPARATE Command Options
PDFSEPARATE primarily offers two options:
`-f number`: Specifies the starting page for extraction. If omitted, extraction begins from the first page (page 1).
`-l number`: Specifies the last page for extraction. If omitted, extraction continues to the final page of the document.
c – Splitting Files with PDFSEPARATE
To split `InputFile.pdf` into individual pages, use the command:
pdfseparate InputFile.pdf InputFile-%d.pdf
This command instructs PDFSEPARATE to extract all pages from `InputFile.pdf` into separate files. For example, if `InputFile.pdf` contains four pages, the command will generate four files:
InputFile-1.pdf, InputFile-2.pdf, InputFile-3.pdf, and InputFile-4.pdf
5 – Using PDFSAM
Besides command-line tools, GUI-based utilities are also available for merging and splitting PDF files. PDFSAM (PDF Split and Merge) is one such application. It offers various functionalities, including merging, splitting, rotating, and extracting pages, as well as manipulating bookmarks. PDFsam, a Java-based application, is readily available in most Linux distributions. Its graphical interface is designed to be intuitive, straightforward, and user-friendly. For Ubuntu/Debian systems, you can install PDFsam using the APT package manager with the command:
sudo apt-get install pdfsam
Once installation is complete, launch PDFsam by executing:
pdfsam
A startup splash screen will appear, indicating that the application is initializing:
Subsequently, the main PDFSAM graphical interface will be displayed, as shown:
Clicking the ‘Merge’ button will open the merge operation window:
Click the ‘Add’ button to select the PDF files for merging. Then, navigate to the ‘Destination file’ section and click ‘Browse’:
Choose a location and filename for the merged PDF file. Finally, click the ‘Run’ button to complete the merge process.
To split a PDF document, click the ‘Split’ button in the main PDFSAM interface:
The process for splitting is similar to merging. Use the file browser to select the PDF file to be split. Configure the ‘Split settings’ as needed, and then specify the output directory for the split files. Click ‘Run’ to initiate the splitting operation.
6 – Conclusion
This article has demonstrated various methods for merging and splitting PDF documents using command-line tools such as PDFTK, PDFUNITE, PDFSEPARATE, and QPDF. For users who prefer a graphical interface, PDFSAM offers a user-friendly GUI-based alternative.
If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.