I spend a lot of time around PDF files because I build file tools. One request comes up more than almost any other: "I just need these five PDFs as one file." If you live in a terminal, you don't need a GUI for that. Here are the four command-line tools I reach for, what each one is good at, and where each one can trip you up.

1. qpdf: my default

qpdf is a structural PDF tool. It rewrites the file without re-rendering pages, so text, vector graphics and image quality stay exactly as they were.

Install:

# Debian/Ubuntu sudo apt install qpdf # macOS brew install qpdf 

Merge whole files:

qpdf --empty --pages a.pdf b.pdf c.pdf -- merged.pdf 

--empty starts from a blank document, and everything between --pages and -- is the list of inputs. You can also pick page ranges per file:

qpdf --empty --pages report.pdf 1-3 appendix.pdf 2,5-z -- out.pdf 

z means the last page, so 5-z is "page 5 to the end". That range syntax is the main reason qpdf is my default: merging and trimming happen in a single command.

Watch out for: encrypted inputs. If a file has a user password, pass it with --password=... right after that file's name in the page list.

2. pdfunite: the simplest option

pdfunite ships with poppler-utils, which is probably installed already if you use pdftotext or pdftoppm.

sudo apt install poppler-utils # or: brew install poppler pdfunite a.pdf b.pdf c.pdf merged.pdf 

The last argument is the output. There are no options worth mentioning, and that's the point. It's great in scripts where you just want everything, in order:

pdfunite $(ls chapter-*.pdf | sort -V) book.pdf 

sort -V sorts chapter-10 after chapter-9 rather than after chapter-1, which plain ls ordering gets wrong.

Watch out for: the output filename. If you forget it, pdfunite treats your last input as the output and overwrites it. Keep backups or double-check the argument order. It also can't select page ranges.

3. Ghostscript: merge and shrink in one pass

Ghostscript doesn't copy pages. It interprets each input and writes a brand-new PDF, which means it can also recompress images while it merges.

gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite \ -sOutputFile=merged.pdf a.pdf b.pdf c.pdf 

Add a preset if you also want a smaller file:

gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite \ -dPDFSETTINGS=/ebook \ -sOutputFile=merged-small.pdf a.pdf b.pdf c.pdf 

/screen is the most aggressive, /ebook is a reasonable middle ground, and /printer and /prepress keep more image detail.

Watch out for: side effects of re-rendering. Form fields, some annotations and links may not survive, and fonts can be re-embedded or subset differently. Ghostscript is also much slower than qpdf on big files. Use it when you want to merge and compress in one go, not for a plain merge.

4. pdftk: the classic

pdftk has been around for a long time, and plenty of old scripts and Stack Overflow answers use it. On most distros today you'll get pdftk-java.

sudo apt install pdftk-java pdftk a.pdf b.pdf c.pdf cat output merged.pdf 

It uses handles for page selection:

pdftk A=report.pdf B=appendix.pdf cat A1-3 B2 B5-end output out.pdf 

Watch out for: availability. It isn't in Homebrew core, and the Java dependency is heavy if all you want is a merge. If you're starting fresh, qpdf does the same job.

Quick comparison

Tool Page ranges Keeps original quality Can shrink output Speed
qpdf Yes Yes No (structural only) Fast
pdfunite No Yes No Fast
Ghostscript Via -dFirstPage/-dLastPage per run Re-renders Yes Slow
pdftk Yes Yes No Medium

A small script I actually use

This merges every PDF in a folder in natural order, then checks the result:

#!/usr/bin/env bash set -euo pipefail out="${1:-merged.pdf}" mapfile -t files < <(ls *.pdf | grep -v "^${out}$" | sort -V) qpdf --empty --pages "${files[@]}" -- "$out" qpdf --check "$out" > /dev/null && echo "OK: ${#files[@]} files -> $out" 

qpdf --check is worth running after any merge. It catches broken cross-reference tables before you send the file to someone else.

When the terminal isn't an option

Not everyone you help will have a shell, Homebrew or apt. When a colleague or family member asks me to merge PDFs, installing qpdf on their laptop is overkill. For that case I built a free browser tool that needs no sign-up: Merge PDF on OneFileKit. You drop the files in, reorder them and download a single PDF.

For anything scripted, repeatable or running on a server, I still use qpdf.

Wrapping up

  • Plain merge, with or without page ranges: qpdf
  • Quickest merge with no options: pdfunite
  • Merge and make it smaller: Ghostscript with -dPDFSETTINGS
  • Maintaining old scripts: pdftk

What do you use? If there's a tool I've missed, especially on Windows, I'd like to hear about it in the comments.