htmlbag is the HTML/CSS renderer within the boxes and glue stack. It parses CSS
stylesheets, matches them against an HTML document, and turns the styled DOM
into internal text and node structures (frontend.Text, node.VList, etc.)
that are later shipped to PDF. bagme
and the HTML mode of glu are built on it.
The CSS parser and selector engine used to be the separate package
github.com/boxesandglue/csshtml, now archived. Its identifiers kept their
names, so csshtml.NewCSSParserWithDefaults became
htmlbag.NewCSSParserWithDefaults.
go get github.com/boxesandglue/htmlbagThe htmlbag manual describes the supported HTML elements and CSS properties, tables, floats, footnotes, cross references and the Go API.
- CSS:
css.goparses stylesheets (including@import,@font-faceand@page),tree.gomatches the rules against the DOM via cascadia and writes the computed styles onto the nodes,processhtml.goloads HTML files or chunks together with their linked stylesheets,nesting.goflattens CSS nesting. CSSBuilder(cssbuilder.go): owns afrontend.Documentand aCSS, parses HTML (ParseHTMLFromNode/HTMLToText), applies CSS, and builds vlists.- Styles:
inheritablestyles.gomodels CSS inheritance; list markers, indents, and table handling live here and inhtmltable.go. - Rendering:
vlistbuilder.gobuilds vertical lists fromfrontend.Text;output.goships pages via the frontend/pdfdraw backend. - Fonts:
fonts.goloads embedded webfonts; assets live underfonts/.
css := htmlbag.NewCSSParserWithDefaults()
_ = css.AddCSSText(`@page { size: A4; } body { font-family: serif; }`)
doc := frontend.NewDocument()
cb, _ := htmlbag.New(doc, css)
te, _ := cb.HTMLToText(`<p><b>Hello</b> world</p>`)
vl, _ := cb.CreateVlist(te, bag.MustSP("15cm"))
doc.Doc.OutputAt(0, 0, vl) // example: output directly- Go 1.25+. Import path:
github.com/boxesandglue/htmlbag. - Tests:
go test ./.... - Changes in the CSS/HTML pipeline should be validated against PDF references (downstream projects rely on stable output).
htmlbag is part of a broader ecosystem of PDF, typesetting and publishing technologies.