Merge PDF Files in Order with pdfunite
Stapling a cover page, a body and an appendix into one file by hand is a chore, and pdfunite does it correctly in one command. You will finish with one PDF containing the pages from several source files in the order you choose. The examples use pdfunite 24.02.0 from poppler-utils 24.02.0-1ubuntu9.9, as installed on this machine.
The route
Jump straight to the step you need, or tick off Done means at the end.
Allow about ten minutes. You need readable PDF files, enough free space for the destination, and permission to write to its directory. The normal workflow is unprivileged. Do not use sudo unless the input or destination is genuinely protected by filesystem permissions.
1. Check the installed command
Confirm which executable is on your path and record its version. These are read-only checks:
$ command -v pdfunite
/usr/bin/pdfunite
$ pdfunite -v
pdfunite version 24.02.0
Copyright 2005-2024 The Poppler Developers - http://poppler.freedesktop.org
Copyright 1996-2011, 2022 Glyph & Cog, LLC
The command takes source files followed by one destination file. Its documented options are only for version and usage information: -v, -h, -help and --help. The merge itself is controlled entirely by the file arguments, not by a format or page-range switch.
Checkpoint
If command -v finds nothing, install the distribution package that provides Poppler utilities through your normal package-management process. Do not download a replacement binary into a working directory without checking its provenance.
2. Inspect the inputs before merging
List the files you intend to combine and ask pdfinfo for a quick, read-only check. Replace the example paths with your own files:
$ ls -lh /path/to/report-cover.pdf /path/to/report-body.pdf /path/to/report-appendix.pdf
$ pdfinfo /path/to/report-cover.pdf | sed -n '1,14p'
Title: Report cover
Pages: 1
Encrypted: no
The exact metadata varies. The fields worth checking are a plausible page count and Encrypted: no. The pdfunite manual says source PDFs must not be encrypted; if one is, get an authorised unencrypted copy or use the document owner's approved workflow. Do not try to bypass a password or access control.
Check every source, not just the first one. This catches a misspelled path and an encrypted appendix before you create a result:
$ for file in /path/to/report-cover.pdf /path/to/report-body.pdf /path/to/report-appendix.pdf; do
> printf '%s: ' "$file"
> pdfinfo "$file" | awk -F': *' '/^Pages:/ { print $2; found=1 } /^Encrypted:/ { print $2; found=1 } END { if (!found) exit 1 }'
> done
/path/to/report-cover.pdf: 1
no
/path/to/report-body.pdf: 8
no
/path/to/report-appendix.pdf: 3
no
If pdfinfo cannot open a file, stop and fix the path or read permission. A failed inspection is not a reason to reach for sudo automatically.
3. Choose a new destination
Pick a destination that is not one of the source files. This makes the operation easier to review and stops you confusing an input with the result:
$ destination=/path/to/report-merged.pdf
$ test ! -e "$destination" && echo "destination is new"
destination is new
This check does not create the file. If the destination already exists, pick another name or make an explicit, recoverable backup first. Do not overwrite a useful document as part of a blind script. If you do take a backup, keep it until the merged PDF has been opened and checked: deleting it is irreversible.
Checkpoint
Write down the intended order now. pdfunite preserves the order of source arguments, so changing their order changes the page order.
4. Merge the PDFs
Run one ordinary command with all sources first and the destination last:
$ pdfunite \
> /path/to/report-cover.pdf \
> /path/to/report-body.pdf \
> /path/to/report-appendix.pdf \
> "$destination"
A successful run normally returns to the shell without a progress report. The command reads every page from the first source, then every page from the second, and so on. It does not ask you to select pages interactively.
There is no persistent service or configuration change to undo. If the result is wrong, keep the source files, choose a different destination, correct the argument order and run the command again. If an incomplete destination appeared after an error, do not treat it as a valid document: move it aside using your normal file-recovery procedure and keep it until you know whether it is needed for diagnosis.
5. Verify the result
Check that the output exists, reads as a PDF, and has the expected page count:
$ pdfinfo "$destination" | sed -n '1,14p'
Pages: 12
Encrypted: no
PDF version: 1.5
$ test -s "$destination" && echo "merged PDF is non-empty"
merged PDF is non-empty
In this example, the source page counts were 1, 8 and 3, so 12 pages is the expected total. Calculate the total for your own inputs rather than copying that number. A readable file and a successful exit status do not prove every page looks right, so open the result in a trusted PDF viewer and check the boundaries between source documents.
If the page count is unexpected, inspect each source again and compare the command with your written order. If pdfunite reports an error, keep the source files, read the error literally, and check paths, permissions and encryption before changing anything else. The installed command has no option that repairs a damaged or encrypted source.
6. Use help when a script needs a contract
For a script or a new machine, capture the local usage text rather than assuming another Poppler build has identical packaging:
$ pdfunite --help 2>&1 | sed -n '1,12p'
pdfunite version 24.02.0
Usage: pdfunite [options] <PDF-sourcefile-1>..<PDF-sourcefile-n> <PDF-destfile>
-v : print copyright and version info
-h : print usage information
-help : print usage information
--help : print usage information
Quote paths in shell scripts, especially ones containing spaces. Keep the source list explicit when document order matters, and check exit status before passing the destination to another process. Do not feed untrusted file names into an unreviewed command line.
Done means
- Version checked. pdfunite and its installed Poppler version were confirmed.
- Sources verified. Every source path was readable and every source was confirmed unencrypted.
- Order recorded. The source arguments appear in the required page order, with the destination last.
- Output checked. The merged file is non-empty, readable by pdfinfo, and has the expected total page count.
- Result reviewed. The result was visually checked before the source files or any backup were removed.