Skip to content

Live Demos ​

Every demo on this page runs in your browser — no install, no server, no build step. Edit the code and press Run (or ⌘/Ctrl + Enter) to re-render the PDF beside it.

Each snippet receives two things:

  • happypdf — the library, as if you had done import * as happypdf from 'happypdf'
  • fonts — preloaded font bytes, keyed by name

and must return a PDFDocument.

Complex scripts ​

Khmer shaping ​

The reason this fork exists. Khmer stacks consonants into orthographic clusters and reorders vowels around them — shaping the font engine has to perform. Try adding your own Khmer text.

⌘/Ctrl + Enter

Loading happypdf…

Arabic ​

Arabic letters take different forms depending on their neighbours, and the script runs right to left. Both fall out of shaping.

⌘/Ctrl + Enter

Loading happypdf…

Thai wrapping ​

Thai does not put spaces between words either. Intl.Segmenter finds the break opportunities, so wrapping and justification land in sensible places.

⌘/Ctrl + Enter

Loading happypdf…

Justifying a script without spaces ​

The same idea in Khmer: the top block is ragged right, the bottom is justified. Gaps open at word boundaries rather than between letters.

⌘/Ctrl + Enter

Loading happypdf…

Text layout ​

Wrapping and alignment ​

maxWidth wraps the text; align decides how the lines sit within it. Change align in the loop, or narrow maxWidth, and re-run.

⌘/Ctrl + Enter

Loading happypdf…

Measuring before drawing ​

wrapText returns the line boxes without drawing anything, so you can size a container to its contents — here a box drawn to exactly fit the text.

⌘/Ctrl + Enter

Loading happypdf…

Multi-page documents ​

⌘/Ctrl + Enter

Loading happypdf…

Fonts ​

Variable fonts ​

variations instances a variable font on its axes, and the instanced outlines are what gets embedded — so the weight you ask for is the weight that renders.

⌘/Ctrl + Enter

Loading happypdf…

OpenType features ​

Feature tags are forwarded to HarfBuzz. Compare the default rendering with ligatures and kerning switched off.

⌘/Ctrl + Enter

Loading happypdf…

Documents ​

Editing an existing PDF ​

Load a document, then stamp it. This is the flow you would use on a file fetched from a server.

⌘/Ctrl + Enter

Loading happypdf…

Merging documents ​

⌘/Ctrl + Enter

Loading happypdf…

Drawing and shapes ​

⌘/Ctrl + Enter

Loading happypdf…

Vector paths ​

drawSvgPath renders vector paths directly, with no rasterisation.

⌘/Ctrl + Enter

Loading happypdf…

Forms ​

Fields can be created, filled and given appearances. Multiline fields wrap with the same segmenter drawText uses.

⌘/Ctrl + Enter

Loading happypdf…

MIT Licensed. A fork of pdf-lib.