pdf

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2024 License: GPL-3.0 Imports: 33 Imported by: 1

Documentation

Overview

Package pdf provides support for reading and writing PDF files.

This package treats PDF files as containers containing a sequence of objects (typically Dictionaries and Streams). Object are written sequentially, but can be read in any order. These objects represent pages of text, fonts, images and so on. Subpackages implement support to produce PDF files representing pages of text and images.

A Reader can be used to read objects from an existing PDF file:

r, err := pdf.Open("in.pdf")
if err != nil {
    log.Fatal(err)
}
defer r.Close()
... use r.Catalog to locate objects in the file ...

A Writer can be used to write objects to a new PDF file:

w, err := pdf.Create("out.pdf")
if err != nil {
    log.Fatal(err)
}

... add objects to the document using w.Write() and w.OpenStream() ...

w.Catalog.Pages = ... // set the page tree

err = out.Close()
if err != nil {
    log.Fatal(err)
}

The following classes represent the native PDF object types: Array, [Bool], Dict, Integer, Name, Real, Reference, *Stream, String. All of these implement the Object interface.

Index

Constants

View Source
const (
	// ErrorHandlingRecover means that the reader will try to recover from
	// errors and continue parsing the file.  This is the default.
	//
	// This guarantees that the reader will return a valid Catalog object,
	// with a non-null Pages field.
	ErrorHandlingRecover = iota

	// ErrorHandlingReport means that the reader will try to recover from
	// errors and continue parsing the file, but will report errors to the
	// caller.
	//
	// This mode tolerates more errors than ErrorHandlingRecover does.
	// In particular, it is not guaranteed that there are any pages in the
	// document.
	ErrorHandlingReport

	// ErrorHandlingStop means that the reader will stop parsing the file as
	// soon as an error is encountered.
	ErrorHandlingStop
)

Variables

View Source
var (
	GetArray   = resolveAndCast[Array]
	GetBoolean = resolveAndCast[Boolean]
	GetDict    = resolveAndCast[Dict]
	GetInteger = resolveAndCast[Integer]
	GetName    = resolveAndCast[Name]
	GetReal    = resolveAndCast[Real]
	GetStream  = resolveAndCast[*Stream]
	GetString  = resolveAndCast[String]
)

Helper functions for getting objects of a specific type. Each of these functions calls Resolve on the object before attempting to convert it to the desired type. If the object is `null`, a zero object is returned witout error. If the object is of the wrong type, an error is returned.

The signature of these functions is

func GetT(r Getter, obj Object) (x T, err error)

where T is the type of the object to be returned.

Functions

func CheckVersion added in v0.3.3

func CheckVersion(pdf Putter, operation string, minVersion Version) error

CheckVersion checks whether the PDF file being written has version minVersion or later. If the version is new enough, nil is returned. Otherwise a VersionError for the given operation is returned.

func DecodeDict added in v0.3.3

func DecodeDict(r Getter, dst interface{}, src Dict) error

DecodeDict initialises a struct using the data from a PDF dictionary. The argument dst must be a pointer to a struct, or the function will panic.

Go struct tags can be used to control the decoding process. The following tags are supported:

  • "optional": the field is optional and may be omitted from the PDF dictionary. Omitted fields default to the Go zero value for the field type.
  • "text string": the field is a string which should be encoded as a PDF text string.
  • "allowstring": the field is a Name, but the PDF dictionary may contain a String instead. If a String is found, it will be converted to a Name.
  • "extra": the field is a map[string]string which contains all entries in the PDF dictionary which are not otherwise decoded.

This function is the converse of AsDict.

func DecodeStream added in v0.3.5

func DecodeStream(r Getter, x *Stream, numFilters int) (io.Reader, error)

DecodeStream returns a reader for the decoded stream data. If numFilters is non-zero, only the first numFilters filters are decoded.

func Format added in v0.3.4

func Format(obj Object) string

Format formats a PDF object as a string, in the same way as the it would be written to a PDF file.

func IsMalformed added in v0.4.0

func IsMalformed(err error) bool

func IsTagged added in v0.3.3

func IsTagged(pdf Putter) bool

func Wrap added in v0.3.5

func Wrap(err error, loc string) error

