Documentation
¶
Overview ¶
Package html2pdf renders static HTML straight to a vector PDF: it drives go-webengine's own layout tree (no screenshot, no raster slicing) into go-pdfkit text/rect/stroke calls. Pagination breaks between atoms — a text line, or a whole table row — never through one; see atoms.go.
Scope ¶
This is a static renderer: no JavaScript, no external stylesheets or @font-face (text uses the three families go-webengine's own paint package bundles — Inter for sans, Lora for serif, Go Mono for mono — so the glyphs drawn always match the metrics layout measured against). Inline-level background/border/padding do not paint: go-webengine's layout does not give a styled inline run its own box, only block/table/flex-level elements do (confirmed in the reference raster painter too — this is a shared engine limitation, not something this package works around).
Images — raster <img>, <img src="*.svg"> and inline <svg> — are fetched, decoded and sized by the engine's own pipeline (Engine.LoadImages) and embedded as bitmaps, so they are laid out and drawn exactly as the engine's raster canvas would. A relative src resolves against Options.BaseURL; an image that fails to fetch or decode is simply left out. This is the one place Export touches the network.
Quick start ¶
doc, err := html2pdf.Export(htmlSource, html2pdf.Options{})
if err != nil { ... }
f, _ := os.Create("out.pdf")
defer f.Close()
doc.Write(f)
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Options ¶
type Options struct {
PageSize pdfkit.PageSize // zero value: pdfkit.A4
MarginMm float64 // zero value: 20
// ViewportPx is the width (CSS px) the page is laid out against, then
// uniformly scaled down to fit the print column. Many real pages carry a
// fixed-width element sized for a desktop viewport — a sidebar, a
// multi-column nav — that a browser's own responsive CSS only collapses
// below some breakpoint. Laying out directly at the print column's actual
// width (a plain A4 page is under 650px wide) sits below most such
// breakpoints, so that fixed-width element squeezes the rest of the page
// into a narrow remainder and the whole document wraps far taller than it
// needs to — confirmed against RFC 9110's HTML edition, whose
// table-of-contents sidebar did exactly this (428 pages laid out at the
// print column's own ~642px width vs. 184 at 1024px). Zero value: 1024,
// a common small-desktop/tablet breakpoint. Set below the print column's
// own width (rare) to lay out 1:1 with no scaling.
ViewportPx float64
// BaseURL is the document's own URL, used to resolve a relative <img src>
// (and to satisfy same-origin-shaped fetch logic in the engine). Leave it
// empty for a document whose images are all absolute or data: URIs.
BaseURL string
}
Options configures a single Export call. The zero value is A4, 20mm margins on all sides, and a 1024px layout viewport.