importpdf

package
v0.0.0-...-f985856 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2026 License: MPL-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package importpdf parses pages from existing PDFs into renderer-independent page references for inspection and reconstruction workflows.

Index

Constants

View Source
const (
	MaxArrayItems         = 10000
	MaxDecodedStreamBytes = 32 * 1024 * 1024
	MaxDictEntries        = 10000
	MaxPages              = 10000
	MaxPageContentBytes   = 32 * 1024 * 1024
	MaxPageTreeDepth      = 512
	MaxReferencedObjects  = 10000
	MaxXrefEntries        = 100000
	MaxXrefChainLength    = 128
	MaxValueNesting       = 128
)
View Source
const MaxSourceBytes = 128 * 1024 * 1024

MaxSourceBytes is the largest PDF source accepted by the built-in importer.

Variables

View Source
var ErrSourceTooLarge = errors.New("PDF import source exceeds maximum size")

ErrSourceTooLarge reports that a PDF source exceeded the configured byte limit.

Functions

func GetPageSizes

func GetPageSizes(source any) (map[int]map[string]Size, error)

GetPageSizes returns available page box sizes for a PDF source. Sizes are reported in PDF points.

func RewriteIndirectRefs

func RewriteIndirectRefs(data []byte, refMap map[ObjRef]int) []byte

RewriteIndirectRefs rewrites indirect object references found outside stream bodies according to refMap.

Types

type ImportOptions

type ImportOptions struct {
	MaxSourceBytes       int64
	MaxReferencedObjects int
}

ImportOptions controls parser limits for the built-in PDF importer. Zero fields use package defaults.

type ObjRef

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

func (ObjRef) Generation

func (r ObjRef) Generation() int

Generation returns the referenced PDF indirect object generation number.

func (ObjRef) ObjectNumber

func (r ObjRef) ObjectNumber() int

ObjectNumber returns the referenced PDF indirect object number.

func (ObjRef) String

func (r ObjRef) String() string

String formats the reference as "object generation".

type Object

type Object struct {
	Ref  ObjRef
	Body []byte
}

Object is an indirect object referenced by an imported page.

type PageRef

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

PageRef is a parsed PDF page ready to be embedded by a renderer.

func (*PageRef) Content

func (p *PageRef) Content() []byte

Content returns a copy of the imported page content stream bytes. Use ContentErr to check whether lazy content loading failed.

func (*PageRef) ContentBorrowedWithContext

func (p *PageRef) ContentBorrowedWithContext(ctx context.Context) ([]byte, error)

ContentBorrowedWithContext returns the page content without copying it. The returned slice is owned by PageRef and must not be retained or modified after the PageRef is no longer in use.

func (*PageRef) ContentErr

func (p *PageRef) ContentErr() error

ContentErr reports the lazy content-loading error, if any.

func (*PageRef) ContentWithContext

func (p *PageRef) ContentWithContext(ctx context.Context) ([]byte, error)

ContentWithContext returns a copy of the imported page content stream bytes, checking ctx while lazy content is loaded.

func (*PageRef) ContentWithError

func (p *PageRef) ContentWithError() ([]byte, error)

ContentWithError returns a copy of the imported page content stream bytes and reports lazy content-loading errors directly.

func (*PageRef) EncodedContent

func (p *PageRef) EncodedContent() ([]byte, string, bool)

EncodedContent returns a preserved encoded content stream and its PDF filter name when the imported page can be embedded without re-encoding.

func (*PageRef) ForEachObject

func (p *PageRef) ForEachObject(fn func(ObjRef, []byte) error) error

ForEachObject calls fn for each imported object in sorted reference order. The body slice passed to fn is a copy. Use ForEachObjectBorrowed only for performance-sensitive internal code that can honor borrowed-slice semantics.

func (*PageRef) ForEachObjectBorrowed

func (p *PageRef) ForEachObjectBorrowed(fn func(ObjRef, []byte) error) error

ForEachObjectBorrowed calls fn for each imported object in sorted reference order. The body slice is owned by PageRef and must not be retained or modified.

func (*PageRef) ForEachObjectCopy

func (p *PageRef) ForEachObjectCopy(fn func(ObjRef, []byte) error) error

ForEachObjectCopy calls fn for each imported object in sorted reference order. The body slice passed to fn is a copy.

func (*PageRef) HeightPoints

func (p *PageRef) HeightPoints() float64

HeightPoints returns the imported page height in PDF points.

func (*PageRef) ObjectCount

func (p *PageRef) ObjectCount() int

ObjectCount returns the number of indirect objects referenced by the imported page.

func (*PageRef) ObjectRefs

func (p *PageRef) ObjectRefs() []ObjRef

ObjectRefs returns imported object references sorted by object number and generation.

func (*PageRef) Objects

func (p *PageRef) Objects() []Object

Objects returns the indirect objects referenced by the imported page, sorted by object number and generation.

func (*PageRef) Resources

func (p *PageRef) Resources() []byte

Resources returns a copy of the imported page resource dictionary bytes.

func (*PageRef) ResourcesBorrowed

func (p *PageRef) ResourcesBorrowed() []byte

ResourcesBorrowed returns the page resource dictionary bytes without copying them. The returned slice is owned by PageRef and must not be retained or modified after the PageRef is no longer in use.

func (*PageRef) WidthPoints

func (p *PageRef) WidthPoints() float64

WidthPoints returns the imported page width in PDF points.

type Size

type Size struct {
	Wd float64
	Ht float64
}

Size reports a PDF page box width and height in points.

type Source

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

func Open

func Open(source any) (*Source, error)

