optimize

package
v0.11.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 23, 2026 License: Apache-2.0 Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package optimize implements a first rewrite pass for existing PDF files: parse an input document with reader.Parse and re-serialize it through the writer's lossless optimization passes (cross-reference streams, object streams, orphan sweep, stream recompression, and object deduplication — see document.WriteOptions).

The pipeline only carries pages and their resources across the rewrite: it walks the page tree via reader.NewCopier, not the document catalog, so document-level structure the copier does not already handle — outlines, AcroForm fields, embedded file attachments, named destinations, page labels, and the structure tree — is dropped from the output. This makes the package a measurement vehicle for how much a lossless rewrite saves, not a general-purpose PDF optimizer.

Encrypted inputs are refused, even ones that decrypt with an empty password: the standard security handler derives per-object keys from object numbers, and the rewrite renumbers every surviving object. Signed inputs are not rejected but rewriting one invalidates its signature, since the signed byte range no longer exists in the output.

Input carrying document-level compliance data — PDF/A output intents, XMP metadata, embedded/associated files, tagging, or a document language — is refused with ErrLossyInput unless the caller opts in with Options.AllowLossy, since the rewrite would silently strip it.

Index

Constants

This section is empty.

Variables

View Source
var ErrEncrypted = errors.New("optimize: input PDF is encrypted; decryption is not supported")

ErrEncrypted is returned by Bytes when the input PDF is encrypted. Decryption is out of scope; the caller must supply an already decrypted document.

View Source
var ErrLossyInput = errors.New("optimize: input carries document-level data the rewrite would drop")

ErrLossyInput is returned by Bytes when the input carries document-level structure the rewrite would silently drop — PDF/A output intents, XMP metadata, embedded/associated files, tagging, or a document language. Opt in with Options.AllowLossy to strip it anyway.

Functions

This section is empty.

Types

type Options added in v0.10.1

type Options struct {
	// AllowLossy permits optimizing a document that carries
	// document-level compliance data (see [ErrLossyInput]), accepting
	// that the rewrite strips it.
	AllowLossy bool
}

Options configures an optimize pass.

type Stats

type Stats struct {
	BytesIn  int // size of the input PDF
	BytesOut int // size of the rewritten PDF
}

Stats reports the size outcome of an optimize pass.

func Bytes

func Bytes(data []byte, opts ...Options) ([]byte, Stats, error)

Bytes parses data as a PDF, rebuilds it through the writer's lossless optimization passes, and returns the result. The output always parses back into an equivalent page sequence and is never larger than the input — if the rewrite does not shrink the file, Bytes returns the original bytes unchanged.

Bytes returns ErrEncrypted for encrypted input, including a document that decrypts successfully with an empty password.

Bytes returns ErrLossyInput when the input catalog carries document-level compliance data (PDF/A output intents, XMP metadata, embedded/associated files, tagging, or a document language) that the rewrite would silently drop, unless the caller passes Options{AllowLossy: true}.

func (Stats) SavedBytes

func (s Stats) SavedBytes() int

SavedBytes returns how many bytes smaller the output is than the input. Never negative: Bytes falls back to the original input whenever the rewrite would grow it.

func (Stats) SavedPercent

func (s Stats) SavedPercent() float64

SavedPercent returns SavedBytes as a percentage of BytesIn, or 0 when BytesIn is 0.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL