pdfcanvas

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: BSD-3-Clause Imports: 5 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

This section is empty.

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

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

Fill implements paint.Canvas.

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

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