docpdf

package
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2026 License: Apache-2.0 Imports: 21 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package docpdf renders an evaluated document to PDF by driving external converters as subprocesses; no PDF renderer is linked into the binary. An engine reading HTML is handed the HTML backend's markup with the print stylesheet; one reading Markdown is handed the Markdown backend's text.

Index

Constants

View Source
const (
	PandocEnv     = "OPENSYSML_PANDOC"
	WeasyPrintEnv = "OPENSYSML_WEASYPRINT"
	PrinceEnv     = "OPENSYSML_PRINCE"
	MermaidEnv    = "OPENSYSML_MMDC"
	// MermaidPuppeteerEnv names an optional puppeteer configuration file
	// passed to mmdc, for environments whose browser needs launch flags.
	MermaidPuppeteerEnv = "OPENSYSML_MMDC_PUPPETEER"
	KatexEnv            = "OPENSYSML_KATEX"
	// KatexCSSEnv names KaTeX's stylesheet, with its fonts directory beside
	// it, when it is not in the dist directory of the katex executable.
	KatexCSSEnv = "OPENSYSML_KATEX_CSS"
	// DotEnv names the Graphviz dot executable that draws DOT blocks; absent,
	// they are kept as source.
	DotEnv = "OPENSYSML_DOT"
	// JavaEnv names the java that runs the PlantUML jar.
	JavaEnv = "OPENSYSML_JAVA"
	// PlantUMLJarEnv names the PlantUML jar that draws PlantUML blocks; absent,
	// they are kept as source.
	PlantUMLJarEnv = "OPENSYSML_PLANTUML_JAR"
)

Environment variables that point each external tool's discovery at a specific executable, ahead of a PATH lookup by its default name.

View Source
const DefaultTableColumns = 12

DefaultTableColumns is the widest table one landscape page sets legibly at the print stylesheet's dense-table size before the column set is split.

View Source
const DefaultTableMeasure = 140

DefaultTableMeasure is the characters of bold dense-table type (8pt), the type every part of a split table is set in, across the text width of a landscape page at the print stylesheet's margins: 667pt on letter, 717pt on A4, at about 4.8pt a character of a camel-cased heading.

Variables

View Source
var PrintStylesheet string

PrintStylesheet is the PDF backend's print stylesheet: page geometry, the page counter, print fonts and breaks, over the HTML backend's default sheet in a later cascade layer so a reader's unlayered stylesheet still wins. It declares the layer a theme's print companion fills after its own.

Functions

func DrawSVG added in v0.9.2

func DrawSVG(diagrams []docrender.Diagram) ([]string, error)

DrawSVG draws every DOT diagram of a document through Graphviz and returns its SVG markup, in order, for the HTML and Markdown backends to write inline; a diagram in another form has an empty entry. Callers check Graphviz.Available first: a missing dot is an error here, not a fallback.

func Engines

func Engines() []string

Engines names the converters, in the order they are offered. The first is the default.

func Render

func Render(document *docir.Document, engine string, opts Options) ([]byte, error)

Render lays an evaluated document out as PDF with the named engine: its graph-shaped diagrams drawn and its formulas typeset ahead of conversion, then the HTML backend's markup with the print stylesheet for an engine reading HTML, or the Markdown backend's text for one reading Markdown.

Types

type Capabilities

type Capabilities struct {
	Input         Input
	Tools         []string
	NativeOptions bool
}

Capabilities is what a converter states about itself: the form it reads, the executables it drives, and whether it applies the deliverable options natively rather than through the prepared HTML.

type Converter

type Converter interface {
	// Name is what -pdf-engine selects the converter by.
	Name() string

	// Capabilities states the converter's input form and tools.
	Capabilities() Capabilities

	// Available returns nil, or a typed error naming the missing tool.
	Available() error

	// Convert lays the prepared document out as PDF bytes.
	Convert(doc *Prepared) ([]byte, error)
}

Converter is one external Markdown/HTML-to-PDF toolchain, run as a subprocess.

func EngineNamed

func EngineNamed(name string) (Converter, error)

EngineNamed returns the converter -pdf-engine named, the default for "", and a typed error for a name no converter answers to.

type Error

type Error struct {
	Kind ErrorKind

	// Engine is the converter that was selected, where one was.
	Engine string

	// Tool is the external executable involved: its configured override or
	// its default name for ErrorToolMissing, its path for ErrorToolFailed.
	Tool string

	// EnvVar is the environment variable that overrides where Tool is found.
	EnvVar string

	// Engines lists the converters an unknown -pdf-engine could have named.
	Engines []string

	// Detail carries what the tool said on stderr, trimmed.
	Detail string

	// Option names the command-line option a converter cannot apply.
	Option string

	// Columns is the table-column limit the options state, for ErrorTableColumns.
	Columns int
}

Error is a typed PDF-rendering failure.

func (*Error) Error

func (e *Error) Error() string

type ErrorKind

type ErrorKind string

ErrorKind classifies a PDF-rendering failure.