Types

type Array

type Array []Object

Array represent an array of objects in a PDF file.

func (Array) PDF

func (x Array) PDF(w io.Writer) error

PDF implements the Object interface.

func (Array) String

func (x Array) String() string

type AuthenticationError

type AuthenticationError struct {
	ID []byte
}

AuthenticationError indicates that authentication failed because the correct password has not been supplied.

func (*AuthenticationError) Error

func (err *AuthenticationError) Error() string

type Boolean added in v0.3.5

type Boolean bool

Boolean represents a boolean value in a PDF file.

func (Boolean) PDF added in v0.3.5

func (x Boolean) PDF(w io.Writer) error

PDF implements the Object interface.

type Catalog

type Catalog struct {
	Version           Version `pdf:"optional"`
	Extensions        Object  `pdf:"optional"`
	Pages             Reference
	PageLabels        Object       `pdf:"optional"`
	Names             Object       `pdf:"optional"`
	Dests             Object       `pdf:"optional"`
	ViewerPreferences Object       `pdf:"optional"`
	PageLayout        Name         `pdf:"optional"`
	PageMode          Name         `pdf:"optional"`
	Outlines          Reference    `pdf:"optional"`
	Threads           Reference    `pdf:"optional"`
	OpenAction        Object       `pdf:"optional"`
	AA                Object       `pdf:"optional"`
	URI               Object       `pdf:"optional"`
	AcroForm          Object       `pdf:"optional"`
	MetaData          Reference    `pdf:"optional"`
	StructTreeRoot    Object       `pdf:"optional"`
	MarkInfo          Object       `pdf:"optional"`
	Lang              language.Tag `pdf:"optional"`
	SpiderInfo        Object       `pdf:"optional"`
	OutputIntents     Object       `pdf:"optional"`
	PieceInfo         Object       `pdf:"optional"`
	OCProperties      Object       `pdf:"optional"`
	Perms             Object       `pdf:"optional"`
	Legal             Object       `pdf:"optional"`
	Requirements      Object       `pdf:"optional"`
	Collection        Object       `pdf:"optional"`
	NeedsRendering    bool         `pdf:"optional"`
	// contains filtered or unexported fields
}

Catalog represents a PDF Document Catalog. The only required field in this structure is Pages, which specifies the root of the page tree. This struct can be used with the DecodeDict and AsDict functions.

The Document Catalog is documented in section 7.7.2 of PDF 32000-1:2008.

type Data added in v0.3.0

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

Data is an in-memory representation of a PDF document.

func NewData added in v0.3.4

func NewData(v Version) *Data

func Read added in v0.3.0

func Read(r io.ReadSeeker, opt *ReaderOptions) (*Data, error)

Read reads a complete PDF document into memory.

func (*Data) Alloc added in v0.3.3

func (d *Data) Alloc() Reference

Alloc allocates a new object number for an indirect object.

func (*Data) AutoClose added in v0.3.3

func (d *Data) AutoClose(obj io.Closer)

func (*Data) Close added in v0.3.3

func (d *Data) Close() error

func (*Data) Get added in v0.3.0

func (d *Data) Get(ref Reference, _ bool) (Object, error)

Get implements the Getter interface.

func (*Data) GetMeta added in v0.3.3

func (d *Data) GetMeta() *MetaInfo

func (*Data) OpenStream added in v0.3.3

func (d *Data) OpenStream(ref Reference, dict Dict, filters ...Filter) (io.WriteCloser, error)

func (*Data) Put added in v0.3.0

func (d *Data) Put(ref Reference, obj Object) error

func (*Data) Write added in v0.3.0

func (d *Data) Write(w io.Writer) error

Write writes the PDF document to w.

func (*Data) WriteCompressed added in v0.3.3

func (d *Data) WriteCompressed(refs []Reference, objects ...Object) error

type Dict

type Dict map[Name]Object

Dict represent a Dictionary object in a PDF file.

func AsDict

func AsDict(s interface{}) Dict

AsDict creates a PDF Dict object, encoding the fields of a Go struct. This is the converse of DecodeDict.

func GetDictTyped added in v0.3.5

func GetDictTyped(r Getter, obj Object, tp Name) (Dict, error)

func (Dict) PDF

func (x Dict) PDF(w io.Writer) error

PDF implements the Object interface.

func (Dict) String

func (x Dict) String() string

type FileInfo added in v0.2.0

type FileInfo struct {
	R             io.ReadSeeker
	FileSize      int64
	PDFStart      int64
	PDFEnd        int64
	HeaderVersion string
	Sections      []*FileSection
}

func SequentialScan added in v0.2.0

func SequentialScan(r io.ReadSeeker) (*FileInfo, error)

SequentialScan reads a PDF file sequentially, extracting information about the file structure and the location of indirect objects. This can be used to attempt to read damaged PDF files, in particular in cases where the cross-reference table is missing or corrupt.

func (*FileInfo) MakeReader added in v0.2.0

func (fi *FileInfo) MakeReader(opt *ReaderOptions) (*Reader, error)

func (*FileInfo) Read added in v0.2.0

func (fi *FileInfo) Read(objInfo *FileObject) (Object, error)

type FileObject added in v0.2.0

type FileObject struct {
	Reference
	ObjStart int64
	ObjEnd   int64
	Broken   bool
	Type     string
	SubType  Name
}

type FileSection added in v0.2.0

type FileSection struct {
	XRefPos       int64
	TrailerPos    int64
	StartXRefPos  int64
	EOFPos        int64
	Objects       []*FileObject
	Catalog       *FileObject
	ObjectStreams []*FileObject
}

TODO(voss): add start and end offsets

type Filter added in v0.3.3

type Filter interface {
	// Info returns the name and parameters of the filter,
	// as they should be written to the PDF file.
	Info(Version) (Name, Dict, error)

	// Encode returns a writer which encodes data written to it.
	// The returned writer must be closed after use.
	Encode(Version, io.WriteCloser) (io.WriteCloser, error)

	// Decode returns a reader which decodes data read from it.
	Decode(Version, io.Reader) (io.Reader, error)
}

Filter represents a PDF stream filter.

Currently, the following filter types are implemented by this library: FilterASCII85, FilterFlate, FilterLZW. In addition, FilterCompress can be used to select the best available compression filter when writing PDF streams. This is FilterFlate for PDF versions 1.2 and above, and FilterLZW for older versions.

type FilterASCII85 added in v0.3.3

type FilterASCII85 struct{}

FilterASCII85 is the ASCII85Decode filter. This filter has no parameters.

func (FilterASCII85) Decode added in v0.3.3

func (f FilterASCII85) Decode(_ Version, r io.Reader) (io.Reader, error)

Decode implements the Filter interface.

func (FilterASCII85) Encode added in v0.3.3

Encode implements the Filter interface.

func (FilterASCII85) Info added in v0.3.3

func (f FilterASCII85) Info(_ Version) (Name, Dict, error)

Info implements the Filter interface.

type FilterCompress added in v0.3.3

type FilterCompress Dict

FilterCompress is a special filter name, which is used to select the best available compression filter when writing PDF streams. This is FilterFlate for PDF versions 1.2 and above, and FilterLZW for older versions.

func (FilterCompress) Decode added in v0.3.3

func (f FilterCompress) Decode(v Version, r io.Reader) (io.Reader, error)

Decode implements the Filter interface.

func (FilterCompress) Encode added in v0.3.3

Encode implements the Filter interface.

func (FilterCompress) Info added in v0.3.3

func (f FilterCompress) Info(v Version) (Name, Dict, error)

Info implements the Filter interface.

type FilterFlate added in v0.3.3

type FilterFlate Dict

FilterFlate is the FlateDecode filter.

The filter is represented by a dictionary of tiler parameters. The following parameters are supported:

  • "Predictor": A code that selects the predictor algorithm, if any. If the value is greater than 1, the data were differenced before being encoded. (Default: 1)

  • "Colors": The number of interleaved color components per sample. (Default: 1)

  • "BitsPerComponent": The number of bits used to represent each color. (Default: 8)

  • "Columns": The number of samples in each row. (Default: 1)

The parameters are explained in detail in section 7.4.4 of PDF 32000-1:2008.

This filter requires PDF versions 1.2 or higher.

func (FilterFlate) Decode added in v0.3.3

func (f FilterFlate) Decode(v Version, r io.Reader) (io.Reader, error)

Decode implements the Filter interface.

func (FilterFlate) Encode added in v0.3.3

func (f FilterFlate) Encode(v Version, w io.WriteCloser) (io.WriteCloser, error)

Encode implements the Filter interface.

func (FilterFlate) Info added in v0.3.3

func (f FilterFlate) Info(v Version) (Name, Dict, error)

Info implements the Filter interface.

type FilterLZW added in v0.3.3

type FilterLZW Dict

FilterLZW is the LZWDecode filter. This is only useful to read legacy PDF files. For new files, use FilterFlate instead.

The filter is represented by a dictionary of tiler parameters. The following parameters are supported:

  • "Predictor": A code that selects the predictor algorithm, if any. If the value is greater than 1, the data were differenced before being encoded. (Default: 1)

  • "Colors": The number of interleaved color components per sample. (Default: 1)

  • "BitsPerComponent": The number of bits used to represent each color. (Default: 8)

  • "Columns": The number of samples in each row. (Default: 1)

  • "EarlyChange": An integer value specifying whether the data is encoded using the correct LZW algorithm (value 0), or whether code with an off-by-one error is used (value 1). (Default: 1)

The parameters are explained in detail in section 7.4.4 of PDF 32000-1:2008.

func (FilterLZW) Decode added in v0.3.3

func (f FilterLZW) Decode(v Version, r io.Reader) (io.Reader, error)

Decode implements the Filter interface.

func (FilterLZW) Encode added in v0.3.3

func (f FilterLZW) Encode(v Version, w io.WriteCloser) (io.WriteCloser, error)

Encode implements the Filter interface.

func (FilterLZW) Info added in v0.3.3

func (f FilterLZW) Info(v Version) (Name, Dict, error)

Info implements the Filter interface.

type Getter added in v0.3.1

type Getter interface {
	GetMeta() *MetaInfo
	Get(Reference, bool) (Object, error)
}

TODO(voss): find a better name for this

type Info

type Info struct {
	Title    string `pdf:"text string,optional"`
	Author   string `pdf:"text string,optional"`
	Subject  string `pdf:"text string,optional"`
	Keywords string `pdf:"text string,optional"`

	// Creator gives the name of the application that created the original
	// document, if the document was converted to PDF from another format.
	Creator string `pdf:"text string,optional"`

	// Producer gives the name of the application that converted the document,
	// if the document was converted to PDF from another format.
	Producer string `pdf:"text string,optional"`

	// CreationDate gives the date and time the document was created.
	CreationDate time.Time `pdf:"optional"`

	// ModDate gives the date and time the document was most recently modified.
	ModDate time.Time `pdf:"optional"`

	// Trapped indicates whether the document has been modified to include
	// trapping information.  (A trap is an overlap between adjacent areas of
	// of different colours, used to avoid visual problems caused by imprecise
	// alignment of different layers of ink.) Possible values are:
	//   * "True": The document has been fully trapped.  No further trapping is
	//     necessary.
	//   * "False": The document has not been trapped.
	//   * "Unknown" (default): Either it is unknown whether the document has
	//     been trapped, or the document has been partially trapped.  Further
	//     trapping may be necessary.
	Trapped Name `pdf:"optional,allowstring"`

	// Custom contains all non-standard fields in the Info dictionary.
	Custom map[string]string `pdf:"extra"`
}

Info represents a PDF Document Information Dictionary. All fields in this structure are optional.

The Document Information Dictionary is documented in section 14.3.3 of PDF 32000-1:2008.

type Integer

type Integer int64

Integer represents an integer constant in a PDF file.

func (Integer) PDF

func (x Integer) PDF(w io.Writer) error

PDF implements the Object interface.

type MalformedFileError

type MalformedFileError struct {
	Err error
	Loc []string
}

MalformedFileError indicates that a PDF file could not be parsed.

func (*MalformedFileError) Error

func (err *MalformedFileError) Error() string

func (*MalformedFileError) Unwrap

func (err *MalformedFileError) Unwrap() error

type MetaInfo added in v0.3.3

