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.

Why Go needs this

Go's PDF story is fragmented: gofpdf is archived and unipdf is commercial, so production-grade output usually means a paid dependency or a brittle wrapper.

Optimization is lossless structural cleanup: nothing in the rendered page changes, the file just carries less weight. Combine optimize (dedupe and compress) with compact (object and xref streams) for the smallest valid output, ideal before archiving, emailing or serving documents at scale.

  • 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 the package, then call the same idiomatic API every rust-pdf binding shares. The snippet below is real Go code from the reference docs.

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.

Compress and Optimize a PDF in Go

One Rust core, the same output across every language. Prototype for free, license the corporate features when you ship.