Optimization · Go

Compress a PDF in Go

Shrink PDF file size from Go. rust-pdf drops unreferenced objects, Flate-compresses uncompressed streams, dedupes byte-identical objects, and can pack everything into object streams with a cross-reference stream.

Last updated: 2026-06-29

Why Go needs this

PDFs that a Go service archives, emails or serves at scale are often bloated with uncompressed streams and duplicated objects. Go ships no tool to slim them, and the usual fix, re-rendering through an external converter, quietly degrades the page and costs a process spawn per file.

rust-pdf optimizes losslessly: it drops unreferenced objects, Flate-compresses raw streams and dedupes byte-identical objects, then optionally packs everything into an object stream with a cross-reference stream. The rendered page is untouched and the smaller output still passes qpdf and mutool.

Deployment stays simple even though the engine is native: ship libpdf_ffi beside the binary, resolved by the macOS install-name or a Linux -rpath, or bake both into a multi-stage scratch image. With no daemon to manage, the optimize step runs in-process inside your existing HTTP service.

  • Drop unreferenced objects, Flate-compress streams and dedupe identical objects.
  • Pack objects into object streams and emit a cross-reference stream for the smallest size.
  • Lossless: the rendered page is unchanged, the output still passes qpdf and mutool.

Compress a PDF 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
data, _ := os.ReadFile("big.pdf")
ed, _ := rustpdf.Load(data)
defer ed.Close()
_ = ed.Optimize()
_ = ed.Compact(true)
_ = ed.Save("small.pdf")
Validated by: qpdfmutool

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

Full Go reference in the documentation.

Compression in Go: FAQ

How much smaller will my PDF be in Go?

It depends on the input. Files with uncompressed streams, duplicated objects or no object streams shrink the most. Optimization is lossless, so the savings come from structure, not from degrading content.

Does compression reduce quality?

No. This is structural optimization, not image down-sampling. The rendered page is identical; only redundant or uncompressed structure is removed, so the output still validates.

Is optimization free?

Yes. Optimize and compact are part of the free tier in Go. No license is needed.

How do I deploy the shared library alongside my Go binary?

Ship libpdf_ffi next to the executable: the macOS dylib resolves through its install-name and the Linux .so through an -rpath, or simply drop both into a multi-stage scratch or distroless image. There is no daemon to run, so Optimize and Compact execute in-process inside your service.

Serve leaner PDFs from your Go apps

One Rust core, the same lossless optimization in every language. Optimize and compact are free in Go — ship smaller files without a token.