Documentation
¶
Overview ¶
Package sr applies a banded report template to a sequence of records and produces a paginated document.
A template is a KDL v2 document; records are Go maps or structs, or JSON on the way in; the result is a printout -- pages of absolutely-positioned marks with nothing evaluable left in them.
tpl, err := sr.LoadTemplate("sakila.kdl")
out, err := tpl.Build(rows, sr.StrictFonts())
The specification the implementation follows is in the doc directory: template.md, expressions.md, layout.md and printout.md.
Index ¶
- func Decimal(text string) decimal.Decimal
- func WritePDF(doc *Printout, path string, options ...pdf.Option) error
- type FontCheck
- type Info
- type Option
- type Parameter
- type Printout
- type Template
- func (tpl *Template) Build(records any, options ...Option) (*Printout, error)
- func (tpl *Template) BuildJSON(reader io.Reader, options ...Option) (*Printout, error)
- func (tpl *Template) CheckFonts(options ...Option) (*FontCheck, error)
- func (tpl *Template) Info() Info
- func (tpl *Template) Name() string
- func (tpl *Template) Warnings() []string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Decimal ¶
Decimal builds an exact decimal value, the type a member declared type="decimal" produces.
func WritePDF ¶
WritePDF renders a printout to a PDF file.
It is package pdf's WriteFile, re-exported for the common case where a caller builds and renders in one place.
The renderer does no layout: everything it draws is already positioned in the printout, so rendering a printout read back from a file gives the same PDF as rendering the one that was just built.
Types ¶
type Info ¶
type Info struct {
Name string
Description string
Version string
Author string
// Page is the page size and margins, in points.
Page printout.PageGeometry
// Columns is the column count, 1 for a template with no columns node.
Columns int
// Names declared, in declaration order.
Parameters []Parameter
Variables []string
Fonts []string
Data []string
Groups []string
// Members are the record members the template declares,
// empty for a template that declares none.
Members []string
// Subreports names the subreport nodes the template carries, in the
// order they are reached, so a front end can say what a build will
// pull in without loading anything itself.
Subreports []string
}
Info is what a template says about itself.
It is what a front end needs to describe or check a template without building anything: the report's identification, its page geometry, and the names it declares. The engine reads none of it.
type Option ¶
Option configures a build.
func AllowOverflow ¶
func AllowOverflow() Option
AllowOverflow downgrades oversized-band errors to warnings recorded in the printout header, so an overflowing document is identifiable from the artifact.
func StrictFonts ¶
func StrictFonts() Option
StrictFonts resolves only fonts the template names by file or data.
It fails with the unresolved typeface named. Tests run this way, so no outcome depends on what is installed.
func WithBuildTime ¶
WithBuildTime fixes BUILD_TIME.
With it, and with StrictFonts, the same template over the same data produces a byte-identical printout on any machine.
func WithDiagnostics ¶
WithDiagnostics receives messages about the machine: a font file that could not be read, two faces claiming one family and style.
They describe the host rather than the document, so they are not printout warnings.
func WithParam ¶
WithParam supplies a report parameter as a typed value.
Its type must match the parameter's declaration.
func WithTextParam ¶
WithTextParam supplies a report parameter as text, parsed per the parameter's declared type -- the form the command line uses.
type Parameter ¶
type Parameter struct {
Name string
// Type is the declared type: "string", "int", "decimal",
// "float", "bool", "datetime", "date", "object" or "list".
Type string
// Default is the declared default text, absent for a parameter
// that has none or computes one.
Default string
// HasDefault distinguishes a declared empty default from none.
HasDefault bool
// HasDefaultExpr reports a computed default.
HasDefaultExpr bool
// Required reports that the caller must supply a value.
Required bool
// Prompt is the template's hint that an interactive front end
// should ask for this one. The engine ignores it.
Prompt bool
}
Parameter is one declared parameter.
type Template ¶
type Template struct {
// contains filtered or unexported fields
}
Template is a loaded, validated template.
func LoadTemplate ¶
LoadTemplate reads and validates a template file.
Validation runs once, here, before any data is read: node nesting, required properties, name collisions, geometry over-specification, content sources, and expression syntax are all load-time diagnostics rather than surprises mid-report.
func ParseTemplate ¶
ParseTemplate reads a template from memory.
name is used in diagnostics and as the base for relative paths.
func (*Template) Build ¶
Build applies the template to records.
records may be a []map[string]any or a slice of structs; struct fields map to declared members by name, or by an `sr:"..."` tag. Pass nil for a template with no records.
func (*Template) BuildJSON ¶
BuildJSON applies the template to records read from a JSON array document or from NDJSON.
The whole dataset is buffered: DATA_COUNT, report-scoped aggregates, and keep-together lookahead all need the full sequence.
func (*Template) CheckFonts ¶
CheckFonts resolves every font the template declares, without any data.
Loading a template validates everything a document can settle on its own. Font resolution is what it cannot: it reads the machine. So a check that wants to say more than "this file parses" runs this, which is what sr validate does.
A font that does not resolve is reported in the result rather than as an error, so that one missing typeface does not hide the next. Parameters are not required here -- one with no value and no default is simply left unbound.
It covers the templates a subreport names by file as well as this one: a template half of whose fonts resolve is not a template that resolves.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
sr
command
Command sr applies a report template to data and writes a PDF.
|
Command sr applies a report template to data and writes a PDF. |
|
internal
|
|
|
barcodes
Package barcodes encodes a value into the stripe geometry a printout carries.
|
Package barcodes encodes a value into the stripe geometry a printout carries. |
|
build
Package build is the layout engine.
|
Package build is the layout engine. |
|
cli
Package cli is the command line, per doc/cli.md.
|
Package cli is the command line, per doc/cli.md. |
|
data
Package data loads input records and coerces them to their declared types.
|
Package data loads input records and coerces them to their declared types. |
|
fontres
Package fontres implements the font resolution chain, metrics, and word wrap of doc/template.md#font-resolution.
|
Package fontres implements the font resolution chain, metrics, and word wrap of doc/template.md#font-resolution. |
|
geom
Package geom implements the geometry model of doc/template.md#geometry: dimensions with units, the three-decimal rounding rule, boxes, the any-two-of-three edge resolution, and content alignment.
|
Package geom implements the geometry model of doc/template.md#geometry: dimensions with units, the three-decimal rounding rule, boxes, the any-two-of-three edge resolution, and content alignment. |
|
kdl
Package kdl reads a KDL v2 document into a small tree of its own.
|
Package kdl reads a KDL v2 document into a small tree of its own. |
|
pdfscan
Package pdfscan reads a PDF back: its objects, its pages, and the text, paths and images its content streams draw.
|
Package pdfscan reads a PDF back: its objects, its pages, and the text, paths and images its content streams draw. |
|
pdfw
Package pdfw writes PDF files: indirect objects, streams, the cross reference table, and the content stream operators a printout's marks need.
|
Package pdfw writes PDF files: indirect objects, streams, the cross reference table, and the content stream operators a printout's marks need. |
|
segment
Package segment solves one-dimensional stretchable segment layout.
|
Package segment solves one-dimensional stretchable segment layout. |
|
sfnt
Package sfnt reads the font tables an embedded PDF font needs and builds a subset containing only the glyphs a document uses.
|
Package sfnt reads the font tables an embedded PDF font needs and builds a subset containing only the glyphs a document uses. |
|
tmpl
Package tmpl holds the template model of doc/template.md: the node tree a KDL document parses into, and the validation that runs once at load, before any data is read.
|
Package tmpl holds the template model of doc/template.md: the node tree a KDL document parses into, and the validation that runs once at load, before any data is read. |
|
vars
Package vars implements variable accumulation: the twelve calc modes.
|
Package vars implements variable accumulation: the twelve calc modes. |
|
Package meta holds build-time metadata stamped in by the linker.
|
Package meta holds build-time metadata stamped in by the linker. |
|
Package pdf renders a printout as PDF.
|
Package pdf renders a printout as PDF. |
|
Package printout is the document an engine produces and a renderer consumes: pages of absolutely-positioned marks in paint order.
|
Package printout is the document an engine produces and a renderer consumes: pages of absolutely-positioned marks in paint order. |