docpdf

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2026 License: Apache-2.0 Imports: 17 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.

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 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
}

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

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

	// 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.
	BaseDir string

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

	// DiagramForm is the source graph-shaped diagrams are drawn from, Mermaid
	// when empty.
	DiagramForm view.Form

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

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