bagme

module
v0.0.0-...-98482bf Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2026 License: BSD-3-Clause

README

bagme - boxes and glue made easy

bagme is a Go library that renders HTML/CSS to PDF using the boxes and glue typesetting engine. It implements TeX's line-breaking and page-breaking algorithms, so you get high-quality typesetting with almost no effort.

Installation

go get github.com/boxesandglue/bagme

Render a complete HTML document

Use RenderPages for automatic page breaks, @page margins, and multi-page output:

package main

import (
	"log"

	"github.com/boxesandglue/bagme/document"
)

func main() {
	d, err := document.New("output.pdf")
	if err != nil {
		log.Fatal(err)
	}
	d.AddCSS(`
		@page { size: A4; margin: 2cm; }
		body { font-family: serif; font-size: 11pt; line-height: 1.4; }
		h1 { page-break-before: always; }
	`)
	if err := d.RenderPages(`
		<h1>Chapter 1</h1>
		<p>In olden times when wishing still helped one, there lived a king
		whose daughters were all beautiful...</p>

		<h1>Chapter 2</h1>
		<p>Close by the king's castle lay a great dark forest, and under an
		old lime-tree in the forest was a well...</p>
	`); err != nil {
		log.Fatal(err)
	}
	if err := d.Finish(); err != nil {
		log.Fatal(err)
	}
}

Place HTML snippets at exact positions

Use OutputAt for precise placement on a page (labels, forms, letterheads):

package main

import (
	"log"

	"github.com/boxesandglue/bagme/document"
	"github.com/boxesandglue/boxesandglue/backend/bag"
)

func main() {
	d, err := document.New("out.pdf")
	if err != nil {
		log.Fatal(err)
	}
	d.AddCSS(`body { font-family: serif; font-size: 12pt; }`)

	ps, _ := d.PageSize()
	w := ps.ContentWidth
	x := ps.MarginLeft
	y := ps.Height - ps.MarginTop

	d.OutputAt("<h1>Hello, World!</h1><p>Placed at exact coordinates.</p>", w, x, y)
	d.Finish()
}

Features

  • Automatic page breaks with CSS @page rules
  • page-break-before / page-break-after (always, avoid)
  • Page margin boxes (@top-center, @bottom-right, etc.) for headers/footers
  • CSS styling: fonts, colors, margins, padding, borders (rounded), backgrounds
  • Tables with colspan/rowspan, borders, and cell backgrounds
  • Ordered and unordered lists
  • Images (PDF, PNG) and inline SVG
  • OpenType features and variable fonts via font-feature-settings / font-variation-settings
  • TeX-quality line breaking (Knuth-Plass algorithm)
  • Heading extraction for table of contents generation
  • Pure Go — no C dependencies, no browser, single binary

Limitations

  • No automatic page breaks within a single paragraph (breaks happen between block elements)
  • Limited CSS support compared to a full browser engine
  • No floats or flexbox/grid layout

Contact

Contact: gundlach@speedata.de
License: New BSD License
Status: Beta — API may change.
Mastodon: boxesandglue@typo.social

Directories

Path Synopsis
fonts/camingocodebold
Package camingocodebold provides the "CamingoCode" font
Package camingocodebold provides the "CamingoCode" font
fonts/camingocodebolditalic
Package camingocodebolditalic provides the "CamingoCode" font
Package camingocodebolditalic provides the "CamingoCode" font
fonts/camingocodeitalic
Package camingocodeitalic provides the "CamingoCode" font
Package camingocodeitalic provides the "CamingoCode" font
fonts/camingocoderegular
Package camingocoderegular provides the "CamingoCode" font
Package camingocoderegular provides the "CamingoCode" font
fonts/crimsonprobold
Package crimsonprobold provides the "Crimson Pro" font
Package crimsonprobold provides the "Crimson Pro" font
fonts/crimsonprobolditalic
Package crimsonprobolditalic provides the "Crimson Pro" font
Package crimsonprobolditalic provides the "Crimson Pro" font
fonts/crimsonproitalic
Package crimsonproitalic provides the "Crimson Pro" font
Package crimsonproitalic provides the "Crimson Pro" font
fonts/crimsonproregular
Package crimsonproregular provides the "Crimson Pro" font
Package crimsonproregular provides the "Crimson Pro" font
fonts/texgyreherosbold
Package texgyreherosbold provides the "TeXGyreHeros" font
Package texgyreherosbold provides the "TeXGyreHeros" font
fonts/texgyreherosbolditalic
Package texgyreherosbolditalic provides the "TeXGyreHeros" font
Package texgyreherosbolditalic provides the "TeXGyreHeros" font
fonts/texgyreherositalic
Package texgyreherositalic provides the "TeXGyreHeros" font
Package texgyreherositalic provides the "TeXGyreHeros" font
fonts/texgyreherosregular
Package texgyreherosregular provides the "TeXGyreHeros" font
Package texgyreherosregular provides the "TeXGyreHeros" font

Jump to

Keyboard shortcuts

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