pdfcpu

Logo

PDF tooling for Go and the command line.

View the Project on GitHub pdfcpu/pdfcpu


Changelog
Future Directions

Merge

Merge 2 or more PDF files into outFile. Have a look at some examples.

Usage

pdfcpu merge outFile inFile... [flags]

Flags

namedescriptiondefaultrequired
m(ode)create, append, zipcreateno
s(ort)sort inFiles if presentunsortedno
b(ookmarks)create bookmarksyesno
bookmark-modewrap, preservewrapno
d(ivider)insert separator between merged docsnono
optoptimize before writingyesno
optimizeoptimize before writingyesno
rmsigremove signaturesnono

Common Flags


Arguments

namedescriptionrequired
outFilePDF output file, use - to write to stdoutyes
inFile…at least 2 PDF input files subject to concatenation, use - for one stdin input in create modeyes

Restrictions

The following PDF elements are not carried over into the merged document:


Bookmark Mode

By default pdfcpu merge creates bookmarks for the merged result. The --bookmark-mode flag controls how input bookmarks are arranged in the output document.

modebehavior
wrapCreate one top-level bookmark per input file and place each input file’s existing bookmarks below this filename wrapper.
preservePreserve the input bookmark trees directly, without adding filename wrapper bookmarks.

wrap is the default and works well for document packages where each input file should remain visible as a top-level section. For example, merging cover.pdf, chapter1.pdf, and chapter2.pdf produces top-level bookmarks named after the input files. Any bookmarks already present in chapter1.pdf or chapter2.pdf appear below the corresponding file bookmark.

Use preserve when the input files already contain a carefully authored outline and the merged document should keep that outline structure without extra filename levels. This is useful when merging chapters exported from the same source document, or when every input already uses consistent top-level bookmark titles.

--bookmark-mode only applies when bookmarks are enabled. If you disable bookmark creation with --bookmarks=false, do not pass --bookmark-mode.


Examples

pdfcpu respects the order of the provided input files and merges accordingly. Merge three input files into out.pdf by concatenating in3.pdf to in2.pdf and the result to in1.pdf:

$ pdfcpu merge out.pdf in1.pdf in2.pdf in3.pdf

Merge all PDF files in the current directory into out.pdf and don’t create bookmarks:

$ pdfcpu merge out.pdf *.pdf -b=f

Merge files and keep the default bookmark wrapping. The output outline gets one top-level bookmark for each input file:

$ pdfcpu merge book.pdf cover.pdf chapter1.pdf chapter2.pdf

Resulting outline:

cover.pdf
chapter1.pdf
  Introduction
  Getting Started
chapter2.pdf
  Configuration
  Examples

Merge files while preserving their existing bookmark trees directly. No filename wrapper bookmarks are added:

$ pdfcpu merge --bookmark-mode preserve book.pdf chapter1.pdf chapter2.pdf

Resulting outline:

Introduction
Getting Started
Configuration
Examples

Merge some PDF files into an existing PDF file out.pdf and create divider pages between the merged documents:

$ pdfcpu merge --mode append -divider out.pdf in1.pdf in2.pdf in3.pdf

Zip two files together (eg. like in 1a,1b,2a,2b..):

$ pdfcpu merge --mode zip out.pdf a.pdf b.pdf

Merge local PDFs and upload the result:

$ pdfcpu merge - quarterly/*.pdf \
   | aws s3 cp - s3://acme-reports/quarterly/merged.pdf

Merge one PDF streamed from S3 with local files:

$ aws s3 cp s3://acme-reports/cover.pdf - \
   | pdfcpu merge - - chapter1.pdf chapter2.pdf \
   | aws s3 cp - s3://acme-reports/book.pdf