type MetaInfo struct {
	// Version is the PDF version used in this file.
	Version Version

	// The ID of the file.  This is either a slice of two byte slices (the
	// original ID of the file, and the ID of the current version), or nil if
	// the file does not specify an ID.
	ID [][]byte

	// Catalog is the document catalog for this file.
	Catalog *Catalog

	// Info is the document information dictionary for this file.
	// This is nil if the file does not contain a document information
	// dictionary.
	Info *Info

	// Trailer is the trailer dictionary for the file.
	// All entries related to the cross-reference table have been removed.
	Trailer Dict
}

MetaInfo represents the meta information of a PDF file.

type Name

type Name string

Name represents a name object in a PDF file.

func ParseName

func ParseName(buf []byte) (Name, error)

ParseName parses a PDF name from the given buffer. The buffer must include the leading slash.

func (Name) PDF

func (x Name) PDF(w io.Writer) error

PDF implements the Object interface.

type Number

type Number float64

A Number is either an Integer or a Real.

func GetNumber added in v0.3.1

func GetNumber(r Getter, obj Object) (Number, error)

GetNumber is a helper function for reading numeric values from a PDF file. This resolves indirect references and makes sure the resulting object is an Integer or a Real.

func (Number) PDF

func (x Number) PDF(w io.Writer) error

PDF implements the Object interface.

type Object

type Object interface {
	// PDF writes the PDF file representation of the object to w.
	PDF(w io.Writer) error
}

Object represents an object in a PDF file. There are nine basic types of PDF objects, which implement this interface: Array, [Bool], Dict, Integer, Name, Real, Reference, *Stream, and String. Custom types can be constructed out of these basic types, by implementing the Object interface.

func Resolve added in v0.3.1

func Resolve(r Getter, obj Object) (Object, error)

Resolve resolves references to indirect objects.

If obj is a Reference, the function reads the corresponding object from the file and returns the result. If obj is not a Reference, it is returned unchanged. The function recursively follows chains of references until it resolves to a non-reference object.

If a reference loop is encountered, the function returns an error of type MalformedFileError.

TODO(voss): rename to "Get"?

type Perm

type Perm int

Perm describes which operations are permitted when accessing the document with User access (but not Owner access). The user can always view the document.

This library just reports the permissions as specified in the PDF file. It is up to the caller to enforce the permissions.

const (
	// PermCopy allows to extract text and graphics.
	PermCopy Perm = 1 << iota

	// PermPrintDegraded allows printing of a low-level representation of the
	// appearance, possibly of degraded quality.
	PermPrintDegraded

	// PermPrint allows printing a representation from which a faithful digital
	// copy of the PDF content could be generated.  This implies
	// PermPrintDegraded.
	PermPrint

	// PermForms allows to fill in form fields, including signature fields.
	PermForms

	// PermAnnotate allows to add or modify text annotations. This implies
	// PermForms.
	PermAnnotate

	// PermAssemble allows to insert, rotate, or delete pages and to create
	// bookmarks or thumbnail images.
	PermAssemble

	// PermModify allows to modify the document.  This implies PermAssemble.
	PermModify

	// PermAll gives the user all permissions, making User access equivalent to
	// Owner access.
	PermAll = permNext - 1
)

type Placeholder

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

A Placeholder is a space reserved in a PDF file that can later be filled with a value. One common use case is to store the length of compressed content in a PDF stream dictionary. To create Placeholder objects, use the [Writer.NewPlaceholder] method.

func NewPlaceholder added in v0.3.3

func NewPlaceholder(pdf Putter, size int) *Placeholder

NewPlaceholder creates a new placeholder for a value which is not yet known. The argument size must be an upper bound to the length of the replacement text. Once the value becomes known, it can be filled in using the Placeholder.Set method.

func (*Placeholder) PDF

func (x *Placeholder) PDF(w io.Writer) error

PDF implements the Object interface.

func (*Placeholder) Set

func (x *Placeholder) Set(val Object) error

Set fills in the value of the placeholder object. This should be called as soon as possible after the value becomes known.

type Putter added in v0.3.3

