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 WithComponent(name string, handler mdx.ComponentHandler) Option
- func WithConfig(cfg *config.Config) Option
- func WithConfigCSSAndTemplate(cfg *config.Config) 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 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 PDF
- func (p *PDF) Build(ctx context.Context) error
- func (p *PDF) ComposeHTML(docs []*mdx.Document) (string, error)
- func (p *PDF) ParseDir() ([]*mdx.Document, error)
- func (p *PDF) Render(html 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
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 WithComponent ¶
func WithComponent(name string, handler mdx.ComponentHandler) Option
func WithConfig ¶
func WithConfigCSSAndTemplate ¶
WithConfigCSSAndTemplate loads CSS and template content from the file paths in cfg. Read 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 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 pre-built theme's CSS and template. It shares composeOpts.CSS/Template with WithCSS, WithTemplate, and the theme branch of WithConfigCSSAndTemplate — whichever of these options is applied last wins, since New() applies options in the order they're passed.
func WithTimeout ¶
func WithValidator ¶
func WithVerbose ¶
type PDF ¶
type PDF struct {
// contains filtered or unexported fields
}
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