pdftest

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package pdftest builds minimal synthetic PDF files for tests, in the spirit of net/http/httptest. The helpers reproduce structural patterns seen in real-world PDFs — Form XObjects, composite fonts, ToUnicode CMaps, segmented content streams (this file); Flate streams, PDF 1.5 cross-reference streams, object streams, and hybrid-reference files (xref.go); and standard-handler-encrypted files (crypt.go) — from entirely synthetic content.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Build

func Build(rootID int, objs ...string) []byte

Build assembles a PDF from numbered objects: objs[i] becomes object i+1. rootID is the /Root (catalog) object number.

func BuildEncrypted added in v0.2.0

func BuildEncrypted(rootID int, spec EncryptSpec, objs ...string) []byte

BuildEncrypted assembles an encrypted classic-xref PDF. objs[i] becomes object i+1; stream bodies are encrypted with the per-object key. The /Encrypt dictionary is added as an extra object. Fixtures must not contain literal strings outside streams (the builder does not rewrite them). rootID is the catalog.

func BuildHybrid added in v0.2.0

func BuildHybrid(rootID int, objs ...string) []byte

BuildHybrid assembles a hybrid-reference file: a classic xref table plus a /XRefStm pointing at a supplementary xref stream that alone locates the last object. A reader ignoring /XRefStm loses that object.

func BuildObjStm added in v0.2.0

func BuildObjStm(rootID int, packed []int, objs ...string) []byte

BuildObjStm assembles a PDF that packs the objects named in `packed` (1-based object numbers) into one object stream, with an xref stream referencing them by type-2 entries. Objects not listed in `packed` remain direct. Stream objects cannot be packed.

func BuildXrefStream added in v0.2.0

func BuildXrefStream(rootID int, objs ...string) []byte

BuildXrefStream assembles a PDF whose cross-reference data is a PDF 1.5 xref stream (no classic table). objs[i] becomes object i+1; the xref stream is appended as the final object. rootID is the catalog.

func CIDFont

func CIDFont(w string) string

CIDFont renders the descendant font with a /W widths array.

func Catalog

func Catalog(pagesID int) string

Catalog renders the document catalog pointing at the page tree root.

func Flate added in v0.2.0

func Flate(dict, content string) string

Flate renders a stream object whose body is zlib-compressed, emitting /Filter /FlateDecode and the correct /Length.

func Helvetica

func Helvetica() string

Helvetica renders a standard-14 simple font object (WinAnsi).

func Hex2

func Hex2(codes ...int) string

Hex2 renders 2-byte character codes as a PDF hex string, e.g. Hex2(1, 2) → "<00010002>".

func Page

func Page(parentID, contentsID int, resources string) string

Page renders a single page with the given resources and content stream.

func PageContentsArray

func PageContentsArray(parentID int, resources string, contentsIDs ...int) string

PageContentsArray renders a page whose /Contents is an array of streams forming one logical content stream (tokens may straddle boundaries).

func Pages

func Pages(kidIDs ...int) string

Pages renders the page tree root listing the given page objects.

func Stream

func Stream(dict, content string) string

Stream renders a stream object; /Length is computed from content.

func StreamWithLength added in v0.2.0

func StreamWithLength(dict, content string, length int) string

StreamWithLength renders a stream object with an explicit (possibly wrong) /Length, for exercising length recovery.

func ToUnicodeCMap

func ToUnicodeCMap(body string) string

ToUnicodeCMap renders a ToUnicode CMap stream object. Entries go inside the bfchar/bfrange sections, e.g. "<0001> <0048>".

func Type0Font

func Type0Font(descendantID, toUnicodeID int) string

Type0Font renders a composite Identity-H font referencing a descendant CIDFont and a ToUnicode stream.

Types

type EncryptSpec added in v0.2.0

type EncryptSpec struct {
	R             int  // revision: 2, 3, or 4
	AES           bool // R4 only: AES-128 (AESV2) instead of RC4
	UserPassword  string
	OwnerPassword string
}

EncryptSpec selects the standard-security-handler variant for BuildEncrypted.

Jump to

Keyboard shortcuts

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