type Putter interface {
	Close() error
	GetMeta() *MetaInfo
	Alloc() Reference
	Put(ref Reference, obj Object) error
	OpenStream(ref Reference, dict Dict, filters ...Filter) (io.WriteCloser, error)

	// TODO(voss): allow to set the object ID for the containing stream?
	WriteCompressed(refs []Reference, objects ...Object) error

	AutoClose(obj io.Closer)
}

TODO(voss): find a better name for this

type Reader

type Reader struct {

	// Errors is a list of errors encountered while opening the file.
	// This is only used if the ErrorHandling option is set to
	// ErrorHandlingReport.
	Errors []*MalformedFileError
	// contains filtered or unexported fields
}

Reader represents a pdf file opened for reading. Use Open or NewReader to create a Reader.

func NewReader

func NewReader(data io.ReadSeeker, opt *ReaderOptions) (*Reader, error)

NewReader creates a new Reader object.

func Open

func Open(fname string, opt *ReaderOptions) (*Reader, error)

Open opens the named PDF file for reading. After use, Reader.Close must be called to close the file the Reader is reading from.

func (*Reader) Authenticate added in v0.3.1

func (r *Reader) Authenticate(perm Perm) error

Authenticate tries to authenticate the actions given in perm. If a password is required, this calls the ReadPassword function specified in the ReaderOptions struct. The return value is nil if the owner was authenticated (or if no authentication is required), and an object of type AuthenticationError if the required password was not supplied.

func (*Reader) AuthenticateOwner

func (r *Reader) AuthenticateOwner() error

AuthenticateOwner tries to authenticate the owner of a document. If a password is required, this calls the ReadPassword function specified in the ReaderOptions struct. The return value is nil if the owner was authenticated (or if no authentication is required), and an object of type AuthenticationError if the required password was not supplied.

func (*Reader) Close

func (r *Reader) Close() error

Close closes the Reader.

This call only has an effect if the Reader was created by Open.

func (*Reader) Get added in v0.3.0

func (r *Reader) Get(ref Reference, canObjStm bool) (_ Object, err error)

Get reads an indirect object from the PDF file. If the object is not present, nil is returned without an error.

The argument canObjStm specifies whether the object may be read from an object stream. Normally, this should be set to true. If canObjStm is false and the object is in an object stream, an error is returned.

func (*Reader) GetMeta added in v0.3.3

func (r *Reader) GetMeta() *MetaInfo

type ReaderErrorHandling added in v0.3.1

type ReaderErrorHandling int

type ReaderOptions added in v0.2.0

type ReaderOptions struct {
	// ReadPassword is a function that queries the user for a password for the
	// document with the given ID.  The function is called repeatedly, with
	// sequentially increasing values of try (starting at 0), until the correct
	// password is entered.  If the function returns the empty string, the
	// authentication attempt is aborted and [AuthenticationError] is reported
	// to the caller.
	ReadPassword func(ID []byte, try int) string

	ErrorHandling ReaderErrorHandling
}

ReaderOptions provides additional information for opening a PDF file.

type Real

type Real float64

Real represents an real number in a PDF file.

func (Real) PDF

func (x Real) PDF(w io.Writer) error

PDF implements the Object interface.

type Rectangle

type Rectangle struct {
	LLx, LLy, URx, URy float64
}

Rectangle represents a PDF rectangle.

func GetRectangle added in v0.3.1

func GetRectangle(r Getter, obj Object) (*Rectangle, error)

GetRectangle resolves references to indirect objects and makes sure the resulting object is a PDF rectangle object. If the object is null, nil is returned.

func (*Rectangle) Extend

func (rect *Rectangle) Extend(other *Rectangle)

Extend enlarges the rectangle to also cover `other`.

func (Rectangle) IsZero

func (rect Rectangle) IsZero() bool

IsZero is true if the rectangle is the zero rectangle object.

func (*Rectangle) NearlyEqual

func (rect *Rectangle) NearlyEqual(other *Rectangle, eps float64) bool

NearlyEqual reports whether the corner coordinates of two rectangles differ by less than `eps`.

func (*Rectangle) PDF

func (rect *Rectangle) PDF(w io.Writer) error

PDF implements the Object interface.

func (*Rectangle) String

func (rect *Rectangle) String() string

func (*Rectangle) XPos

