Merge / convert multiple PDF files into one PDF
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
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
qpdfwhen you want a fast, reliable merge with minimal transformation. - Use Ghostscript when you want to normalize or compress output aggressively.
- Use Python with
pypdfwhen 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:
Merge files in order:
Merge all PDFs in a folder in lexicographic order:
For robust scripting, avoid raw ls expansion when file names may contain spaces. Use a shell array.
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:
Merge with typical screen-quality compression:
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:
Merge files:
You can also merge only selected pages by indexing reader.pages explicitly.
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.
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
qpdfis excellent for clean, fast merges with predictable behavior.- Ghostscript is useful when you also need normalization and compression.
- Python with
pypdfis 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.

