Skip to main content
The merge command combines multiple PDF files into one unified PDF document while preserving the content and formatting of all input files.

Syntax

python solution.py merge <inputs...>

Parameters

inputs
string[]
required
Space-separated list of input PDF files to merge. At least 2 files required.Example: file1.pdf file2.pdf file3.pdf

Usage Examples

Merge Two PDFs

python solution.py merge file1.pdf file2.pdf
Expected Output:
Success: Merged PDF saved to merged_file1_file2.pdf

Merge Multiple Documents

python solution.py merge report.pdf appendix.pdf references.pdf
Expected Output:
Success: Merged PDF saved to merged_report_appendix_references.pdf

Merge with Full Paths

python solution.py merge ./docs/chapter1.pdf ./docs/chapter2.pdf ./docs/chapter3.pdf
Expected Output:
Success: Merged PDF saved to merged_chapter1_chapter2_chapter3.pdf

Real-World Examples

Combine Report Sections

python solution.py merge executive-summary.pdf methodology.pdf results.pdf conclusions.pdf
Output: merged_executive-summary_methodology_results_conclusions.pdf

Merge Invoice Pages

python solution.py merge invoice-page1.pdf invoice-page2.pdf invoice-page3.pdf
Output: merged_invoice-page1_invoice-page2_invoice-page3.pdf

Consolidate Scanned Documents

python solution.py merge scan001.pdf scan002.pdf scan003.pdf scan004.pdf scan005.pdf
Output: merged_scan001_scan002_scan003_scan004_scan005.pdf

Output File Naming Convention

The output filename follows this pattern:
merged_{stem1}_{stem2}_{stem3}...pdf
Where {stem} is the filename without extension. Examples:
Input FilesOutput Filename
doc1.pdf doc2.pdfmerged_doc1_doc2.pdf
a.pdf b.pdf c.pdfmerged_a_b_c.pdf
report-2024.pdf data.pdfmerged_report-2024_data.pdf

How It Works

  1. Input Validation: Checks that all input files exist and are valid PDFs
  2. Sequential Merging: Appends each PDF in the order specified
  3. Content Preservation: Maintains all pages, formatting, and embedded content
  4. 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:
python solution.py merge ch1.pdf ch2.pdf ch3.pdf ch4.pdf ch5.pdf

2. Consolidate Scanned Pages

Combine individually scanned pages into one document:
python solution.py merge page-*.pdf  # Note: Shell expansion may vary

3. Append Supporting Documents

Add appendices and references to main report:
python solution.py merge main-report.pdf appendix-a.pdf appendix-b.pdf references.pdf

4. Create Document Packages

Bundle related documents for distribution:
python solution.py merge cover-letter.pdf resume.pdf portfolio.pdf

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

python solution.py merge document.pdf corrupted.pdf
Error: Invalid PDF file: corrupted.pdf

File Not Found

python solution.py merge existing.pdf missing.pdf
Error: File not found: missing.pdf

Insufficient Arguments

python solution.py merge single.pdf
Error: At least 2 PDF files required for merging

Technical Details

The merge operation:
  • Uses the pypdf library’s PdfWriter class
  • Appends each input PDF to the writer object sequentially
  • Writes the final merged result to disk
  • Automatically closes file handles after completion
  • split - Extract specific pages from a PDF
  • convert - Convert between different file formats

Build docs developers (and LLMs) love