Documentation
¶
Overview ¶
Package prettypdf transforms MDX source files into print-ready PDFs via headless Chrome.
It is both a composable Go library and a CLI tool. The library exposes a pipeline with step-by-step methods (ParseDir, ValidateDoc, ComposeHTML, Render) and 18 functional options for full customization.
Quick start (library) ¶
pdf, err := prettypdf.New(
prettypdf.WithSourceDir("./docs"),
prettypdf.WithOutputFile("output.pdf"),
prettypdf.WithTitle("My Book"),
)
if err != nil {
log.Fatal(err)
}
if err := pdf.Build(context.Background()); err != nil {
log.Fatal(err)
}
Quick start (CLI) ¶
go install github.com/sazardev/go-pretty-pdf/cmd/pretty-pdf@latest pretty-pdf build --source ./docs --out out.pdf
MDX files require frontmatter with an `id` field in [X.Y.Z] format (e.g. `[1.0.0]`). Documents are sorted by ID, not filename. Variables in {{key}} syntax are substituted before parsing.
Built-in custom components: <DeepDive>, <Warning>, <Axiom>. Additional components can be registered via WithComponent().
Trust model ¶
MDX is parsed with raw HTML passthrough enabled, and component transpilation does not escape inner content. Only build PDFs from MDX you trust — see SECURITY.md for details. Network access during rendering is blocked by default (see WithNetworkAccess).
Index ¶
- func ComposeHTML(docs []*mdx.Document, opts ComposeOptions) (string, error)
- type ComposeOptions
- type Option
- func WithAuthor(author string) Option
- func WithCSS(css string) Option
- func WithChromeExecPath(path string) Option
- func WithComponent(name string, handler mdx.ComponentHandler) Option
- func WithConfig(cfg *config.Config) Option
- func WithConfigCSSAndTemplate(cfg *config.Config) Option
- func WithCoverImage(imagePath string) Option
- func WithEpubLanguage(lang string) Option
- func WithFormats(formats ...OutputFormat) Option
- func WithFullConfig(cfg *config.Config) Option
- func WithHeaderTitle(title string) Option
- func WithNetworkAccess(enabled bool) Option
- func WithOutputFile(path string) Option
- func WithPaperSize(width, height float64) Option
- func WithRenderMargins(top, bottom, left, right float64) Option
- func WithSourceDir(dir string) Option
- func WithSubtitle(subtitle string) Option
- func WithTemplate(html string) Option
- func WithTheme(t theme.Theme) Option
- func WithThemeName(name string, opts theme.Options) Option
- func WithTimeout(d time.Duration) Option
- func WithTitle(title string) Option
- func WithValidator(v mdx.Validator) Option
- func WithVars(vars map[string]string) Option
- func WithVerbose(v bool) Option
- type OutputFormat
- type PDF
- func (p *PDF) Build(ctx context.Context) error
- func (p *PDF) ComposeHTML(docs []*mdx.Document) (string, error)
- func (p *PDF) Formats() []OutputFormat
- func (p *PDF) LastAudit() *render.AuditReport
- func (p *PDF) NeedsChrome() bool
- func (p *PDF) ParseDir() ([]*mdx.Document, error)
- func (p *PDF) Render(html string) error
- func (p *PDF) RenderEpub(docs []*mdx.Document, outputPath string) error
- func (p *PDF) Validate(ctx context.Context) ([]mdx.ValidationError, error)
- func (p *PDF) ValidateAll(docs []*mdx.Document) []mdx.ValidationError
- func (p *PDF) ValidateDoc(doc *mdx.Document) []mdx.ValidationError
- func (p *PDF) Warnings() []string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ComposeHTML ¶
func ComposeHTML(docs []*mdx.Document, opts ComposeOptions) (string, error)
Types ¶
type ComposeOptions ¶
func DefaultComposeOptions ¶
func DefaultComposeOptions() ComposeOptions
type Option ¶
type Option func(*PDF)
func WithAuthor ¶
func WithChromeExecPath ¶ added in v0.5.0
WithChromeExecPath pins rendering to a specific Chrome/Chromium binary instead of chromedp's default system discovery. Leave unset (or pass "") to keep the default behavior. See the chromemgr package for resolving this automatically, including downloading a browser when none is installed.
func WithComponent ¶
func WithComponent(name string, handler mdx.ComponentHandler) Option
func WithConfig ¶
func WithConfigCSSAndTemplate ¶
WithConfigCSSAndTemplate resolves cfg.Theme (with cfg.ThemeOptions customization) and then loads CSS/template content from cfg.CSS/ cfg.Template, which — being explicit file overrides — take priority over the theme and replace its CSS/template outright. Read/resolve failures are recorded as warnings and flushed to stderr by New() once all options have been applied, so ordering relative to WithVerbose does not matter.
func WithCoverImage ¶ added in v0.8.0
WithCoverImage replaces the theme's text cover with a full-bleed page built from imagePath (.png/.jpg/.jpeg). That cover page is sized to the image's own pixel dimensions exactly — a square image gets a square cover page — while every other page keeps its configured paper size. The theme's own text cover (title/subtitle/metadata) is suppressed regardless of theme/section settings and regardless of the order options are applied in — see New(), which enforces this once after every Option has run.
func WithEpubLanguage ¶ added in v0.10.0
func WithFormats ¶ added in v0.10.0
func WithFormats(formats ...OutputFormat) Option
func WithFullConfig ¶ added in v0.3.0
WithFullConfig applies every field of cfg: source/output/title/subtitle /author (via WithConfig), CSS/template/theme (via WithConfigCSSAndTemplate), variable substitution (cfg.Vars), and render settings (cfg.Render: timeout, paper size, margins, header title). Unlike WithConfig and WithConfigCSSAndTemplate, which only cover a subset of Config, this is the single option needed to fully apply a loaded go-pretty-pdf.yml.
func WithHeaderTitle ¶
WithHeaderTitle sets the PDF page header text. If never called, New() defaults it to the document title (WithTitle/composeOpts.Title).
func WithNetworkAccess ¶ added in v0.3.0
WithNetworkAccess controls whether headless Chrome may make outbound network requests while rendering. It defaults to false: the composed HTML is a self-contained data URI, so network access is blocked to prevent SSRF/exfiltration from untrusted MDX content (e.g. a malicious <img> or <script> tag). Enable it only if your documents intentionally reference remote images, fonts, or other resources by URL.
func WithOutputFile ¶
func WithPaperSize ¶
func WithRenderMargins ¶
func WithSourceDir ¶
func WithSubtitle ¶
func WithTemplate ¶
func WithTheme ¶
WithTheme applies a raw builtin/synthetic Theme's CSS as-is, with no customization (colors/fonts/sections/density) and no section toggles applied. It shares composeOpts.CSS with WithCSS and WithThemeName — whichever of these options is applied last wins, since New() applies options in the order they're passed. Most callers should prefer WithThemeName, which resolves section toggles (cover/TOC/page numbers/header) into composeOpts/renderOpts too.
func WithThemeName ¶ added in v0.4.0
WithThemeName resolves a theme by name — a builtin ("default", "corporate", ...), a custom theme discovered in ./themes/ or the global themes directory, or a direct path to a .theme.yml/.css file — applies opts customization (colors, fonts, density, network fonts), and wires the resulting section toggles (cover, TOC, page numbers, header) into composeOpts/renderOpts.
func WithTimeout ¶
func WithValidator ¶
func WithVerbose ¶
type OutputFormat ¶ added in v0.10.0
type OutputFormat string
const ( FormatPDF OutputFormat = "pdf" FormatEPUB OutputFormat = "epub" )
func ParseFormats ¶ added in v0.10.0
func ParseFormats(s string) ([]OutputFormat, error)
type PDF ¶
type PDF struct {
// contains filtered or unexported fields
}
func (*PDF) Formats ¶ added in v0.10.0
func (p *PDF) Formats() []OutputFormat
func (*PDF) LastAudit ¶ added in v0.8.0
func (p *PDF) LastAudit() *render.AuditReport
LastAudit returns the visual/structural audit report from the most recent Build or Render call, or nil if neither has run yet. See render.AuditReport and render/audit.go for what it checks.
func (*PDF) NeedsChrome ¶ added in v0.10.0
func (*PDF) RenderEpub ¶ added in v0.10.0
func (*PDF) ValidateAll ¶ added in v0.2.0
func (p *PDF) ValidateAll(docs []*mdx.Document) []mdx.ValidationError
func (*PDF) ValidateDoc ¶
func (p *PDF) ValidateDoc(doc *mdx.Document) []mdx.ValidationError
func (*PDF) Warnings ¶ added in v0.9.0
Warnings returns the non-fatal configuration warnings recorded by New — e.g. an unresolvable theme name or an unreadable --css/--template file — each of which caused that option to fall back rather than apply. They are also printed to stderr by New itself; this accessor exists for callers that want to detect the condition programmatically instead.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package chromemgr resolves a working Chrome/Chromium executable for headless rendering without requiring the user to install one by hand.
|
Package chromemgr resolves a working Chrome/Chromium executable for headless rendering without requiring the user to install one by hand. |
|
cmd
|
|
|
pretty-pdf
command
|
|
|
Package epub converts parsed MDX documents into a single EPUB 3 file — no headless Chrome involved, unlike the PDF pipeline in render.
|
Package epub converts parsed MDX documents into a single EPUB 3 file — no headless Chrome involved, unlike the PDF pipeline in render. |
|
scripts
|
|
|
bump
command
|
|
|
docsgen
command
|
|
|
Package theme implements go-pretty-pdf's theme engine: a set of professional built-in themes plus a customization layer (colors, fonts, section toggles, density) that composes on top of them via CSS custom properties, and a YAML format for user-defined custom themes.
|
Package theme implements go-pretty-pdf's theme engine: a set of professional built-in themes plus a customization layer (colors, fonts, section toggles, density) that composes on top of them via CSS custom properties, and a YAML format for user-defined custom themes. |