func (rect *Rectangle) XPos(rel float64) float64

func (*Rectangle) YPos

func (rect *Rectangle) YPos(rel float64) float64

type Reference

type Reference uint64

Reference represents a reference to an indirect object in a PDF file. The lowest 32 bits represent the object number, the next 16 bits the generation number.

func NewInternalReference added in v0.4.0

func NewInternalReference(number uint32) Reference

NewInternalReference creates a new reference object which is guaranteed not to clash with an existing reference in the PDF file.

func NewReference added in v0.2.0

func NewReference(number uint32, generation uint16) Reference

NewReference creates a new reference object.

func (Reference) Generation

func (x Reference) Generation() uint16

Generation returns the generation number of the reference.

func (Reference) IsInternal added in v0.4.0

func (x Reference) IsInternal() bool

IsInternal returns true if the reference is an internal reference.

func (Reference) Number

func (x Reference) Number() uint32

Number returns the object number of the reference.

func (Reference) PDF

func (x Reference) PDF(w io.Writer) error

PDF implements the Object interface.

func (Reference) String

func (x Reference) String() string

type Resources

type Resources struct {
	ExtGState  Dict  `pdf:"optional"` // maps resource names to graphics state parameter dictionaries
	ColorSpace Dict  `pdf:"optional"` // maps each resource name to either the name of a device-dependent colour space or an array describing a colour space
	Pattern    Dict  `pdf:"optional"` // maps resource names to pattern objects
	Shading    Dict  `pdf:"optional"` // maps resource names to shading dictionaries
	XObject    Dict  `pdf:"optional"` // maps resource names to external objects
	Font       Dict  `pdf:"optional"` // maps resource names to font dictionaries
	ProcSet    Array `pdf:"optional"` // predefined procedure set names
	Properties Dict  `pdf:"optional"` // maps resource names to property list dictionaries for marked content
}

Resources describes a PDF Resource Dictionary.

See section 7.8.3 of PDF 32000-1:2008 for details.

func (*Resources) IsEmpty added in v0.3.5

func (r *Resources) IsEmpty() bool

type Stream

type Stream struct {
	Dict
	R io.Reader
	// contains filtered or unexported fields
}

Stream represent a stream object in a PDF file.

func (*Stream) PDF

func (x *Stream) PDF(w io.Writer) error

PDF implements the Object interface.

func (*Stream) String

func (x *Stream) String() string

type String

type String []byte

String represents a raw string in a PDF file. The character set encoding, if any, is determined by the context.

func Date

func Date(t time.Time) String

Date creates a PDF String object encoding the given date and time.

func ParseString

func ParseString(buf []byte) (String, error)

ParseString parses a string from the given buffer. The buffer must include the surrounding parentheses or angle brackets.

func TextString

func TextString(s string) String

TextString creates a String object using the "text string" encoding, i.e. using either UTF-16BE encoding (with a BOM) or PdfDocEncoding.

func (String) AsDate

func (x String) AsDate() (time.Time, error)

AsDate converts a PDF date string to a time.Time object. If the string does not have the correct format, an error is returned.

func (String) AsTextString

func (x String) AsTextString() string

AsTextString interprets x as a PDF "text string" and returns the corresponding utf-8 encoded string.

func (String) PDF

func (x String) PDF(w io.Writer) error

PDF implements the Object interface.

type Version

type Version int

Version represents a version of PDF standard.

const (
	V1_0 Version
	V1_1
	V1_2
	V1_3
	V1_4
	V1_5
	V1_6
	V1_7
	V2_0
)

PDF versions supported by this library.

func GetVersion added in v0.3.5

func GetVersion(pdf Putter) Version

func ParseVersion

func ParseVersion(verString string) (Version, error)

ParseVersion parses a PDF version string.

func (Version) String

func (ver Version) String() string

func (Version) ToString

func (ver Version) ToString() (string, error)

ToString returns the string representation of ver, e.g. "1.7". If ver does not correspond to a supported PDF version, an error is returned.

type VersionError

type VersionError struct {
	Operation string
	Earliest  Version
}

VersionError is returned when trying to use a feature in a PDF file which is not supported by the PDF version used. Use CheckVersion to create VersionError objects.