const (
	// ErrorUnknownEngine reports a -pdf-engine name no converter answers to.
	ErrorUnknownEngine ErrorKind = "unknown-engine"
	// ErrorToolMissing reports an external tool the selected converter needs
	// that could not be found.
	ErrorToolMissing ErrorKind = "tool-missing"
	// ErrorToolFailed reports an external tool that ran and failed.
	ErrorToolFailed ErrorKind = "tool-failed"
	// ErrorNoPDF reports a converter that succeeded without producing a PDF.
	ErrorNoPDF ErrorKind = "no-pdf"
	// ErrorUnsupportedOption reports a stylesheet option the selected
	// converter cannot apply, as it writes its own HTML.
	ErrorUnsupportedOption ErrorKind = "unsupported-option"
	// ErrorImageMissing reports an image block whose file cannot be found
	// where its location resolves.
	ErrorImageMissing ErrorKind = "missing-image"
	// ErrorTableColumns reports a table-column limit no continuation table
	// keeps: one leaves no room beside the repeated first column.
	ErrorTableColumns ErrorKind = "table-columns"
)

type Graphviz added in v0.9.2

type Graphviz struct{}

Graphviz is the docrender.DiagramDrawer the HTML and Markdown backends draw the automatic choice's DOT diagrams with: the dot found on PATH or where OPENSYSML_DOT points.

func (Graphviz) Available added in v0.9.2

func (Graphviz) Available() bool

Available reports whether a Graphviz dot is found: what decides whether the automatic diagram form draws a positioned view through Graphviz or falls back to Mermaid.

func (Graphviz) Draw added in v0.9.2

func (Graphviz) Draw(diagrams []docrender.Diagram) ([]string, error)

Draw is DrawSVG.

type Input

type Input string

Input is the document form a converter reads.

const (
	InputHTML     Input = "html"
	InputMarkdown Input = "markdown"
)

type Options

type Options struct {
	// TitlePage puts the document title in a page of its own.
	TitlePage bool

	// TOC writes a table of contents ahead of the content.
	TOC bool

	// NumberSections numbers the section headings hierarchically.
	NumberSections bool

	// NumberFigures numbers the figures and tables in their captions, as
	// docrender.HTMLOptions.NumberFigures.
	NumberFigures bool

	// Theme names the HTML backend's bundled theme laid under the print
	// stylesheet, with the theme's print companion, when it carries one, laid
	// over the print stylesheet; empty is the default sheet alone.
	Theme string

	// NoDefaultStylesheet leaves the HTML backend's default sheet out, and the
	// print stylesheet layered over it, so Stylesheets alone style the page.
	NoDefaultStylesheet bool

	// Stylesheets are the reader's, attached after the bundled sheets and
	// unlayered, so they override them as they override the HTML form.
	Stylesheets []docrender.Stylesheet

	// BaseDir is the directory a reader stylesheet's relative url() and
	// @import references resolve against, the current directory when empty:
	// the PDF's own, as an HTML page's sheets resolve against the page's; and
	// an image block's relative location when its source is no file on disk.
	BaseDir string

	// Lang is the document language, "en" when empty.
	Lang string

	// DiagramForm is the source graph-shaped diagrams are drawn from. Empty
	// picks per diagram: DOT drawn by Graphviz for a rendering a Layout or
	// Route positions when Graphviz is installed, Mermaid otherwise, the
	// document stating each fallback.
	DiagramForm view.Form

	// Unplaced is where a diagram some Layout positions puts the nodes none
	// does: left undrawn when empty, or drawn too (a strip below a DOT drawing).
	Unplaced view.Unplaced

	// Style is the drawing style every DOT diagram is drawn in, the Pilot look
	// when empty.
	Style view.DrawingStyle

	// TableColumns is the most columns one table is set with on a page: a
	// table projecting more is split into continuation tables, each repeating
	// the first column ahead of its share of the rest. 0 is DefaultTableColumns;
	// 1 is refused, as nothing would fit beside the repeated column.
	TableColumns int

	// TableMeasure is the characters of heading type one landscape table line
	// holds: a table whose headings need more is split into continuation
	// tables whose headings each set unbroken. 0 is DefaultTableMeasure.
	TableMeasure int
}

Options are the deliverable choices of the PDF backend: the document options every engine applies, and the stylesheet choices the HTML-input engines take, meaning what they mean for -doc-form html.

type Prepared

type Prepared struct {
	// Dir is the working directory holding every input, diagram SVGs included.
	Dir string

	// MarkdownFile is the Markdown document's name within Dir, for a converter
	// reading Markdown; empty for one reading HTML.
	MarkdownFile string

	// Filter is a pandoc Lua filter within Dir that swaps the drawn diagrams
	// and typeset formulas into the Markdown; empty when there are none.
	Filter string

	// HTMLFile is the HTML document's name within Dir, for a converter reading
	// HTML; empty for one reading Markdown.
	HTMLFile string

	// MathCSS is the KaTeX stylesheet's path within Dir, for converters that
	// read the Markdown themselves; empty when the document has no formulas.
	MathCSS string

	// BaseDir is the absolute directory the document's relative references
	// resolve against; Dir's own files are referenced by absolute file URL.
	BaseDir string

	// Options are the deliverable choices, for converters with native flags.
	Options Options
}

Prepared is one document laid out in a working directory for a converter, in the form the converter reads, with its diagrams drawn and its formulas typeset beside it.

Jump to

Keyboard shortcuts

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