Manipulation · Go

Merge PDF files in Go

Combine, split, reorder and rotate PDF pages from Go. rust-pdf parses each document into an editable model, renumbers and remaps every object on merge, and rebuilds a clean page tree on output.

Last updated: 2026-06-29

Why Go needs this

Back-office Go services constantly stitch PDFs together: invoices into one statement, signed pages back into a contract, a cover sheet onto a report. Yet Go has no built-in way to even read an existing file, let alone reorder or rotate its pages.

rust-pdf flattens each document into a single page order, so merge, split, reorder, rotate and delete become plain list operations; objects from a second file are renumbered, references are deep-remapped, and the real page tree is rebuilt on save. The parser reads classic and cross-reference streams, object streams and encrypted input, so it works on files it never wrote.

A chi or net/http endpoint can accept two uploads and return the merged result in a handful of lines. Because output is deterministic, you can pin the merged bytes in a table-driven golden-file test and catch a regression the instant the hash changes, with no rendering step to flake.

  • Merge whole documents, extract page subsets, reorder with a permutation, rotate or delete pages.
  • Objects are renumbered and references deep-remapped, so merged files stay valid.
  • Reads classic and xref streams, object streams and RC4 / AES encrypted input.

Merge PDFs in Go with rust-pdf

Install with go get, then call the same idiomatic API every rust-pdf binding shares. The snippet below is real Go code from the reference docs.

go get github.com/rustpdf/rustpdf-go

Go
ad, _ := os.ReadFile("a.pdf")
bd, _ := os.ReadFile("b.pdf")
a, _ := rustpdf.Load(ad)
defer a.Close()
b, _ := rustpdf.Load(bd)
_ = a.Merge(b)                 // a now has a's pages followed by b's
b.Close()
_ = a.RotatePage(0, 90)
_ = a.Save("merged.pdf")

sub, _ := a.ExtractPages([]int{0, 2})   // pages 1 and 3
defer sub.Close()
_ = sub.Save("subset.pdf")
Validated by: qpdfmutool

This is part of the free tier in Go. No license required.

Full Go reference in the documentation.

Merging in Go: FAQ

Can I merge more than two PDFs in Go?

Yes. Load each document and call merge for each one; pages are appended in order. You can then reorder, rotate or extract any subset before saving.

Does merging keep the files valid?

Yes. On merge every object from the other document is renumbered and its references are deep-remapped, and the real page tree is rebuilt on output, so the result passes qpdf and mutool.

Is merging free?

Yes. Loading, merging, splitting, reordering and text extraction are all part of the free tier in Go. You only need a license for the corporate features such as PDF/A, signatures and encryption.

Does merge preserve bookmarks and form fields?

Page content, annotations and rotation survive the merge because every object is renumbered and its references are deep-remapped. Document-level structures such as the bookmark outline tree and a shared AcroForm are not stitched together, so rebuild those after merging if your workflow needs them.

Wire PDF assembly into your Go backend

One Rust core, the same byte-for-byte results in every language. Merging, splitting and reordering are free in Go — no token, no daemon.