Version: v1.1.2-0...-67596a8 Latest Latest

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

Go to latest
Published: Dec 9, 2021 License: 0BSD Imports: 16 Imported by: 1



Package pdf signs PDF documents and provides some processing utilities.



This section is empty.


This section is empty.


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.


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 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

	// simple tokens

	// higher-level objects

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.

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) 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.

Source Files

Jump to

Keyboard shortcuts

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