Help us shape the future of Go! Take the 2026 Go Developer Survey

pdf

package
v0.0.0-...-928b491 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultOptions = Options{
	Compress:      true,
	SubsetFonts:   true,
	ImageEncoding: cimage.Lossless,
}

Functions

This section is empty.

Types

type Dest

type Dest struct {
	Kind           DestKind
	X, Y, Zoom     float64
	X0, Y0, X1, Y1 float64
}

Dest describes a named destination. Coordinates are in millimetres with the origin at the top left of the target page, matching the source formats this is fed from; they are converted to PDF's bottom-left origin when written.

Which fields matter depends on Kind: Fit uses none, FitH uses Y, FitV uses X, XYZ uses X, Y and Zoom, and FitR uses X0, Y0, X1 and Y1.

type DestKind

type DestKind int

DestKind is a named destination type, matching the PDF destination styles.

const (
	// DestFit fits the whole page in the window.
	DestFit DestKind = iota
	// DestFitH fits the page height and scrolls to Y.
	DestFitH
	// DestFitV fits the page width and scrolls to X.
	DestFitV
	// DestXYZ scrolls to X, Y and applies Zoom. A Zoom of 0 keeps the current one.
	DestXYZ
	// DestFitR fits the X0, Y0 to X1, Y1 rectangle in the window.
	DestFitR
)

type Options

type Options struct {
	Compress    bool
	SubsetFonts bool
	cimage.ImageEncoding
}

type PDF

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

PDF is a portable document format renderer.

func New

func New(w io.Writer, width, height float64, opts *Options) *PDF

New returns a portable document format (PDF) renderer.

func (*PDF) AddAnchor

func (r *PDF) AddAnchor(name string, rect canvas.Rect)

AddAnchor adds an anchor that can be referenced by a link (see AddLink). The rectangle is the area to be referenced. If the width and height are zero and the X and Y positions are zero, it will fit the entire page. If either width/height and X/Y are zero then it will fit the page's height/width and scroll to the X/Y position. Otherwise, if the width and height are zero and X and Y are not zero, it will scroll to the position but not change it's zoom.

func (*PDF) AddAnchorToPage

func (r *PDF) AddAnchorToPage(page int, name string, rect canvas.Rect)

AddAnchorToPage adds an anchor bound to the given page index instead of the current page. The destination name tree is only built in Close, and the page references it indexes are known by then, so an anchor may be registered before its target page is created. Links that point backwards — a link on a later page targeting an earlier one — cannot use AddAnchor, which would bind them to whichever page happens to be current.

func (*PDF) AddDestToPage

func (r *PDF) AddDestToPage(page int, pageHeight float64, name string, dest Dest)

AddDestToPage registers a named destination of an explicit kind on the given page, which a link can then point at with AddLink("#" + name).

It is the unambiguous counterpart to AddAnchor, which infers the destination type from the shape of a rectangle and therefore cannot express, for example, an XYZ destination at x = 0 — indistinguishable from a FitH — or a non-zero zoom, which the inferred form always writes as zero. Prefer this whenever the destination type is known.

pageHeight is the height of the target page in millimetres: Dest coordinates use the top left as their origin while PDF uses the bottom left, so the conversion needs the page height. It may differ from the current page's height, and passing the wrong value flips the destination vertically.

func (r *PDF) AddLink(uri string, rect canvas.Rect)

AddLink adds a link at the given rectangle. If the URI starts with # this will link to an anchor (set with AddAnchor).

func (*PDF) AddOutline

func (r *PDF) AddOutline(name string, level int, y float64)

AddOutline adds an outline element at the given y position. The top-level element must have level zero. If any level is missing, then higher level elements are ignored.

func (*PDF) AddOutlineToPage

func (r *PDF) AddOutlineToPage(page int, pageHeight float64, name string, level int, dest Dest)

AddOutlineToPage adds an outline entry of an explicit destination kind, bound to the given page rather than to the current one.

It is the counterpart to AddAnchorToPage and AddDestToPage. AddOutline infers the destination from y alone, which can only express Fit and FitH; pass dest to state the kind directly. The outline tree is written in Close, so an entry may be registered before its target page exists; entries whose target page never gets written are dropped along with their descendants.

level is the nesting depth, zero for a top-level entry, and entries are expected in document order — the tree is rebuilt from the levels, the way AddOutline does.

pageHeight is the height of the target page in millimetres; see Dest.

func (*PDF) Close

func (r *PDF) Close() error

Close finished and closes the PDF.

func (*PDF) NewPage

func (r *PDF) NewPage(width, height float64)

NewPage starts adds a new page where further rendering will be written to.

func (*PDF) RenderCanvas

func (r *PDF) RenderCanvas(scene *canvas.Canvas, m canvas.Matrix)

RenderCanvas embeds an immutable canvas as a Form XObject, reused by identity within this PDF. Other PDFs get independent objects and resource references.

func (*PDF) RenderCanvasWith

func (r *PDF) RenderCanvasWith(scene *canvas.Canvas, m canvas.Matrix, draw func())

RenderCanvasWith is RenderCanvas with an optional renderer adapter. draw is called synchronously, only when the Form is first built, with r temporarily targeting the Form. The callback must render the scene in local coordinates and must not add pages, links, anchors or outlines. The same scene identity must always use equivalent rendering settings.

func (*PDF) RenderImage

func (r *PDF) RenderImage(img image.Image, m canvas.Matrix)

RenderImage renders an image to the canvas using a transformation matrix.

func (*PDF) RenderPath

func (r *PDF) RenderPath(path *canvas.Path, style canvas.Style, m canvas.Matrix)

RenderPath renders a path to the canvas using a style and a transformation matrix.

func (*PDF) RenderText

func (r *PDF) RenderText(text *canvas.Text, m canvas.Matrix)

RenderText renders a text object to the canvas using a transformation matrix.

func (*PDF) SetImageEncoding

func (r *PDF) SetImageEncoding(enc cimage.ImageEncoding)

SetImageEncoding sets the image encoding to Loss or Lossless.

func (*PDF) SetInfo

func (r *PDF) SetInfo(title, subject, keywords, author, creator string)

SetInfo sets the document's title, subject, keywords, author and creator.

func (*PDF) SetLang

func (r *PDF) SetLang(lang string)

SetLang sets the document's language. It must adhere the RFC 3066 specification on Language-Tag, eg. es-CL.

func (*PDF) Size

func (r *PDF) Size() (float64, float64)

Size returns the size of the canvas in millimeters.

Jump to

Keyboard shortcuts

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