Documentation
¶
Overview ¶
Package widget prints a go-widgets/toolkit widget tree onto a pdfkit page, so any UI composed from toolkit widgets can be "printed" to a PDF. Two levels are offered:
- Add rasterises the tree through a painter.PixelPainter and places the result as an image XObject. It renders every widget faithfully (the same pixels a window would show) but the output is a bitmap.
- AddVector runs the tree through a painter.Painter that emits PDF vector operators (fills, strokes, rounded rects and text-show operators) instead of pixels, so text stays selectable and lines stay crisp. It uses a caller-supplied embedded Font for text; widgets drawing through the toolkit's built-in bitmap font reach the painter's Text primitive, which becomes real PDF text, and a TrueType/OpenType toolkit font (a painter.Face) is embedded itself.
The bridge lives in its own package so that a program which only writes PDFs (import "github.com/go-pdfkit/pdfkit") does not link go-widgets/toolkit and everything it pulls in. Until v0.20.0 it was Page.AddWidget and Page.AddWidgetVector in the root package.
The toolkit lays a scene out in integer painter units (pixels) with absolute child bounds, then every widget's Draw targets a single painter over the whole canvas — so one painter, positioned once over the target rectangle, renders the entire tree.
Index ¶
Constants ¶
const DefaultScale = 2.0
DefaultScale is the number of layout pixels per PDF point used when Options.Scale is unset. A value of 2 lays the tree out at twice the point resolution, giving crisper raster output and finer layout rounding.
Variables ¶
var ErrEmptyRect = errors.New("pdfkit/widget: target rectangle is empty at the given scale")
ErrEmptyRect is returned when the target rectangle scales to a zero-or-negative pixel canvas, so there is nothing to lay out or render.
var ErrNoFont = errors.New("pdfkit/widget: AddVector requires Options.Font for selectable text")
ErrNoFont is returned by AddVector when no Font is supplied: selectable vector text needs an embedded font.
Functions ¶
func Add ¶
Add lays root out to fill rect (in points) on page p, renders the whole widget tree to an RGBA raster through a painter.PixelPainter, and places that raster as an image XObject at rect. It is the "print any UI to PDF" path: every widget renders exactly as it would on screen. opts may be nil.
func AddVector ¶
AddVector lays root out to fill rect (in points) on page p and renders the widget tree with PDF vector operators, so text stays selectable and edges stay crisp. Text is drawn with opts.Font, which must be non-nil: a nil or fontless opts returns ErrNoFont. The whole tree is clipped to rect.
Types ¶
type Options ¶
type Options struct {
// Theme is the toolkit theme the widgets paint with. When nil,
// toolkit.DefaultLight() is used.
Theme *toolkit.Theme
// Scale is the number of layout pixels per PDF point. Values <= 0 default
// to DefaultScale. Larger values give a crisper raster (Add) and finer
// layout rounding for both paths.
Scale float64
// Font is the embedded font used for selectable text on the vector path
// (AddVector). It is ignored by the raster Add path.
Font *pdfkit.Font
}
Options configures how a widget tree is transferred onto a page. The nil *Options is valid and selects every default.