syntax

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: Apache-2.0 Imports: 18 Imported by: 1 module, 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 syntax reads the PDF file format: objects, cross references, object streams, damaged file recovery, the stream filters and encryption.

Index

Constants

View Source
const DefaultStreamCacheBytes = 1 << 26

DefaultStreamCacheBytes is the default bound of a file's decoded stream cache.

Variables

View Source
var (
	ErrInvalid     = errors.New("pdf: invalid file")
	ErrUnsupported = errors.New("pdf: unsupported file")
	ErrPassword    = errors.New("pdf: wrong password")
)

ErrInvalid is returned for a file that is not a PDF or cannot be recovered, ErrUnsupported for a well formed file this package cannot handle.

Functions

This section is empty.

Types

type Array

type Array []Object

Array is a PDF array.

type BitReader

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

BitReader reads MSB-first fields.

func NewBitReader

func NewBitReader(buf []byte) *BitReader

NewBitReader reads from buf.

func (*BitReader) Read

func (b *BitReader) Read(n int) uint32

Read returns the next n bits, zero filled past the end of the buffer.

type Bool

type Bool bool

Bool is a PDF boolean.

type Dict

type Dict map[Name]Object

Dict is a PDF dictionary; a null value is a missing key, ISO 32000-1 7.3.9.

func (Dict) Get

func (d Dict) Get(key Name) Object

Get returns the value for key without following indirect references.

func (Dict) Keys

func (d Dict) Keys() []Name

Keys returns the dictionary keys in sorted order.

type File

type File struct {

	// StreamCacheBytes bounds the decoded stream data kept: zero is
	// DefaultStreamCacheBytes, negative is none.
	StreamCacheBytes int
	// contains filtered or unexported fields
}

File is an open PDF file.

func Load

func Load(buf []byte, password string) (*File, error)

Load reads a document from a buffer, which it takes ownership of.

func NewReader

func NewReader(r io.ReaderAt, size int64) (*File, error)

NewReader reads size bytes from r into memory.

func NewReaderPassword

func NewReaderPassword(r io.ReaderAt, size int64, password string) (*File, error)

NewReaderPassword reads size bytes from r, decrypting with password.

func Open

func Open(name string) (*File, error)

Open reads the named file.

func OpenPassword

func OpenPassword(name, password string) (*File, error)

OpenPassword reads the named file, which is encrypted with password.

func (*File) Catalog

func (f *File) Catalog() Dict

Catalog returns the document catalog, the root of the object graph.

func (*File) Close

func (f *File) Close() error

Close releases the file. It must not be called while anything is still reading it.

func (*File) DecodeImageData

func (f *File) DecodeImageData(dict Dict, data []byte) ([]byte, Name, Dict, error)

DecodeImageData applies the byte filters dict names to data and returns the bytes, the remaining image filter and its parameters.

func (*File) Err

func (f *File) Err() []error

Err returns the errors recorded while reading the file. It is safe to call while other goroutines are reading the same file.

func (*File) GetArray

func (f *File) GetArray(o Object) Array

GetArray resolves o and returns it as an array, or nil.

func (*File) GetBool

func (f *File) GetBool(o Object, def bool) bool

GetBool resolves o and returns it as a boolean, or def.

func (*File) GetBytes

func (f *File) GetBytes(o Object) []byte

GetBytes resolves o and returns it as a string, or nil.

func (*File) GetDict

func (f *File) GetDict(o Object) Dict

GetDict resolves o and returns it, or a stream's dictionary, as a dictionary, or nil.

func (*File) GetFloat

func (f *File) GetFloat(o Object, def float64) float64

GetFloat resolves o and returns it as a number, or def.

func (*File) GetFloats

func (f *File) GetFloats(o Object) []float64

GetFloats resolves o as an array of numbers.

func (*File) GetInt

func (f *File) GetInt(o Object, def int64) int64

GetInt resolves o and returns it as an integer, truncating a real, or def.

func (*File) GetName

func (f *File) GetName(o Object) Name

GetName resolves o and returns it as a name, or "".

func (*File) GetNames

func (f *File) GetNames(o Object) []Name

GetNames resolves o as either a single name or an array of names.

func (*File) GetStream

func (f *File) GetStream(o Object) *Stream

GetStream resolves o and returns it as a stream, or nil.

func (*File) Lookup

func (f *File) Lookup(dict Dict, keys ...Name) Object

Lookup resolves dict[key], trying each key in turn.

func (*File) NameTreeLookup

func (f *File) NameTreeLookup(root Object, key string) Object

NameTreeLookup finds a key in a name tree, ISO 32000-1 7.9.6, and returns nil for one that is not there.

func (*File) NumPages

func (f *File) NumPages() int

NumPages returns the number of pages.

func (*File) NumberTreeEach

func (f *File) NumberTreeEach(root Object, fn func(int64, Object) bool)

NumberTreeEach calls fn for every entry of a number tree in order, ISO 32000-1 7.9.7, and stops when fn returns false.

func (*File) Page

func (f *File) Page(i int) Dict

Page returns the dictionary of page i, counting from zero, with the inheritable attributes of its ancestors filled in.

func (*File) PageIndex

func (f *File) PageIndex(r Ref) int

PageIndex returns which page a reference names, and -1 for none.

func (*File) PageRef

func (f *File) PageRef(i int) Ref

PageRef returns the reference to page i, counting from zero.

func (*File) Repaired

func (f *File) Repaired() bool

Repaired reports whether the cross-reference table had to be rebuilt.

func (*File) Resolve

