html

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: MIT Imports: 8 Imported by: ? 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 html converts HTML strings into Paper rows so they can be added to a Paper document. No browser, no external binary, no JavaScript.

Supported tags and CSS properties are documented in docs/html-support.md.

Index

Constants

View Source
const (
	// LimitElements reports that the configured element/node budget was exceeded.
	LimitElements = translate.LimitElements
	// LimitDepth reports that the configured DOM nesting depth was exceeded.
	LimitDepth = translate.LimitDepth
	// LimitStyleRules reports that the configured stylesheet rule budget was exceeded.
	LimitStyleRules = translate.LimitStyleRules
)

Variables

View Source
var (
	// ErrImageTooLarge is returned when an HTML image exceeds configured byte
	// or pixel limits.
	ErrImageTooLarge = htmllimits.ErrImageTooLarge

	// ErrDOMTooDeep is returned when the parsed DOM exceeds MaxDOMDepth.
	ErrDOMTooDeep = htmllimits.ErrDOMTooDeep

	// ErrDOMTooLarge is returned when the parsed DOM exceeds MaxDOMNodes.
	ErrDOMTooLarge = htmllimits.ErrDOMTooLarge

	// ErrSVGTooLarge is returned when SVG rasterization exceeds MaxSVGPixels.
	ErrSVGTooLarge = htmllimits.ErrSVGTooLarge

	// ErrStyleRulesTooLarge is returned when CSS parsing exceeds MaxStyleRules.
	ErrStyleRulesTooLarge = htmllimits.ErrStyleRulesTooLarge
)

Functions

func FromReader

func FromReader(ctx context.Context, r io.Reader, opts ...Option) ([]core.Row, error)

FromReader parses HTML from an io.Reader and returns the corresponding rows. It observes ctx before and after reading and during translation.

func FromString

func FromString(ctx context.Context, htmlStr string, opts ...Option) ([]core.Row, error)

FromString parses an HTML string and returns the corresponding Paper rows. It observes ctx before and after DOM parsing and during translation.

Types

type AssetError added in v0.2.2

type AssetError = translate.AssetError

AssetError indicates a document-referenced asset failed to load while converting HTML with WithStrictAssets enabled.

type Document added in v0.2.0

type Document = translate.Document

Document is the full result of converting an HTML document: content rows plus document-level options parsed from `@page` rules.

func DocumentFromReader added in v0.2.0

func DocumentFromReader(ctx context.Context, r io.Reader, opts ...Option) (*Document, error)

DocumentFromReader parses HTML from an io.Reader and returns the full Document result. It observes ctx before and after reading and during translation.

func DocumentFromString added in v0.2.0

func DocumentFromString(ctx context.Context, htmlStr string, opts ...Option) (*Document, error)

DocumentFromString parses an HTML string and returns the full Document result. Unlike FromString, it surfaces `@page` size/margin options so callers (notably paper.FromHTML) can configure the page before rendering. It observes ctx before and after DOM parsing and during translation.

type LimitError added in v0.2.2

type LimitError = translate.LimitError

LimitError indicates HTML conversion stopped because a configured resource ceiling was crossed. Its Kind field reports which one.

type LimitKind added in v0.2.2

type LimitKind = translate.LimitKind

LimitKind identifies which HTML conversion resource ceiling was exceeded.

type Limits

type Limits = htmllimits.Limits

Limits caps resource use while translating untrusted HTML.

func DefaultLimits

func DefaultLimits() Limits

DefaultLimits returns the safe resource limits used by FromString and FromReader unless overridden.

type Option

type Option func(*config)

Option configures FromString / FromReader behaviour.

func WithContentWidth

func WithContentWidth(mm float64) Option

WithContentWidth sets the page content width in mm for accurate gap-to-col approximation. Default is 170mm (A4 with 20mm left+right margins).

func WithFallbackFontPath added in v0.2.2

func WithFallbackFontPath(path string) Option

