pdf

package
v1.1.2-0...-147b880 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2024 License: 0BSD Imports: 18 Imported by: 1

Documentation

Overview

Package pdf signs PDF documents and provides some processing utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FillInSignature

func FillInSignature(document []byte, signOff, signLen int,
	key crypto.PrivateKey, certs []*x509.Certificate) error

FillInSignature signs PDF contents and writes the signature into the given window that has been reserved for this specific purpose. This is a very low-level function.

func PKCS12Parse

func PKCS12Parse(p12 []byte, password string) (
	crypto.PrivateKey, []*x509.Certificate, error)

PKCS12Parse parses and verifies PKCS#12 data.

func Sign

func Sign(document []byte, key crypto.PrivateKey, certs []*x509.Certificate,
	reservation int) ([]byte, error)

Sign signs the given document, growing and returning the passed-in slice. There must be at least one certificate, matching the private key. The certificates must form a chain.

A good default for the reservation is around 4096 (the value is in bytes).

The presumption here is that the document is valid and that it doesn't employ cross-reference streams from PDF 1.5, or at least constitutes a hybrid-reference file. The results with PDF 2.0 (2017) are currently unknown as the standard costs money.

Types

type BytesWriter

type BytesWriter interface {
	Bytes() []byte
	Len() int
	Write(p []byte) (n int, err error)
	WriteByte(c byte) error
	WriteRune(r rune) (n int, err error)
	WriteString(s string) (n int, err error)
}

BytesWriter is an interface over a subset of bytes.Buffer methods.

type Lexer

type Lexer struct {
	P []byte // input buffer
}

Lexer is a basic lexical analyser for the Portable Document Format, giving limited error information.

func (*Lexer) Next

func (lex *Lexer) Next() (Object, error)

type Object

type Object struct {
	Kind ObjectKind

	String        string            // Comment/Keyword/Name/String
	Number        float64           // Bool, Numeric
	Array         []Object          // Array, Indirect
	Dict          map[string]Object // Dict, Stream
	Stream        []byte            // Stream
	N, Generation uint              // Indirect, Reference
}

Object is a PDF token/object thingy. Objects may be composed either from one or a sequence of tokens. The PDF Reference doesn't actually speak of tokens, though ISO 32000-1:2008 does.

func New

func New(kind ObjectKind) Object

New returns a new Object of the given kind, with default values.

func NewArray

func NewArray(a []Object) Object

func NewBool

func NewBool(b bool) Object

func NewComment

func NewComment(c string) Object

func NewDate

func NewDate(ts time.Time) Object

NewDate makes a PDF object representing the given point in time.

func NewDict

func NewDict(d map[string]Object) Object

func NewIndirect

func NewIndirect(o Object, n, generation uint) Object

func NewKeyword

func NewKeyword(k string) Object

func NewName

func NewName(n string) Object

func NewNumeric

func NewNumeric(n float64) Object

func NewReference

func NewReference(n, generation uint) Object

func NewStream

func NewStream(d map[string]Object, s []byte) Object

func NewString

func NewString(s string) Object

func (*Object) IsInteger

func (o *Object) IsInteger() bool

IsInteger checks if the PDF object is an integer number.

func (*Object) IsUint

func (o *Object) IsUint() bool

IsUint checks if the PDF object is an integer number that fits into a uint.

func (*Object) Serialize

func (o *Object) Serialize() string

FIXME: Lines /should not/ be longer than 255 characters, some wrapping is in order.

type ObjectKind

type ObjectKind int
const (
	End ObjectKind = iota
	NL
	Comment
	Nil
	Bool
	Numeric
	Keyword
	Name
	String

	// simple tokens
	BArray
	EArray
	BDict
	EDict

	// higher-level objects
	Array
	Dict
	Stream
	Indirect
	Reference
)

type Updater

type Updater struct {

	// PDF document data
	Document []byte

	// the new trailer dictionary to be written, initialized with the old one
	Trailer map[string]Object
	// contains filtered or unexported fields
}

Updater is a utility class to help read and possibly incrementally update PDF files.

func NewUpdater

func NewUpdater(document []byte) (*Updater, error)

NewUpdater initializes an Updater, building the cross-reference table and preparing a new trailer dictionary.

func (*Updater) Allocate

func (u *Updater) Allocate() uint

Allocate allocates a new object number.

func (*Updater) Dereference

func (u *Updater) Dereference(o Object) (Object, error)

Derefence dereferences Reference objects, and passes the other kinds through.

func (*Updater) FlushUpdates

func (u *Updater) FlushUpdates()

FlushUpdates writes an updated cross-reference table and trailer, or stream.

func (*Updater) Get

func (u *Updater) Get(n, generation uint) (Object, error)

Get retrieves an object by its number and generation--may return Nil or End with an error.

func (*Updater) GetFirstPage

func (u *Updater) GetFirstPage(node Object) Object

GetFirstPage retrieves the first page of the given page (sub)tree reference, or returns a Nil object if unsuccessful.

func (*Updater) GetStreamData

func (u *Updater) GetStreamData(stream Object) ([]byte, error)

GetStreamData returns the actual data stored in a stream object, applying any filters.

func (*Updater) ListIndirect

func (u *Updater) ListIndirect() []Object

ListIndirect returns the whole cross-reference table as Reference Objects.

func (*Updater) Update

func (u *Updater) Update(n uint, fill func(buf BytesWriter))

Update appends an updated object to the end of the document. The fill callback must write exactly one PDF object.

func (*Updater) Version

func (u *Updater) Version(root *Object) int

Version extracts the claimed PDF version as a positive decimal number, e.g. 17 for PDF 1.7. Returns zero on failure.

Jump to

Keyboard shortcuts

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