func (f *File) Resolve(o Object) Object

Resolve follows indirect references until it reaches a direct object.

func (*File) Trailer

func (f *File) Trailer() Dict

Trailer returns the document trailer dictionary.

type Integer

type Integer int64

Integer is a PDF integer.

type Keyword

type Keyword string

Keyword is a bare token: an operator, or one of the structural words such as obj, endobj, stream or R. It is not a PDF object type.

type Lexer

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

Lexer tokenizes PDF syntax. The whole file is in memory, so a token is a slice of it and scanning is a pointer walk.

func NewLexer

func NewLexer(buf []byte) *Lexer

NewLexer reads the tokens of a PDF out of a buffer. The content stream interpreter tokenizes with the same code, which is why this is exported.

func (*Lexer) Bytes

func (l *Lexer) Bytes() []byte

Bytes returns the buffer being scanned.

func (*Lexer) Errors

func (l *Lexer) Errors() []error

Errors returns what the scanner could not make sense of.

func (*Lexer) Next

func (l *Lexer) Next() (obj Object, ok bool)

Next returns the next token. ok is false at end of input.

func (*Lexer) SetPos

func (l *Lexer) SetPos(p int)

SetPos moves the scanner, clamped to the buffer. It is how the content interpreter steps over the binary data of an inline image.

type MQ

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

MQ decodes a decision at a time against a caller supplied context array, in which each entry packs the state index in its high seven bits and the more probable symbol in its low one. JBIG2 and JPEG 2000 both read it.

func NewMQ

func NewMQ(data []byte, start, end int) *MQ

NewMQ returns a decoder over data[start:end].

func (*MQ) ReadBit

func (d *MQ) ReadBit(contexts []uint8, pos uint32) int

ReadBit decodes one decision against contexts[pos].

type Name

type Name string

Name is a PDF name, without the leading slash and with #xx decoded.

type Object

type Object interface {
	// contains filtered or unexported methods
}

Object is a PDF object. The concrete types are Bool, Integer, Real, String, Name, Array, Dict, Ref and *Stream. A nil Object is the null object.

type Operand

type Operand struct {
	// Num is the value when Obj is nil.
	Num float64
	// Whole is the exact value of an integer, which Num only rounds once it
	// runs past what a float64 counts one by one.
	Whole int64
	// IsInt reports that the number was written without a fraction, which is
	// what tells an object number from a coordinate.
	IsInt bool
	// IsNum says the operand is the number rather than Obj. It is not the same
	// question as whether Obj is nil, because nil is the null object.
	IsNum bool
	// Obj is the token when it is anything but a number.
	Obj Object
}

An Operand is one token, a number left as a number rather than boxed into an Object, because a content stream is almost all numbers and boxing costs.

func (Operand) Number

func (o Operand) Number() bool

Number reports whether the operand is a number rather than another object.

func (Operand) Object

func (o Operand) Object() Object

Object boxes the operand.

type Parser

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

Parser turns tokens into objects with two tokens of lookahead.

func NewParser

func NewParser(l *Lexer, f *File) *Parser

NewParser reads objects from a lexer, resolving references against f, which may be nil.

func (*Parser) AllowStreams

func (p *Parser) AllowStreams(ok bool)

AllowStreams sets whether a stream keyword may follow a dictionary.

func (*Parser) Lexer

func (p *Parser) Lexer() *Lexer

Lexer returns the scanner the Parser reads from.

func (*Parser) Object

func (p *Parser) Object() (obj Object, ok bool)

Object reads one object. ok is false at end of input.

func (*Parser) Operand

func (p *Parser) Operand() (Operand, bool)

Operand reads one item of a content stream's operand list, a number unboxed.

func (*Parser) Pos

func (p *Parser) Pos() int

Pos returns where the token Object returned last begins.

func (*Parser) Refill

func (p *Parser) Refill()

Refill reloads both lookahead slots after the scanner position was moved by hand.

type Real

type Real float64

Real is a PDF real number.

type Ref

type Ref struct {
	Num uint32
	Gen uint16
}

Ref is an indirect reference.

func (Ref) String

func (r Ref) String() string

String formats the reference as it appears in a file.

type Stream

type Stream struct {
	Dict Dict
	Ref  Ref
	// contains filtered or unexported fields
}

Stream is a PDF stream: a dictionary and the bytes that follow it.

func (*Stream) Data

func (s *Stream) Data() ([]byte, error)

Data returns the fully decoded stream contents.

func (*Stream) ImageData

func (s *Stream) ImageData() (data []byte, filter Name, parms Dict, err error)

ImageData returns the stream contents with every byte filter applied, plus the image filter that remains and its parameters, if any.

func (*Stream) ImageDataLimit added in v0.2.8

func (s *Stream) ImageDataLimit(limit int) ([]byte, Name, Dict, error)

ImageDataLimit is ImageData decoding no more than limit bytes, and not cached.

func (*Stream) ImageReader added in v0.3.0

func (s *Stream) ImageReader(limit int) (io.ReadCloser, bool)

ImageReader is ImageDataLimit as a reader, for a stream whose last filter is FlateDecode or LZWDecode and that leaves no image filter: it holds the compressed bytes and no more. It declines any other stream, and one whose inflation fails before it yields a byte, which ImageDataLimit then reports.

func (*Stream) Raw

func (s *Stream) Raw() ([]byte, error)

Raw returns the stream contents as stored, decrypted but not decoded.

type String

type String []byte

String is a PDF byte string, already unescaped and decrypted.

Jump to

Keyboard shortcuts

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