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 doneimport * 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.
Loading happypdf…
Arabic
Arabic letters take different forms depending on their neighbours, and the script runs right to left. Both fall out of shaping.
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.
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.
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.
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.
Loading happypdf…
Multi-page documents
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.
Loading happypdf…
OpenType features
Feature tags are forwarded to HarfBuzz. Compare the default rendering with ligatures and kerning switched off.
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.
Loading happypdf…
Merging documents
Loading happypdf…
Drawing and shapes
Loading happypdf…
Vector paths
drawSvgPath renders vector paths directly, with no rasterisation.
Loading happypdf…
Forms
Fields can be created, filled and given appearances. Multiline fields wrap with the same segmenter drawText uses.
Loading happypdf…