WithFallbackFontPath registers a font (local path, data: URI, or http(s) URL with WithRemoteAssets) loaded on demand when the document contains text outside the WinAnsi (cp1252) repertoire.

func WithGridSize

func WithGridSize(n int) Option

WithGridSize overrides the default 12-column grid for flex quantization. Use this when the paper document was built with config.WithMaxGridSize(n).

func WithHTTPClient added in v0.2.2

func WithHTTPClient(client *http.Client) Option

WithHTTPClient overrides http.DefaultClient for remote asset fetches.

func WithImageBaseDir

func WithImageBaseDir(dir string) Option

WithImageBaseDir scopes <img src=...> local-file reads to a single directory. Paths that escape via ".." or absolute prefix are refused.

func WithLimits

func WithLimits(l Limits) Option

WithLimits overrides resource limits for untrusted HTML translation. Zero fields keep their safe default values.

func WithMaxDepth added in v0.2.2

func WithMaxDepth(n int) Option

WithMaxDepth caps the DOM nesting depth translated. Exceeding the cap returns a *LimitError with Kind LimitDepth. Other limits keep their safe default values.

func WithMaxElements added in v0.2.2

func WithMaxElements(n int) Option

WithMaxElements caps the number of DOM nodes translated. Exceeding the cap returns a *LimitError with Kind LimitElements. Other limits keep their safe default values.

func WithOutlineFromHeadings added in v0.2.0

func WithOutlineFromHeadings() Option

WithOutlineFromHeadings adds h1-h6 headings to the PDF document outline: h1 becomes a level-0 entry, h2 a level-1 entry, and so on. Hidden headings are skipped.

func WithRemoteAssets added in v0.2.2

func WithRemoteAssets() Option

WithRemoteAssets enables http(s) fetching for document-referenced assets. Off by default so untrusted HTML cannot trigger network access.

func WithStrictAssets added in v0.2.2

func WithStrictAssets() Option

WithStrictAssets makes asset load failures (stylesheet links, @font-face sources, images, background images) surface as an error from FromString / FromReader. The partially-converted rows are still returned alongside the joined *AssetError values. Without this option failures warn-and-continue.

func WithStylesheetBaseDir

func WithStylesheetBaseDir(dir string) Option

WithStylesheetBaseDir scopes <link href=...> local-file reads to a single directory. Paths that escape via ".." or absolute prefix are refused.

func WithURLPolicy added in v0.2.2

func WithURLPolicy(policy URLPolicy) Option

WithURLPolicy registers a callback that can veto individual URLs before any remote fetch. Only consulted when WithRemoteAssets is enabled.

func WithUnsafeNoLimits

func WithUnsafeNoLimits() Option

WithUnsafeNoLimits disables resource caps. Use only for trusted input.

func WithUnsupportedHandler

func WithUnsupportedHandler(fn func(thing, value string)) Option

WithUnsupportedHandler registers a callback invoked for unsupported HTML tags or CSS properties. Use it to log diagnostics during development.

type PageOptions added in v0.2.0

type PageOptions = translate.PageOptions

PageOptions captures the supported subset of the CSS `@page` rule.

type ParseError added in v0.2.2

type ParseError = translate.ParseError

ParseError indicates the input HTML could not be parsed into a document.

type URLPolicy added in v0.2.2

type URLPolicy = translate.URLPolicy

URLPolicy vets a URL before any remote asset fetch. Returning a non-nil error blocks the fetch.

Directories

Path Synopsis
Package css parses CSS declarations and resolves them into a ComputedStyle that the HTML translator can read.
Package css parses CSS declarations and resolves them into a ComputedStyle that the HTML translator can read.
Package dom provides a Paper-friendly wrapper over golang.org/x/net/html.
Package dom provides a Paper-friendly wrapper over golang.org/x/net/html.
Package translate converts a parsed HTML DOM into a slice of Paper rows.
Package translate converts a parsed HTML DOM into a slice of Paper rows.

Jump to

Keyboard shortcuts

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