PDF files
File Conversion
PDF Merging
Tech Tutorial
Document Management

Merge / convert multiple PDF files into one PDF

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Merging PDF files is a common workflow for reports, contracts, and automated document pipelines. You can do it with desktop tools, but command-line and script-based methods are usually better for repeatability and large batches. This guide focuses on practical approaches that are easy to run on macOS, Linux, and CI environments.

Pick the Right Tool for Your Workflow

There is no single best tool for every case. Choose based on your constraints.

  • Use qpdf when you want a fast, reliable merge with minimal transformation.
  • Use Ghostscript when you want to normalize or compress output aggressively.
  • Use Python with pypdf when you want logic such as sorting, filtering, or metadata handling.

If your files contain sensitive data, local tools are safer than uploading documents to online services.

Merge with qpdf

qpdf is one of the simplest and most dependable options.

Install:

bash
1# macOS
2brew install qpdf
3
4# Ubuntu or Debian
5sudo apt-get update && sudo apt-get install -y qpdf

Merge files in order:

bash
qpdf --empty --pages part1.pdf part2.pdf part3.pdf -- merged.pdf

Merge all PDFs in a folder in lexicographic order:

bash
qpdf --empty --pages $(ls -1 *.pdf | sort) -- merged.pdf

For robust scripting, avoid raw ls expansion when file names may contain spaces. Use a shell array.

bash
1#!/usr/bin/env bash
2set -euo pipefail
3
4mapfile -t files < <(find . -maxdepth 1 -name '*.pdf' -print | sort)
5qpdf --empty --pages "${files[@]}" -- merged.pdf

Merge and Recompress with Ghostscript

Ghostscript can merge and optionally reduce output size. It can also convert incompatible producer quirks into a standardized result.

Install:

bash
1# macOS
2brew install ghostscript
3
4# Ubuntu or Debian
5sudo apt-get update && sudo apt-get install -y ghostscript

Merge with typical screen-quality compression:

bash
1gs -dBATCH -dNOPAUSE -q \
2   -sDEVICE=pdfwrite \
3   -dCompatibilityLevel=1.6 \
4   -dPDFSETTINGS=/screen \
5   -sOutputFile=merged.pdf \
6   part1.pdf part2.pdf part3.pdf

Common -dPDFSETTINGS options include /screen, /ebook, /printer, and /prepress. Lower size usually means lower image quality.

Automate with Python

When you need custom ordering, page selection, or integration into existing pipelines, Python is straightforward.

Install dependency:

bash
python -m pip install pypdf

Merge files:

python
1from pathlib import Path
2from pypdf import PdfReader, PdfWriter
3
4source_dir = Path("./invoices")
5output = Path("./combined-invoices.pdf")
6
7writer = PdfWriter()
8for pdf_path in sorted(source_dir.glob("*.pdf")):
9    reader = PdfReader(str(pdf_path))
10    for page in reader.pages:
11        writer.add_page(page)
12
13with output.open("wb") as f:
14    writer.write(f)
15
16print(f"wrote: {output}")

You can also merge only selected pages by indexing reader.pages explicitly.

python
# Example: first page only
writer.add_page(reader.pages[0])

GUI Option for Manual One-Off Jobs

For occasional manual work on macOS, Preview can combine files via thumbnails and drag-and-drop. This is convenient but harder to reproduce and audit than scripted methods. For recurring workflows, prefer commands or scripts committed with your project.

Verification Checklist

After creating the output file, validate correctness before sharing.

bash
qpdf --check merged.pdf
pdfinfo merged.pdf | head

Check page count and sample key pages visually. If output is unexpectedly large, try Ghostscript compression. If output has rendering issues, merge with qpdf first, then compress in a second step.

Common Pitfalls

  • Relying on online PDF mergers for confidential documents. Use local tools to reduce privacy risk.
  • Assuming shell wildcard order matches your intended order. Always sort explicitly.
  • Overcompressing with Ghostscript settings and making text or charts unreadable.
  • Ignoring password-protected or corrupted source files, which can break batch merges.
  • Skipping output validation, then discovering missing pages after distribution.

Summary

  • qpdf is excellent for clean, fast merges with predictable behavior.
  • Ghostscript is useful when you also need normalization and compression.
  • Python with pypdf is best for programmable workflows and page-level control.
  • Use deterministic file ordering and validate output after every merge.
  • Prefer local tooling over web upload services for sensitive documents.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.