pdf

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package pdf parses PDF file structure: the tokenizer, indirect objects, cross-reference tables and streams, object streams, and the page tree.

It exposes a read-only Document that, once opened, is safe to share across goroutines without locking. Higher layers (filters, the content interpreter, and the rasterizer) build on the objects this package returns.

Index

Constants

This section is empty.

Variables

View Source
var ErrEncrypted = errors.New("pdf: unsupported encryption")

ErrEncrypted is returned when a document uses an encryption scheme that is not supported: a non-Standard security handler, or a Standard handler with an unsupported /V or /R. Standard-handler documents readable with the empty user password are decrypted transparently; ones needing a real password return ErrEncryptedNeedsPassword instead.

View Source
var ErrEncryptedNeedsPassword = errors.New("pdf: document requires a password")

ErrEncryptedNeedsPassword is returned when a document uses the Standard Security Handler but the empty user password does not authenticate, i.e. a non-empty user password would be required to read it. Only empty-password decryption is supported.

Functions

func Debug

func Debug(o Object) string

Debug returns a human-readable representation of an object. It is not the PDF serialization; it is intended for logging and test output.

func IntValue

func IntValue(o Object) (int, bool)

IntValue returns the int value of an Integer (or a Real truncated to int) and reports whether the object was numeric.

func Number

func Number(o Object) (float64, bool)

Number returns the float64 value of an Integer or Real, and reports whether the object was numeric.

Types

type Array

type Array []Object

Array is a PDF array.

type Boolean

type Boolean bool

Boolean is a PDF boolean (true/false).

type ContentScanner

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

ContentScanner tokenizes a PDF content stream into operands and operators. It reuses the object grammar (numbers, strings, names, arrays, inline dicts) and reports bare keywords as operators. It is used by the content interpreter package; exposing it here avoids duplicating the lexer.

func NewContentScanner

func NewContentScanner(src []byte) *ContentScanner

NewContentScanner returns a scanner over a content stream's bytes.

func (*ContentScanner) Next

func (s *ContentScanner) Next() (obj Object, op string, ok bool, err error)

Next returns the next item. For an operand it returns (obj, "", true, nil). For an operator it returns (nil, opName, true, nil). At EOF it returns (nil, "", false, nil). Operators true/false/null are returned as operand objects (Boolean/Null), matching their meaning in content streams.

func (*ContentScanner) ReadInlineImage

func (s *ContentScanner) ReadInlineImage() (Dict, []byte, error)

ReadInlineImage parses the body of an inline image after the BI operator has been returned by Next. It reads the abbreviated key/value pairs up to the ID keyword, then captures the raw sample bytes up to the EI delimiter, and leaves the scanner positioned to continue with the operators that follow.

The returned dict uses the keys as written (abbreviated, e.g. /W, /H, /CS, /F, /BPC, /IM, /D); the caller is responsible for normalizing them. The data is the verbatim (still-encoded) bytes between the single whitespace after ID and the EI delimiter.

Per the spec the sample data starts after exactly one whitespace byte following ID and ends at EI preceded by whitespace and followed by whitespace or EOF. Because the data is arbitrary binary, EI is only honored at a token boundary (whitespace/delimiter on both sides), so a literal "EI" inside the samples does not end the image prematurely.

type Dict

type Dict map[Name]Object

Dict is a PDF dictionary. Keys are name objects without the leading slash.

type Document

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

Document is a parsed PDF. After Open returns, a Document is read-only and safe for concurrent use by multiple goroutines.

func Open

func Open(path string) (*Document, error)

Open reads and parses a PDF file from disk.

func Parse

func Parse(data []byte) (*Document, error)

Parse parses a PDF from an in-memory byte slice. The slice is retained by the returned Document and must not be modified by the caller.

func (*Document) DecodedStream

func (d *Document) DecodedStream(s *Stream) (data []byte, imageFilter string, err error)

DecodedStream returns the fully decoded bytes of a stream, applying its filter chain. Image-only filters (e.g. DCTDecode) are left to the caller; in that case DecodedStream returns the raw bytes together with the remaining image filter name.

func (*Document) GetArray

func (d *Document) GetArray(o Object) Array

GetArray resolves o to an Array, returning nil if it is not an array.

func (*Document) GetDict

func (d *Document) GetDict(o Object) Dict

