Skip to content

Optimize

huzpsb edited this page Oct 25, 2026 · 1 revision

Optimize

Deduplicate and prune page resources — embedded fonts and images shared or unused across pages — and write a leaner PDF. Safe to run on any valid file; output is semantically identical.

Command line

pdfcpu2 optimize inFile [outFile] [flags]
Flag Meaning
--stats file.csv append a stats line (root/page resource usage) — great for batch monitoring
--rmenc remove encryption
--rmsig remove signatures
pdfcpu2 optimize in.pdf out.pdf
pdfcpu2 optimize --stats stats.csv in.pdf out.pdf
cat master.pdf | pdfcpu2 optimize - - > optimized.pdf

Go API

package main

import (
	"context"

	"github.com/huzpsb/pdfcpu2/pkg/api"
	"github.com/huzpsb/pdfcpu2/pkg/pdfcpu/model"
)

func main() {
	ctx := context.Background()
	conf := model.NewDefaultConfiguration()

	err := api.OptimizeFile(ctx, "in.pdf", "out.pdf", conf, nil)
	if err != nil {
		panic(err)
	}
}

The trailing *api.ProgressOptions enables progress callbacks for long files; api.Optimize(ctx, rs, w, conf, opts) is the streaming variant.

Tip: merge optimizes by default. For huge batch jobs you can disable it (--opt=false) and optimize once at the end.

Clone this wiki locally