pdfscan

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package pdfscan reads a PDF back: its objects, its pages, and the text, paths and images its content streams draw.

It exists so that a rendered document can be checked against the printout it came from -- glyph positions, line widths, link rectangles, outline structure -- which no printout-level test can see. It is a verification tool, deliberately independent of the writer: it parses the cross reference table rather than trusting object order, and it recovers text through each font's own ToUnicode map rather than through anything the writer remembers.

It reads the subset of PDF this project writes. It is not a general reader: no encryption, no object streams, no filters but FlateDecode.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Text

func Text(object Object) string

Text renders an object the way a diagnostic wants to see it.

Types

type Annot

type Annot struct {
	Subtype                  string
	Left, Top, Width, Height float64
	URI                      string
	// DestPage is the index of the page a destination points at,
	// -1 for a link that has none.
	DestPage          int
	DestLeft, DestTop float64
	Contents          string
}

Annot is a link annotation.

type Array

type Array []Object

Array is a PDF array.

type Dict

type Dict map[Name]Object

Dict is a PDF dictionary.

type Draw

type Draw struct {
	Name                     string
	Left, Top, Width, Height float64
}

Draw is an XObject placement, with the box the matrix maps it onto.

type File

type File struct {
	Trailer Dict
	// contains filtered or unexported fields
}

File is a parsed PDF.

func Read

func Read(raw []byte) (*File, error)

Read parses a file's cross reference table and trailer.

func (*File) Catalog

func (file *File) Catalog() Dict

Catalog is the document catalog.

func (*File) Data

func (file *File) Data(stream *Stream) ([]byte, error)

Data decodes a stream's contents, undoing FlateDecode.

func (*File) Fonts

func (file *File) Fonts(page Dict) (map[string]*Font, error)

Fonts reads every font a page's resources offer, which is what a test comparing the file's widths against the original tables needs.

func (*File) Get

func (file *File) Get(dict Dict, key Name) Object

Get resolves a dictionary entry.

func (*File) Info

func (file *File) Info() Dict

Info is the document information dictionary, absent as nil.

func (*File) Number

func (file *File) Number(dict Dict, key Name) (float64, bool)

Number reads a dictionary entry as a number.

func (*File) PageContent

func (file *File) PageContent(page Dict) ([]byte, error)

PageContent returns a page's decoded content stream.

func (*File) Pages

func (file *File) Pages() []Dict

Pages returns the page dictionaries in document order.

func (*File) ReadPages

func (file *File) ReadPages() ([]*Page, error)

ReadPages replays every page's content stream.

func (*File) Resolve

func (file *File) Resolve(object Object) Object

Resolve follows an indirect reference, as many times as it takes.

type Font

type Font struct {
	// Resource is the name a content stream selects it by.
	Resource string
	// BaseFont is the /BaseFont name, subset tag included.
	BaseFont string
	// Composite reports Identity-H encoding, whose codes are two bytes.
	Composite bool
	// Widths are in thousandths of an em, by character code.
	Widths map[uint16]float64
	// Unicode maps a code to the characters it stands for.
	Unicode map[uint16]string
	// Descriptor is the font descriptor dictionary.
	Descriptor Dict
	// FontFile is the embedded font program.
	FontFile []byte
}

Font is a font as the file describes it: the widths a reader advances the pen by, and the map back from codes to characters.

type Name

type Name string

Name is a PDF name, without its leading slash.

type Object

type Object any

Object is any PDF value: nil, bool, float64, String, Name, Ref, Array, Dict, or *Stream.

type Page

type Page struct {
	Number        int
	Width, Height float64
	Runs          []Run
	Rects         []Rect
	Segments      []Segment
	Draws         []Draw
	Annots        []Annot
}

Page is one page as read back.

func (*Page) Text

func (page *Page) Text() string

Text joins a page's runs, which is the whole of what a reader would select on it.

type Rect

type Rect struct {
	Left, Top, Width, Height float64
	// Painted is "fill", "stroke", "fillstroke", or "clip".
	Painted   string
	Fill      [3]float64
	Stroke    [3]float64
	LineWidth float64
	Dash      []float64
}

Rect is a rectangle the content stream painted.

type Ref

type Ref struct{ Num int }

Ref is an indirect reference.

type Run

type Run struct {
	Font     string
	Size     float64
	Left     float64
	Baseline float64
	Text     string
	// Width is the advance the file itself says the string has, summed
	// from the font dictionary's W array. It is what a reader would move
	// the pen by, which is not necessarily what the engine measured.
	Width float64
}

Run is one shown string, with where the reader's pen was when it was shown and how far the string advances it.

Left and Baseline are in the printout's coordinates -- origin top left, Y down -- so they compare directly against a text mark.

type Segment

type Segment struct {
	FromLeft, FromTop float64
	ToLeft, ToTop     float64
	Stroke            [3]float64
	LineWidth         float64
	Dash              []float64
}

Segment is a stroked straight line.

type Stream

type Stream struct {
	Dict Dict
	Raw  []byte
}

Stream is a dictionary with data attached.

type String

type String []byte

String is a PDF string, after escape processing.

Jump to

Keyboard shortcuts

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