file

package
v1.117.0 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package file reads a PDF file through its cross-reference tables and appends incremental updates to it.

It reads what Chrome writes and what its own updates add: classic cross-reference tables, chained by /Prev. Cross-reference streams, compressed object streams and encryption are refused rather than half-read.

Index

Constants

View Source
const (

	// MaxObjectNumber is the largest object number a PDF may use (ISO 32000-1, Annex C).
	MaxObjectNumber = 8388607
)

Variables

View Source
var ErrUnsupported = fmt.Errorf("unsupported pdf feature: %w", altshiftErrors.ErrValidationError)

ErrUnsupported is a valid PDF feature this package does not read; it is a validation error, so an endpoint answers it as input it cannot take.

Functions

This section is empty.

Types

type File

type File struct {
	Data    []byte
	Trailer object.Dictionary
	// contains filtered or unexported fields
}

File is a parsed PDF file as it stands after its last update; it caches what it reads, so it is not safe for concurrent use.

func Open

func Open(data []byte) (*File, error)

Open reads the cross-reference chain from the last section back to the first, the newest entry for each object winning.

func (*File) Catalog

func (f *File) Catalog() (object.Dictionary, *object.Reference, error)

Catalog returns the document catalog and its reference.

func (*File) Generation

func (f *File) Generation(number int) int

Generation is the generation the object with that number has in the newest revision.

func (*File) NewUpdate

func (f *File) NewUpdate() *Update

NewUpdate starts an update that numbers new objects past every number the file has used.

func (*File) Object

func (f *File) Object(number int) (object.Object, error)

Object returns the indirect object with that number, or nil for one that is free or absent, as the syntax has a reference to it read.

func (*File) PageTree

func (f *File) PageTree() (*PageTree, error)

PageTree walks the page tree from the catalog's /Pages.

func (*File) Resolve

func (f *File) Resolve(value object.Object) (object.Object, error)

Resolve follows references until it reaches a direct object; a reference whose generation is not the object's reads as null.

func (*File) ResolveArray

func (f *File) ResolveArray(value object.Object) (object.Array, error)

ResolveArray resolves to an array.

func (*File) ResolveDictionary

func (f *File) ResolveDictionary(value object.Object) (object.Dictionary, error)

ResolveDictionary resolves to a dictionary, or a stream's dictionary.

func (*File) Size

func (f *File) Size() int

Size is the first object number no revision has used.

type Page

type Page struct {
	Reference  *object.Reference
	Dictionary object.Dictionary
	Inherited  object.Dictionary
}

Page is a leaf of the page tree; Inherited holds the attributes it takes from its ancestors.

type PageTree

type PageTree struct {
	Root      *object.Reference
	Pages     []*Page
	InnerNode []*object.Reference
}

PageTree is the document's pages in order, and the references of the tree's inner nodes.

type Update

type Update struct {
	// contains filtered or unexported fields
}

Update is an incremental update being assembled over a file: new and replaced objects, written after every byte that is already there.

func (*Update) NewObject

func (u *Update) NewObject() *object.Reference

NewObject reserves the next object number.

func (*Update) Set

func (u *Update) Set(reference *object.Reference, value object.Object) error

Set writes the value as the object the reference names, replacing it if the file has one.

func (*Update) SetRaw

func (u *Update) SetRaw(reference *object.Reference, body []byte)

SetRaw writes the object's body, between "N G obj" and "endobj", as given; for a body whose byte positions the caller has to know.

func (*Update) Write

func (u *Update) Write() (*Written, error)

Write appends the objects, a classic cross-reference section for them and a trailer pointing back at the previous section.

type Written

type Written struct {
	Data        []byte
	BodyOffsets map[int]int
}

Written is the file with the update appended, and where each object's body begins in it.

Jump to

Keyboard shortcuts

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