func (*VersionError) Error

func (err *VersionError) Error() string

type Writer

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

Writer represents a PDF file open for writing. Use Create or NewWriter to create a new Writer.

func Create

func Create(name string, opt *WriterOptions) (*Writer, error)

Create creates a PDF file with the given name and opens it for output. If a file with the same name already exists, it will be overwritten.

After writing the content to the file, Writer.Close must be called to write the PDF trailer and close the underlying file.

TODO(voss): make the pdf version an argument instead of an option.

func NewWriter

func NewWriter(w io.Writer, opt *WriterOptions) (*Writer, error)

NewWriter prepares a PDF file for writing, using the provided io.Writer.

After writing the content to the file, Writer.Close must be called to write the PDF trailer.

TODO(voss): make the pdf version an argument instead of an option.

func (*Writer) Alloc

func (pdf *Writer) Alloc() Reference

Alloc allocates an object number for an indirect object.

func (*Writer) AutoClose added in v0.2.0

func (pdf *Writer) AutoClose(obj io.Closer)

AutoClose registers an object to be automatically closed when the PDF file is closed. Only one object can be registered for each key, and objects are closed in ascending order of their keys.

func (*Writer) Close

func (pdf *Writer) Close() error

Close closes the Writer, flushing any unwritten data to the underlying io.Writer.

func (*Writer) GetMeta added in v0.3.3

func (pdf *Writer) GetMeta() *MetaInfo

GetMeta returns the MetaInfo for the PDF file.

func (*Writer) OpenStream

func (pdf *Writer) OpenStream(ref Reference, dict Dict, filters ...Filter) (io.WriteCloser, error)

OpenStream adds a PDF Stream to the file and returns an io.Writer which can be used to add the stream's data. No other objects can be added to the file until the stream is closed.

func (*Writer) Put added in v0.3.0

func (pdf *Writer) Put(ref Reference, obj Object) error

Put writes an indirect object to the PDF file, using the given reference.

func (*Writer) WriteCompressed

func (pdf *Writer) WriteCompressed(refs []Reference, objects ...Object) error

WriteCompressed writes a number of objects to the file as a compressed object stream.

Object streams are only available for PDF version 1.5 and newer; in case the file version is too low, the objects are written directly into the PDF file, without compression.

type WriterOptions

type WriterOptions struct {
	Version Version
	ID      [][]byte

	UserPassword    string
	OwnerPassword   string
	UserPermissions Perm
}

WriterOptions allows to influence the way a PDF file is generated.

Directories

Path Synopsis
Package color implements different PDF color spaces.
Package color implements different PDF color spaces.
examples
font/cff-glyphs
Read a CFF font and display a magnified version of each glyph in a PDF file.
Read a CFF font and display a magnified version of each glyph in a PDF file.
Package font implements the foundations of PDF font handling.
Package font implements the foundations of PDF font handling.
cff
Package cff implements CFF font data embedded into PDF files.
Package cff implements CFF font data embedded into PDF files.
cmap
Package cmap implements CMap files for embedding in PDF files.
Package cmap implements CMap files for embedding in PDF files.
embed
Package embed provides convenience functions to embed fonts into a PDF file.
Package embed provides convenience functions to embed fonts into a PDF file.
opentype
Package opentype implements OpenType fonts embedded into PDF files.
Package opentype implements OpenType fonts embedded into PDF files.
truetype
Package truetype implements TrueType fonts embedded into PDF files.
Package truetype implements TrueType fonts embedded into PDF files.
type1
Package type1 implements Type 1 fonts embedded into PDF files.
Package type1 implements Type 1 fonts embedded into PDF files.
type3
Package type3 implements Type 3 fonts embedded into PDF files.
Package type3 implements Type 3 fonts embedded into PDF files.
Package graphics allows to read and write PDF content streams.
Package graphics allows to read and write PDF content streams.
internal
Package lzw implements the Lempel-Ziv-Welch compressed data format.
Package lzw implements the Lempel-Ziv-Welch compressed data format.
Package numtree implements PDF number trees.
Package numtree implements PDF number trees.
Package pagetree implements PDF page trees.
Package pagetree implements PDF page trees.

Jump to

Keyboard shortcuts

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