merge command combines multiple PDF files into one unified PDF document while preserving the content and formatting of all input files.
Syntax
Parameters
Space-separated list of input PDF files to merge. At least 2 files required.Example:
file1.pdf file2.pdf file3.pdfUsage Examples
Merge Two PDFs
Merge Multiple Documents
Merge with Full Paths
Real-World Examples
Combine Report Sections
merged_executive-summary_methodology_results_conclusions.pdf
Merge Invoice Pages
merged_invoice-page1_invoice-page2_invoice-page3.pdf
Consolidate Scanned Documents
merged_scan001_scan002_scan003_scan004_scan005.pdf
Output File Naming Convention
The output filename follows this pattern:{stem} is the filename without extension.
Examples:
| Input Files | Output Filename |
|---|---|
doc1.pdf doc2.pdf | merged_doc1_doc2.pdf |
a.pdf b.pdf c.pdf | merged_a_b_c.pdf |
report-2024.pdf data.pdf | merged_report-2024_data.pdf |
How It Works
- Input Validation: Checks that all input files exist and are valid PDFs
- Sequential Merging: Appends each PDF in the order specified
- Content Preservation: Maintains all pages, formatting, and embedded content
- File Creation: Writes the merged result to the current working directory
Features
- Order Preservation: PDFs are merged in the exact order specified on the command line
- Metadata Retention: Preserves PDF metadata where possible
- Page Numbering: Concatenates all pages sequentially
- No Page Limit: Can merge PDFs with any number of pages (limited only by system memory)
- Format Support: Works with all standard PDF versions
Common Use Cases
1. Combine Report Chapters
Merge individual chapter PDFs into a complete document:2. Consolidate Scanned Pages
Combine individually scanned pages into one document:3. Append Supporting Documents
Add appendices and references to main report:4. Create Document Packages
Bundle related documents for distribution:Notes
- All input files must be valid PDF documents
- The merged PDF is saved in the current working directory
- Files are merged in the exact order specified
- Interactive PDF features (forms, JavaScript) may not be preserved
- Encrypted or password-protected PDFs must be decrypted first
- Large files may require significant memory during merging
Error Handling
Invalid PDF File
File Not Found
Insufficient Arguments
Technical Details
The merge operation:- Uses the
pypdflibrary’sPdfWriterclass - Appends each input PDF to the writer object sequentially
- Writes the final merged result to disk
- Automatically closes file handles after completion