Open parses a PDF source. source may be a file path string, []byte, or io.Reader.

func OpenBytes

func OpenBytes(data []byte) (*Source, error)

OpenBytes parses PDF bytes.

func OpenBytesImmutable

func OpenBytesImmutable(data []byte) (*Source, error)

OpenBytesImmutable parses PDF bytes without copying them. The caller must not mutate data while the returned Source is in use.

func OpenBytesImmutableWithOptions

func OpenBytesImmutableWithOptions(data []byte, options ImportOptions) (*Source, error)

OpenBytesImmutableWithOptions parses PDF bytes without copying them using explicit parser limits. The caller must not mutate data while the returned Source is in use.

func OpenBytesImmutableWithOptionsContext

func OpenBytesImmutableWithOptionsContext(ctx context.Context, data []byte, options ImportOptions) (*Source, error)

OpenBytesImmutableWithOptionsContext parses PDF bytes without copying them using explicit parser limits and checks ctx before and after parsing. The caller must not mutate data while the returned Source is in use.

func OpenBytesWithOptions

func OpenBytesWithOptions(data []byte, options ImportOptions) (*Source, error)

OpenBytesWithOptions parses PDF bytes using explicit parser limits.

func OpenBytesWithOptionsContext

func OpenBytesWithOptionsContext(ctx context.Context, data []byte, options ImportOptions) (*Source, error)

OpenBytesWithOptionsContext parses PDF bytes using explicit parser limits and checks ctx before copying and after parsing.

func OpenFile

func OpenFile(path string) (*Source, error)

OpenFile parses a PDF file.

func OpenFileWithOptions

func OpenFileWithOptions(path string, options ImportOptions) (*Source, error)

OpenFileWithOptions parses a PDF file using explicit parser limits.

func OpenFileWithOptionsContext

func OpenFileWithOptionsContext(ctx context.Context, path string, options ImportOptions) (*Source, error)

OpenFileWithOptionsContext parses a PDF file using explicit parser limits and checks ctx before opening and after parsing.

func OpenReader

func OpenReader(r io.Reader) (*Source, error)

OpenReader reads and parses a PDF stream.

func OpenReaderAt

func OpenReaderAt(r io.ReaderAt, size int64) (*Source, error)

OpenReaderAt parses a seekable PDF source without copying the whole file. The caller must keep r readable while the returned Source is used.

func OpenReaderAtWithOptions

func OpenReaderAtWithOptions(r io.ReaderAt, size int64, options ImportOptions) (*Source, error)

OpenReaderAtWithOptions parses a seekable PDF source using explicit parser limits. The caller must keep r readable while the returned Source is used.

func OpenReaderAtWithOptionsContext

func OpenReaderAtWithOptionsContext(ctx context.Context, r io.ReaderAt, size int64, options ImportOptions) (*Source, error)

OpenReaderAtWithOptionsContext parses a seekable PDF source using explicit parser limits and checks ctx before and after parsing. The caller must keep r readable while the returned Source is used.

func OpenReaderWithOptions

func OpenReaderWithOptions(r io.Reader, options ImportOptions) (*Source, error)

OpenReaderWithOptions reads and parses a PDF stream using explicit parser limits.

func OpenReaderWithOptionsContext

func OpenReaderWithOptionsContext(ctx context.Context, r io.Reader, options ImportOptions) (*Source, error)

OpenReaderWithOptionsContext reads and parses a PDF stream using explicit parser limits and checks ctx before and during bounded reads.

func OpenWithOptions

func OpenWithOptions(source any, options ImportOptions) (*Source, error)

OpenWithOptions parses a PDF source using explicit parser limits. source may be a file path string, []byte, io.Reader, or *Source.

func OpenWithOptionsContext

func OpenWithOptionsContext(ctx context.Context, source any, options ImportOptions) (*Source, error)

OpenWithOptionsContext parses a PDF source using explicit parser limits and checks ctx before and after bounded reads/parsing.

func (*Source) ForEachObjectBorrowedContext

func (doc *Source) ForEachObjectBorrowedContext(ctx context.Context, fn func(ObjRef, []byte) error) error

ForEachObjectBorrowedContext calls fn for every in-use classic-xref object in file-offset order. Object bodies are immutable slices owned by Source; callers must not modify or retain them after Source is no longer in use. This low-level traversal is intended for inspection and reconstruction code that must account for objects outside the page-reachable graph.

func (*Source) Page

func (doc *Source) Page(pageNo int, boxName string) (*PageRef, error)

func (*Source) PageContext

func (doc *Source) PageContext(ctx context.Context, pageNo int, boxName string) (*PageRef, error)

PageContext returns an imported page and checks ctx while resolving page resources, content streams, and referenced objects.

func (*Source) PageCount

func (doc *Source) PageCount() int

PageCount returns the number of importable pages in the source.

func (*Source) PageSizes

func (doc *Source) PageSizes() map[int]map[string]Size

type SourceCache

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

SourceCache stores parsed PDF sources for reuse across documents.

func NewSourceCache

func NewSourceCache() *SourceCache

NewSourceCache creates an empty reusable PDF import source cache.

func NewSourceCacheWithMaxBytes

func NewSourceCacheWithMaxBytes(maxBytes int64) *SourceCache

NewSourceCacheWithMaxBytes creates an empty reusable PDF import source cache with a byte budget based on source file sizes. Files larger than maxBytes are parsed successfully but not cached.

func (*SourceCache) OpenFile

func (c *SourceCache) OpenFile(path string) (*Source, error)

OpenFile returns a parsed source for path, reusing a cached parse when the path, size, and modification time match.

Jump to

Keyboard shortcuts

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