reader

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: MIT Imports: 15 Imported by: ? modules, 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 reader parses existing PDF files and exposes basic page metadata, content streams, text extraction, and merge conveniences.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCannotReadFile is returned when Load cannot read a PDF from disk.
	ErrCannotReadFile = errors.New("reader: cannot read file")
	// ErrEncryptedUnsupported is returned for encrypted PDFs.
	ErrEncryptedUnsupported = errors.New("reader: encrypted PDFs are not supported")
	// ErrPageOutOfRange is returned when Page receives an invalid index.
	ErrPageOutOfRange = errors.New("reader: page index out of range")
	// ErrUnsupportedPDF is returned for PDF constructs not supported by this foundation parser.
	ErrUnsupportedPDF = errors.New("reader: unsupported PDF")
)

Functions

This section is empty.

Types

type Box

type Box struct {
	X1, Y1, X2, Y2 float64
}

Box represents a PDF rectangle in points: lower-left (X1,Y1) to upper-right (X2,Y2).

func (Box) Height

func (b Box) Height() float64

Height returns the box height.

func (Box) IsZero

func (b Box) IsZero() bool

IsZero reports whether the box is unset.

func (Box) Width

func (b Box) Width() float64

Width returns the box width.

type Modifier

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

Modifier contains the bytes produced by reader operations such as Merge.

func ExtractPages

func ExtractPages(r *PdfReader, pageIndexes ...int) (*Modifier, error)

ExtractPages copies selected zero-based pages from r into a new PDF. The output order follows pageIndexes.

func Merge

func Merge(readers ...*PdfReader) (*Modifier, error)

Merge concatenates parsed PDFs using Paper's existing merge engine.

func MergeFiles

func MergeFiles(paths ...string) (*Modifier, error)

MergeFiles loads and merges PDF files from disk.

func RedactPattern

func RedactPattern(r *PdfReader, pattern *regexp.Regexp, opts *RedactOptions) (*Modifier, error)

RedactPattern permanently removes regex matches from uncompressed page content streams by replacing matched bytes with spaces.

func RedactText

func RedactText(r *PdfReader, targets []string, opts *RedactOptions) (*Modifier, error)

RedactText permanently removes literal text targets from uncompressed page content streams by replacing matched bytes with spaces.

func (*Modifier) AddBlankPage

func (m *Modifier) AddBlankPage(width, height float64) error

AddBlankPage appends one blank page with dimensions in PDF points.

func (*Modifier) Bytes

func (m *Modifier) Bytes() []byte

Bytes returns a copy of the modified PDF bytes.

func (*Modifier) CropPage

func (m *Modifier) CropPage(index int, rect [4]float64) error

CropPage sets the zero-based page CropBox.

func (*Modifier) PageCount

func (m *Modifier) PageCount() int

PageCount returns the expected page count for the modified PDF.

func (*Modifier) RemovePage

func (m *Modifier) RemovePage(index int) error

RemovePage removes the zero-based page at index.

func (*Modifier) ReorderPages

func (m *Modifier) ReorderPages(order []int) error

ReorderPages rearranges pages according to order, a zero-based permutation.

func (*Modifier) RotatePage

func (m *Modifier) RotatePage(index, degrees int) error

RotatePage sets the zero-based page rotation. Degrees must be a multiple of 90.

func (*Modifier) SaveTo

func (m *Modifier) SaveTo(path string) error

SaveTo writes the modified PDF to path.

type PageInfo

type PageInfo struct {
	Number int
	Width  float64
	Height float64
	Rotate int

	MediaBox Box
	CropBox  Box
	BleedBox Box
	TrimBox  Box
	ArtBox   Box
	// contains filtered or unexported fields
}

PageInfo holds parsed metadata for one PDF page.

func (*PageInfo) ContentStream

func (p *PageInfo) ContentStream() ([]byte, error)

ContentStream returns the decoded page content stream bytes.

func (*PageInfo) ExtractText

func (p *PageInfo) ExtractText() (string, error)

ExtractText extracts simple text operators from decoded page content streams.

func (*PageInfo) VisibleBox

func (p *PageInfo) VisibleBox() Box

VisibleBox returns the effective visible page box.

type PdfReader

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

PdfReader holds parsed state for an existing PDF.

func Load

func Load(path string) (*PdfReader, error)

Load reads and parses a PDF file from disk.

func Parse

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

Parse reads and parses a PDF byte slice.

func ParseWithOptions

func ParseWithOptions(data []byte, opts ReadOptions) (*PdfReader, error)

ParseWithOptions reads and parses a PDF byte slice with custom options.

func (*PdfReader) Page

func (r *PdfReader) Page(index int) (*PageInfo, error)

Page returns page metadata by zero-based index.

func (*PdfReader) PageCount

func (r *PdfReader) PageCount() int

PageCount returns the number of pages in the document.

func (*PdfReader) RawBytes

func (r *PdfReader) RawBytes() []byte

RawBytes returns a copy of the parsed PDF bytes.

func (*PdfReader) Version

func (r *PdfReader) Version() string

Version returns the PDF version from the header.

type ReadOptions

type ReadOptions struct {
	Strictness Strictness
}

ReadOptions configures PDF parsing.

type RedactOptions

type RedactOptions struct {
	FillColor       [3]float64
	OverlayText     string
	OverlayFontSize float64
	OverlayColor    [3]float64

	// StripMetadata empties the document information dictionary and any
	// uncompressed XMP /Metadata stream. A compressed metadata stream is
	// reported as an error instead of being left in the output.
	StripMetadata bool
}

RedactOptions configures redaction behavior.

This foundation performs byte-preserving text removal and, with StripMetadata, byte-preserving metadata removal. The visual overlay fields are kept for API compatibility with richer redaction flows and are not drawn.

type Strictness

type Strictness int

Strictness controls how the reader handles unsupported stream filters.

const (
	// StrictnessTolerant preserves undecodable streams as raw data where possible.
	StrictnessTolerant Strictness = iota
	// StrictnessStrict fails when a page content stream uses an unsupported filter.
	StrictnessStrict
)

Jump to

Keyboard shortcuts

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