pdfcanvas

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Overview

Package pdfcanvas draws a go-webengine layout into a go-pdfkit page: a paint.Canvas whose paths, shadings, images and glyphs become the PDF's own, so the page keeps its text as text (subset-embedded fonts, with a /ToUnicode map, so it can be selected, searched and copied) and its edges as vectors.

doc := pdfkit.New(pdfkit.Options{Compress: true})
fonts := pdfcanvas.NewFonts()
l, _ := eng.LayoutHTML(ctx, html, base, image.Rect(0, 0, 1280, 720))
page := doc.AddPage(pdfkit.NewPageSize(960, 540))
l.PaintVector(pdfcanvas.New(page, 0.75, fonts))

A device pixel is mapped to scale points (0.75 for a CSS pixel at device scale 1: 1/96 in against 1/72 in) and the y axis is flipped, the page's top left being the layout's origin.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TextOf added in v0.14.0

func TextOf(n *dom.Node) string

TextOf is the text under a DOM node, its words separated by single spaces: an accessible name for an annotation's /Contents.

Types

type Canvas

type Canvas struct {

	// Err is the first error a drawing call met (a font pdfkit cannot embed,
	// a shading it refuses); the call that met it drew nothing, the others
	// went on.
	Err error
	// contains filtered or unexported fields
}

Canvas is a paint.Canvas drawing into one pdfkit page.

func New

func New(p *pdfkit.Page, scale float64, fonts *Fonts) *Canvas

New returns a canvas drawing into p, scale points per device pixel, with fonts shared across the document's pages. A nil fonts gets a map of its own.

func (*Canvas) Clip

func (c *Canvas) Clip(p *paint.Path, evenOdd bool)

Clip implements paint.Canvas: the path, then W (or W*) and n, the clipping path operators of ISO 32000-1 §8.5.4.

func (*Canvas) Element added in v0.14.0

func (c *Canvas) Element(n *dom.Node) *pdfkit.StructElem

Element returns the structure element of a DOM node that has a role (an <a href>, a heading…), making it if no content of it was drawn yet: what a caller ties an annotation to (pdfkit.Page.AddTaggedLink). It returns nil for a node with no role of its own, or an artifact, or without Tag.

func (*Canvas) Fill

func (c *Canvas) Fill(p *paint.Path, col css.Color, evenOdd bool)

Fill implements paint.Canvas.

func (*Canvas) Finish added in v0.14.0

func (c *Canvas) Finish()

Finish closes the open sequence. Call it after PaintVector, before drawing anything else on the page.

func (*Canvas) Glyphs

func (c *Canvas) Glyphs(run *paint.GlyphRun)

Glyphs implements paint.Canvas: the run's font, subset-embedded with a /ToUnicode map, and its glyphs at the layout's positions.

func (*Canvas) Image

func (c *Canvas) Image(img image.Image, dst paint.Rect)

Image implements paint.Canvas. Pixel-identical images are embedded once per document (pdfkit content-addresses them).

func (*Canvas) Mark added in v0.14.0

func (c *Canvas) Mark(m paint.Mark)

Mark implements paint.StructuredCanvas: what is drawn next stands for m. The marked-content sequence opens at the next drawing.

func (*Canvas) Restore

func (c *Canvas) Restore()

Restore implements paint.Canvas.

func (*Canvas) Save

func (c *Canvas) Save()

Save implements paint.Canvas.

func (*Canvas) Shade

func (c *Canvas) Shade(s paint.Shading)

Shade implements paint.Canvas: an axial shading (type 2) for a linear gradient, a radial one (type 3) drawn under a transform that makes its circles the gradient's ellipses.

func (*Canvas) Tag added in v0.14.0

func (c *Canvas) Tag(parent *pdfkit.StructElem)

Tag makes the canvas write tagged content under parent, an element of the document's structure (typically pdfkit.Document.Structure(), or an element per page under it). Call it before PaintVector.

func (*Canvas) VectorImage added in v0.22.0

func (c *Canvas) VectorImage(v *paint.VectorImage, dst paint.Rect) bool

VectorImage implements paint.VectorCanvas: the drawing is a form XObject (ISO 32000-1 §8.10), made the first time the document shows it and painted with one Do at each use, so a formula or an emoji used thirty times is one stream in the file. It is placed in the marked content of the element it stands for, which a form painted many times must be (§14.7.4.2): a Figure or a Formula with its alternate description. A Formula's drawing is outlines, not text (go-tex/math draws its glyphs as paths), so it also carries the formula's description as its ActualText (§14.9.4), what a reader copies and a search finds.

type Fonts

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

Fonts converts the layout's parsed fonts into pdfkit fonts, once each. Use one per document (a pdfkit.Font may be shared, but a document is where the set of fonts is bounded); it is not safe for concurrent use.

func NewFonts

func NewFonts() *Fonts

NewFonts returns an empty font map.

Jump to

Keyboard shortcuts

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