Want to learn how to merge PDFs using command line tools? You can combine multiple PDF documents into one file using utilities such as pdfunite, qpdf, Ghostscript, PDFtk, or Python. These methods are useful for developers, system administrators, and anyone who wants to automate PDF management.
Instead of opening a graphical PDF editor and manually uploading documents, command-line tools let you merge files directly from Terminal, PowerShell, or a script.
For example, you can combine monthly reports, invoices, scanned documents, and business records into a single PDF using one command.
In this guide, you'll learn five practical ways to merge PDF files from the command line, with examples for Windows, macOS, and Linux. We'll also explain how to merge multiple PDFs automatically, handle filenames containing spaces, and troubleshoot common errors.
If you prefer a simpler browser-based solution, try the OneFileKit Merge PDF tool.
Quick Answer: How to Merge PDFs Using Command Line
One straightforward method is to use pdfunite, a utility included in the Poppler PDF tools.
Once installed, open your terminal and run:
pdfunite file1.pdf file2.pdf merged.pdfThis command combines the two input PDF files into a single document named merged.pdf.
The order of the input filenames determines the order of the documents in the output.
For example:
pdfunite cover.pdf report.pdf appendix.pdf final-report.pdfThis combines three PDFs into one file.
Important: pdfunite is not installed by default on every operating system. You may need to install Poppler or a compatible package first.
For documentation, see the official Poppler project.
Why Merge PDF Files Using Command Line?
Command-line PDF merging is useful when you need more control over document processing.
1. Automate Repetitive Tasks
Instead of manually merging the same types of documents every day, you can automate the process with a script.
2. Process Multiple Files
Command-line utilities can combine many PDFs without requiring you to select each file through a graphical interface.
3. Integrate With Applications
Developers can incorporate PDF merging into backend systems, document-processing applications, and scheduled workflows.
4. Work Without a Graphical Interface
Command-line tools are useful on remote servers and Linux environments without a desktop interface.
5. Process Files Locally
Many command-line utilities can merge PDFs without uploading documents to an external website.
6. Control File Ordering
You can specify exactly which documents should appear first, second, or last.
For users who don't need automation, the OneFileKit online PDF merger offers a simpler alternative.
Method 1: Merge PDF Files Using pdfunite
pdfunite is a command-line utility distributed with Poppler.
It combines multiple PDF files into a single output document.
Step 1: Install pdfunite
On Ubuntu or Debian-based Linux systems, you can install Poppler utilities using:
sudo apt update
sudo apt install poppler-utilsOn macOS, if you use Homebrew, you can install Poppler with:
brew install popplerFor Windows, use a trusted compatible Poppler distribution and ensure the utility is accessible from your terminal.
Step 2: Open Terminal
Open Terminal on macOS or Linux.
On Windows, open PowerShell or Windows Terminal.
Step 3: Navigate to Your PDF Folder
For example:
cd ~/Documents/PDFsOn Windows PowerShell, use a suitable directory such as:
cd "$HOME\Documents\PDFs"Step 4: Merge Two PDF Files
Run:
pdfunite document1.pdf document2.pdf merged.pdfThe command creates merged.pdf.
Step 5: Merge Multiple PDF Files
You can provide additional input files:
pdfunite cover.pdf report.pdf appendix.pdf final.pdfThe last filename is the output file.
Step 6: Check the Result
Open the merged PDF.
Verify that all pages are present and appear in the correct order.
Advantages of pdfunite
Simple command syntax.
Supports multiple input PDFs.
Suitable for scripting.
Available on Linux and other platforms through compatible packages.
Can process files locally.
Limitations
Encrypted PDFs and certain advanced document features may require additional handling.
Avoid using an existing input filename as the output filename.
For a graphical alternative, use OneFileKit Merge PDF.
Method 2: Merge PDF Files Using qpdf
qpdf is an open-source command-line utility for processing PDF files.
It supports operations such as combining pages, splitting documents, and manipulating PDF structures.
Step 1: Install qpdf
On Ubuntu or Debian:
sudo apt install qpdfOn macOS with Homebrew:
brew install qpdfWindows users can obtain a compatible release from the official project.
Step 2: Open Terminal
Navigate to the folder containing your PDFs.
Step 3: Combine Two PDFs
Run:
qpdf --empty --pages first.pdf second.pdf -- combined.pdfThis creates a new PDF containing pages from both input documents.
Step 4: Combine Three or More PDFs
For example:
qpdf --empty --pages cover.pdf report.pdf appendix.pdf -- final.pdfStep 5: Merge Selected Page Ranges
You can specify page ranges for individual files:
qpdf --empty --pages first.pdf 1-3 second.pdf 2-5 -- selected-pages.pdfThis combines pages 1–3 from the first document with pages 2–5 from the second.
Step 6: Verify the Output
Open the resulting PDF and check its page order.
Advantages of qpdf
Open-source software.
Supports page-range selection.
Suitable for automation.
Useful for PDF structure operations.
Available across major desktop operating systems.
Limitations
Some advanced document features may not survive page-level reconstruction unchanged.
Digital signatures and interactive document elements require particular care.
For detailed instructions, consult the official qpdf documentation.
Method 3: Merge PDFs Using Ghostscript
Ghostscript is a document-processing interpreter that supports PostScript and PDF workflows.
It can combine PDF documents by processing them through its PDF output device.
Unlike a straightforward page-copying utility, Ghostscript may rewrite or optimize document content.
Step 1: Install Ghostscript
On Ubuntu or Debian:
sudo apt install ghostscriptOn macOS:
brew install ghostscriptWindows users can obtain an appropriate Ghostscript installer from the official website.
Step 2: Open Terminal
Navigate to your PDF folder.
Step 3: Run the Merge Command
On macOS or Linux, run:
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=merged.pdf first.pdf second.pdfThis processes both PDFs and creates merged.pdf.
Step 4: Merge Multiple Files
You can add additional input filenames:
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=combined.pdf cover.pdf report.pdf appendix.pdfStep 5: Check the Result
Open the combined PDF.
Inspect text, images, fonts, links, and page formatting.
What Do the Command Options Mean?
Option | Purpose |
|---|---|
| Exit after processing |
| Process pages without interactive pauses |
| Reduce informational output |
| Generate PDF output |
| Specify the output filename |
Advantages of Ghostscript
Supports automated PDF processing.
Available on multiple platforms.
Can be integrated into scripts.
Provides additional PDF output controls.
Limitations
Ghostscript may rewrite PDF content, so it is not ideal when exact preservation of interactive features or digital signatures is required.
Only process documents from trusted sources using appropriately updated software and safe execution settings.
For technical information, consult the official Ghostscript documentation.
Method 4: Merge PDF Files Using Python
Python provides another way to combine PDF documents.
This approach is particularly useful for developers building document-management applications, backend services, or automation scripts.
One commonly used library is pypdf.
Step 1: Install Python
Install a supported Python version if it is not already available.
Step 2: Install pypdf
Open Terminal or PowerShell and run:
python -m pip install pypdfOn systems where Python is invoked as python3, use:
python3 -m pip install pypdfStep 3: Create a Python Script
Create a file named merge_pdfs.py.
Step 4: Add the PDF Merging Code
from pypdf import PdfWriter
writer = PdfWriter()
writer.append("first.pdf")
writer.append("second.pdf")
writer.append("third.pdf")
writer.write("merged.pdf")
writer.close()
print("PDF files merged successfully")Step 5: Run the Script
Execute:
python merge_pdfs.pyThe script creates merged.pdf in the current working directory.
Step 6: Verify the Output
Open the resulting document.
Check that the page count and content are correct.
Why Use Python for PDF Merging?
Python allows developers to integrate PDF merging into larger workflows.
For example:
Combine invoices automatically.
Merge uploaded documents in a web application.
Create monthly PDF reports.
Process customer documents.
Build document-management systems.
Can Python Merge Hundreds of PDFs?
Yes, subject to available memory, storage, document complexity, and library capabilities.
For large workloads, implement input validation, error handling, file limits, and resource monitoring.
The official pypdf documentation provides additional examples of merging PDFs and selecting page ranges.
If you only need to combine a few documents, OneFileKit Merge PDF avoids writing a script.
Method 5: Merge PDF Files Using PDFtk
PDFtk is a PDF processing toolkit that provides command-line operations for combining and manipulating PDF documents.
It is useful for users who prefer a concise command syntax.
Step 1: Install a Compatible PDFtk Utility
Obtain PDFtk or a compatible implementation from a trusted source.
Installation methods vary by operating system.
Step 2: Open Terminal
Navigate to the directory containing your PDFs.
Step 3: Combine Two Documents
Run:
pdftk first.pdf second.pdf cat output merged.pdfThis combines the two documents.
Step 4: Combine Multiple PDFs
For example:
pdftk cover.pdf report.pdf appendix.pdf cat output final.pdfStep 5: Merge Selected Pages
PDFtk also supports page selection.
For example:
pdftk A=first.pdf B=second.pdf cat A1-3 B2-5 output selected.pdfThis combines the specified page ranges.
Step 6: Review the Output
Open the resulting PDF and verify the contents.
Advantages of PDFtk
Concise commands.
Supports document merging.
Supports page selection.
Useful for scripts.
Works locally.
Limitations
Available packages and supported features vary between PDFtk implementations.
For project information, visit the PDFtk website.
Five Command-Line PDF Merging Methods Compared
Tool | Platform support | Best use |
|---|---|---|
pdfunite | Linux, macOS, compatible Windows builds | Simple merging |
qpdf | Windows, Mac, Linux | Page ranges and PDF structure |
Ghostscript | Windows, Mac, Linux | PDF processing and rewriting |
Python pypdf | Windows, Mac, Linux | Application development and automation |
PDFtk | Depends on implementation | Scripted merging and page selection |
Which Command-Line PDF Merger Should You Choose?
For simple PDF merging, pdfunite is a straightforward option.
For selecting specific pages and working with PDF structures, qpdf is useful.
For document processing that requires PDF rewriting, Ghostscript may be appropriate.
For developers, Python and pypdf offer flexibility.
PDFtk provides another option for command-line document manipulation.
How to Merge PDFs Using Command Line on Windows
Windows users can merge PDFs through PowerShell or Windows Terminal after installing a compatible PDF utility.
Step 1: Install a PDF Merging Utility
Choose a tool such as qpdf or a compatible Poppler distribution.
Step 2: Open PowerShell
Launch Windows Terminal or PowerShell.
Step 3: Navigate to Your Files
For example:
cd "$HOME\Documents\PDFs"Step 4: Run a Merge Command
If qpdf is installed:
qpdf --empty --pages "report.pdf" "appendix.pdf" -- "merged.pdf"Step 5: Open the Result
Verify the combined document.
For users who prefer a graphical interface, read How to Merge PDF Files on Windows.
How to Merge PDFs Using Terminal on Mac
macOS users can install compatible command-line PDF tools using a package manager.
Step 1: Install qpdf
If Homebrew is installed:
brew install qpdfStep 2: Open Terminal
Navigate to your PDF folder.
Step 3: Merge Your Documents
Run:
qpdf --empty --pages first.pdf second.pdf -- combined.pdfStep 4: Verify the Result
Open the combined PDF in Preview.
Alternative: Use Preview
If you don't need command-line automation, macOS Preview provides built-in PDF merging.
Apple explains the process in its official Preview guide.
You can also read How to Combine PDF Files on Mac.
How to Merge Multiple PDF Files Automatically
Command-line merging becomes especially useful when processing many files.
For example, you may have:
report-01.pdfreport-02.pdfreport-03.pdfreport-04.pdf
You want to combine them into one document.
Example Using pdfunite
On a Unix-like shell, when the input files follow a predictable naming convention:
pdfunite report-01.pdf report-02.pdf report-03.pdf report-04.pdf combined-report.pdfExample Using Python
from pathlib import Path
from pypdf import PdfWriter
files = sorted(Path("reports").glob("*.pdf"))
writer = PdfWriter()
for file in files:
writer.append(str(file))
writer.write("combined-report.pdf")
writer.close()This script combines PDF files from the reports directory in filename order.
The output is saved outside that input directory to avoid accidentally including it in a later run.
Why Automate PDF Merging?
Automation can reduce repetitive manual work.
It can be useful for:
Monthly business reports.
Invoice processing.
Document archiving.
Automated exports.
Internal document-management systems.
How to Merge PDFs in a Specific Order
The order of input files determines the sequence of pages in the merged document.
For example:
pdfunite cover.pdf chapter1.pdf chapter2.pdf appendix.pdf final.pdfThe output contains the cover first, followed by the chapters and appendix.
Tips for Correct Ordering
Use numbered filenames when processing many files.
For example:
01-cover.pdf02-introduction.pdf03-report.pdf04-appendix.pdf
Avoid assuming that wildcard expansion always matches your intended logical order.
For precise page-level organization, use a compatible page organizer.
OneFileKit provides an Organize PDF tool.
How to Merge Selected PDF Pages Using Command Line
Sometimes you don't want to combine entire PDF documents.
Instead, you may need selected pages from each file.
For example:
Pages 1–3 from the first PDF.
Pages 2–5 from the second PDF.
Using qpdf:
qpdf --empty --pages first.pdf 1-3 second.pdf 2-5 -- combined.pdfThis creates a PDF containing the selected pages.
When Is Page Selection Useful?
Combining selected report chapters.
Extracting document sections.
Preparing academic submissions.
Creating custom document collections.
Removing unnecessary content from a merged document.
For a graphical alternative, explore OneFileKit Extract PDF Pages.
Does Command-Line PDF Merging Reduce Quality?
Merging PDFs is not the same as compressing them.
Some command-line tools primarily combine or copy PDF pages, while others rewrite document content.
pdfunite and qpdf
These tools are generally used for combining document pages without intentionally reducing image resolution.
However, advanced document features may not always be preserved.
Ghostscript
Ghostscript processes PDF content through its output device.
Depending on settings, the output may differ from the original in file size, image encoding, or document features.
Python pypdf
pypdf can combine PDF pages without rendering them into images, but certain interactive features or document structures may require special handling.
Best Practices
Keep the original files.
Verify page count.
Check text and images.
Review links and bookmarks.
Test form fields.
Check digital signatures.
For additional information, read Does Compressing a PDF Reduce Quality?.
How to Reduce the Size of a Merged PDF
Combining multiple documents can create a large PDF.
For example, merging several scanned reports may produce a document containing many high-resolution page images.
If the resulting file is too large, consider using a PDF compressor.
Steps Using OneFileKit
Open OneFileKit Compress PDF.
Upload the merged PDF.
Choose an appropriate compression level.
Start compression.
Download the result.
Verify document quality.
For additional methods, read How to Compress PDF Files.
How to Merge Password-Protected PDFs Using Command Line
Some PDF files are encrypted or protected by passwords.
Whether they can be merged depends on their security settings and the capabilities of the selected utility.
Recommended Workflow
Confirm you have permission to access the PDFs.
Identify the required passwords.
Check the merging utility's documentation.
Use supported authentication options.
Merge the authorized documents.
Review the resulting security settings.
Do not assume that the merged document will retain the same password protection or permissions as its source files.
For sensitive documents, preserve the originals and use an appropriate security workflow.
Common Command-Line PDF Merging Errors and Fixes
1. Command Not Found
Problem: The terminal cannot find pdfunite, qpdf, or another utility.
Solution: Install the tool and verify that its executable is available in your PATH.
2. File Not Found
Problem: The specified input PDF does not exist.
Solution: Check the filename and working directory.
3. Permission Denied
Problem: The application cannot read the source file or write the output.
Solution: Check file permissions and choose an appropriate output directory.
4. Invalid PDF Structure
Problem: A source PDF is damaged or incompatible.
Solution: Open each file separately and verify that it is a valid PDF.
5. Password-Protected PDF
Problem: The utility cannot process an encrypted document.
Solution: Use supported authentication options when you are authorized.
6. Incorrect Page Order
Problem: The merged document has pages in the wrong sequence.
Solution: Explicitly specify input files in the desired order.
7. Output File Already Exists
Problem: A command may overwrite an existing file.
Solution: Use a new output filename and preserve your originals.
8. Large File Processing
Problem: Processing is slow or fails because of limited resources.
Solution: Check memory, disk space, file size, and application limits.
Is It Safe to Merge PDFs Using Command Line?
Command-line PDF merging can process files locally, which may be useful for confidential documents.
However, local processing does not automatically guarantee security.
Security Recommendations
Download tools from trusted sources.
Keep PDF utilities updated.
Avoid executing untrusted scripts.
Validate input filenames.
Use appropriate file permissions.
Limit access to sensitive documents.
Avoid passing untrusted filenames directly into shell commands.
Run untrusted document processing with suitable isolation and resource limits.
For applications that accept user-uploaded PDFs, implement additional security controls.
Command Line vs Online PDF Merger: Which Is Better?
Feature | Command-line tools | OneFileKit |
|---|---|---|
Installation | Usually required | No |
Internet connection | Not required after setup | Required |
Automation | Excellent | Manual browser workflow |
Batch processing | Supported by scripts | Depends on tool limits |
Technical knowledge | Required | Minimal |
Local processing | Available | Depends on service architecture |
Best for | Developers and automated workflows | Everyday PDF merging |
When to Use Command Line
Choose command-line tools when you need automation, repeatable workflows, or integration into applications.
When to Use OneFileKit
Choose OneFileKit Merge PDF when you want to combine documents quickly without installing or configuring command-line software.
Frequently Asked Questions
1. 2. 3.
4.
5.
6. 7. 8.
9.
10.
Related OneFileKit Articles and Tools
Explore these five relevant resources.
OneFileKit Merge PDF — Combine PDF documents online without command-line tools.
How to Merge PDF Files on Windows — Explore Windows PDF merging methods.
How to Combine PDF Files on Mac — Learn macOS PDF merging methods.
How to Merge Two PDF Files Into One — Combine two PDFs using five different approaches.
OneFileKit Organize PDF — Rearrange and organize PDF pages.
Final Thoughts
Learning how to merge PDFs using command line gives you more control over document processing and automation.
Utilities such as pdfunite, qpdf, Ghostscript, PDFtk, and Python's pypdf library provide different ways to combine PDF files.
For straightforward merging, pdfunite is a convenient option. For page-range selection, qpdf is useful. Python is particularly suitable for developers building automated document workflows.
However, command-line tools are not necessary for every PDF merging task.
If you want a simple browser-based solution, use the OneFileKit Merge PDF tool to upload, arrange, and combine your documents online.
Always preserve your original PDFs and review the merged document before sharing it.




