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 and no @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). External stylesheets — <link rel="stylesheet"> and their @import chains — are fetched through the engine's own bounded loader (Engine.LoadStylesheets) and cascaded for the print medium by default (Options.Media), so a page's @media print rules apply and its screen-only ones do not, as in a browser's print preview. Inline-level background and borders paint per line fragment from the engine's own LineBox.Inlines (box-decoration-break: slice); border-radius, background-image and box-shadow on an inline element do not.
Images — raster <img>, <img src="*.svg"> and inline <svg> — are fetched, decoded and sized by the engine's own pipeline (Engine.LoadImageSet) and embedded so they are laid out and drawn exactly as the engine's raster canvas would: a JPEG source as its own bytes (DCTDecode), any other lossy source re-encoded as JPEG when opaque, everything else as a flate bitmap with a soft mask for transparency — see images.go and Options.ImageDPI. A relative src resolves against Options.BaseURL; an image that fails to fetch or decode is simply left out. Stylesheets and images are the two places Export touches the network.
Navigation ¶
An <a href> becomes a link annotation — a URI action for an http(s) target, a GoTo to a named destination for a fragment that points at an element id in the document — one clickable rectangle per line the anchor spans, or the box of an anchor that lays out no text (engine.LinkRuns). Every element id becomes a named destination, and the headings become the viewer's bookmark tree (<h1> at the top level, deeper headings nested under the last shallower one). The <title> fills the PDF's Title unless Options.Title is set.
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 a relative <a href> (and to satisfy same-origin-shaped fetch logic
// in the engine). A link whose href is this URL plus a fragment becomes an
// in-document jump. Leave it empty for a document whose images and links
// are all absolute or data: URIs.
BaseURL string
// Title, Author, Subject and Keywords fill the PDF's information
// dictionary. An empty Title is taken from the document's <title>.
Title, Author, Subject, Keywords string
// ImageDPI caps the pixel density of an embedded bitmap at its painted
// size: a bitmap that would exceed it — a 1024 px photograph painted
// 60 mm wide is 430 dpi — is downsampled to it. Zero (the default) keeps
// every pixel the engine fetched, which is what Chrome's print and
// WeasyPrint do by default; WeasyPrint's --dpi is the same lever. 150
// is a sound print value, 96 the screen's.
ImageDPI float64
// Media is the CSS medium the page is styled for: "print" (the zero
// value) applies the page's @media print rules and print-only
// stylesheets and skips its screen-only ones — a browser's print
// preview, where a site's navigation, sidebars and footers are usually
// hidden; "screen" styles the page as displayed. Width features
// (min-width, max-width) are evaluated at ViewportPx under either.
Media string
}
Options configures a single Export call. The zero value is A4, 20mm margins on all sides, and a 1024px layout viewport.