GetDict resolves o to a Dict (or a Stream's dict), returning nil if it is not a dictionary.

func (*Document) GetInt

func (d *Document) GetInt(o Object) (int, bool)

GetInt resolves o to an integer value.

func (*Document) GetName

func (d *Document) GetName(o Object) (Name, bool)

GetName resolves o to a Name, returning ("", false) if it is not a name.

func (*Document) GetStream

func (d *Document) GetStream(o Object) *Stream

GetStream resolves o to a *Stream, returning nil if it is not a stream.

func (*Document) Page

func (d *Document) Page(i int) (*Page, error)

Page returns the page at the given zero-based index.

func (*Document) PageCount

func (d *Document) PageCount() int

PageCount returns the number of pages in the document.

func (*Document) Resolve

func (d *Document) Resolve(o Object) Object

Resolve follows indirect references until it reaches a direct object. Direct objects are returned unchanged. A missing object resolves to Null.

func (*Document) Trailer

func (d *Document) Trailer() Dict

Trailer returns the document trailer dictionary.

type Integer

type Integer int64

Integer is a PDF integer.

type Name

type Name string

Name is a PDF name object (e.g. /Type). The leading slash is not stored and name escapes (#xx) are already decoded.

type Null

type Null struct{}

Null is the PDF null object.

type Object

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

Object is any PDF object value. Concrete types are: Null, Boolean, Integer, Real, String, Name, Array, Dict, Stream, and Reference.

type Page

type Page struct {
	MediaBox  Rectangle
	CropBox   Rectangle
	Rotate    int  // normalized to 0, 90, 180, or 270
	Resources Dict // inherited resource dictionary
	// contains filtered or unexported fields
}

Page is a single resolved page with its inherited attributes.

func (*Page) ContentBytes

func (p *Page) ContentBytes() ([]byte, error)

ContentBytes returns the concatenated, fully decoded content streams of the page. Multiple content streams are joined with a single space, per the spec.

func (*Page) Dict

func (p *Page) Dict() Dict

Dict returns the page's own dictionary.

func (*Page) Doc

func (p *Page) Doc() *Document

Doc returns the document this page belongs to.

type Real

type Real float64

Real is a PDF real (floating-point) number.

type Rectangle

type Rectangle struct {
	LLX, LLY, URX, URY float64
}

Rectangle is a PDF rectangle in default user space units (points).

func (Rectangle) Height

func (r Rectangle) Height() float64

Height returns the rectangle height.

func (Rectangle) Width

func (r Rectangle) Width() float64

Width returns the rectangle width.

type Reference

type Reference struct {
	Number     int // object number
	Generation int // generation number
}

Reference is an indirect object reference (e.g. "12 0 R").

func (Reference) String

func (r Reference) String() string

type Stream

type Stream struct {
	Dict Dict
	Raw  []byte
}

Stream is a PDF stream object: a dictionary plus raw (still-encoded) bytes. Use the filter package together with the dictionary's Filter entry to decode the contents.

type String

type String string

String is a PDF string. The bytes are the decoded value (after resolving literal escapes or hex encoding); they are not necessarily valid UTF-8.

Directories

Path Synopsis
Package content interprets a PDF page content stream.
Package content interprets a PDF page content stream.
Package extract reconstructs document structure (paragraphs, headings, lists, tables) from a PDF's positioned glyphs and vector graphics, then builds a *cssbox.Box tree the existing conversion writers turn into Markdown/HTML.
Package extract reconstructs document structure (paragraphs, headings, lists, tables) from a PDF's positioned glyphs and vector graphics, then builds a *cssbox.Box tree the existing conversion writers turn into Markdown/HTML.
Package filter decodes PDF stream data.
Package filter decodes PDF stream data.
jbig2
Package jbig2 一个高性能、零依赖的纯 Go 语言 JBIG2 解码器
Package jbig2 一个高性能、零依赖的纯 Go 语言 JBIG2 解码器
Package function implements the PDF Function objects of ISO 32000-1 §7.10.
Package function implements the PDF Function objects of ISO 32000-1 §7.10.
Package pageres resolves the page-/Resources entries the raster and extract backends share: fonts, form XObjects, and their /Matrix and /BBox.
Package pageres resolves the page-/Resources entries the raster and extract backends share: fonts, form XObjects, and their /Matrix and /BBox.

Jump to

Keyboard shortcuts

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