We shipped a poster maker: pub-trivia.app/tools/quiz-night-poster-maker. You type the venue, the night, the entry fee and the prize, pick one of four themes, optionally give it a link to turn into a QR code, and press a button to get an A4 PDF to print and put on the door. No sign-up and no backend: the poster is laid out and rendered in your browser.
The interesting part is not the PDF. It is that the poster is drawn twice. Once as an SVG, so you can see it change as you type, and once by jsPDF when you press Download. Two renderers that both have to produce the same poster, or the preview stops being a preview.
One layout, two renderers
The way to stop them drifting is to not let either of them do layout. The layout is worked out once, in millimetres on a 210 by 297 page, and comes out as a flat list of things to draw:
export type PosterRect = { kind: 'rect'; x: number; y: number; w: number; h: number; fill?: string; /* ... */ }
export type PosterText = { kind: 'text'; text: string; x: number; y: number; size: number; style: FontStyle; color: string }
export type PosterQr = { kind: 'qr'; x: number; y: number; size: number }
export type PosterElement = PosterRect | PosterText | PosterQr
x on a text element is the centre of the line, because every line on this poster is centred, and y is the baseline. The SVG renderer maps those to <text text-anchor="middle">, and the PDF renderer maps them to doc.text(..., { align: 'center' }). Neither of them decides anything.
The default poster is 12 elements: one page rect, one inset frame, one accent rule under the venue name, and nine lines of text. Adding a QR code makes it 15, because the code brings a white panel behind it and a caption. The ink saver theme is 16, since it draws two coloured bars instead of a background. Fill every field to its limit and it is 23.
Text width is the one thing a layout function cannot know
Wrapping, shrinking and centring all need to know how wide a string is, and that depends on the font, which depends on who is drawing. So the width function is an argument:
export type Measure = (text: string, style: FontStyle, sizePt: number) => number
There are three implementations in the repository.
The PDF uses jsPDF's own metrics, which is the only honest answer for a PDF, since the file declares Helvetica and the viewer sets it:
doc.setFont('helvetica', style)
doc.setFontSize(size)
return doc.getTextWidth(text)
The preview uses a canvas. It sets 100px Helvetica, Arial, sans-serif, measures the string once, caches the result per string and style, and scales. On a Mac that measures Helvetica itself; on Windows it measures Arial, which has the same widths, and that metric compatibility is the entire reason a canvas may stand in for a PDF at all.
The third is an estimate, 13 lines of per-character widths, used when neither of the other two exists: on the server, and in the tests as a sanity check.
Text jsPDF Helvetica the estimate over by
QUIZ NIGHT 89.7mm 108.3mm 20.7%
Every Tuesday 105.9mm 119.8mm 13.1%
£50 bar tab 81.5mm 92.1mm 13.1%
Teams of up to 6 122.9mm 136.9mm 11.4%
The Red Lion 98.1mm 103.7mm 5.7%
All at 44pt bold. The estimate is wrong in a useful direction, always too wide, so text fitted with it never overflows. But 20.7 per cent on an all-capitals headline is a lot, and you can watch what it costs.
Exactly one line changes when the page hydrates
The page is server rendered, so the SVG arrives in the HTML, with the sizes the estimate chose. Then the component mounts, builds its canvas measurer and lays the poster out again.
Here is the whole difference, read off the same page twice:
$ curl -s https://pub-trivia.app/tools/quiz-night-poster-maker \
| grep -o 'font-size="[0-9.]*"' | sort -u
font-size="14.11111111111111"
font-size="15.52222222222222"
font-size="17.638888888888886"
font-size="24.87083333333333" <- QUIZ NIGHT, 70.5pt
font-size="4.58611111111111"
font-size="6.35"
font-size="9.877777777777776"
Then in the browser, after hydration, the headline is font-size="29.986111111111107", which is 85pt. The other eight lines are byte for byte identical.
That is not luck. The venue name, the night, the start time, the entry and prize values, their labels and the team line are all short enough to sit at their maximum size, so measuring them better does not change the answer. The headline is the one line on the poster whose size was actually decided by a measurement: fitText walks from 96pt down to 40pt in half point steps, 113 candidate sizes, and takes the first one that fits on one line. The estimate thinks QUIZ NIGHT needs 108.3mm of the 174mm available at 96pt, so it stops at 70.5. The canvas and jsPDF both say 85pt fits, and they agree with each other exactly, which is the metric compatibility paying for itself.
So the poster reflows once, on one line, and then matches the PDF. You can see it if you throttle the network and watch the headline jump.
Nothing a visitor types can leave the page
This is a public tool with no account behind it, so the layout has to survive anything typed into it. Three layers do that.
Every field is clamped to a length. Eight text fields, 370 characters between them, plus 300 for the URL:
export const POSTER_LIMITS = {
venue: 60, when: 40, startTime: 20, entryFee: 30,
prize: 50, teams: 50, tagline: 90, qrUrl: 300, qrCaption: 30,
}
Every block has a maximum number of lines and a size range, so a long venue name shrinks and wraps rather than growing the stack:
The Red Lion 12 chars -> 44pt, 1 line
The Royal Oak and Harbour Inn 29 chars -> 44pt, 2 lines
The Ancient Order of Froth Blowers Social Club 46 chars -> 39.5pt, 2 lines
And then the whole middle stack is laid out at a scale from 1.00 down to 0.50 in steps of 0.05, taking the first scale at which it fits the space between the headline and the QR code.
That last layer, 11 steps of it, is almost entirely dead. With every field at its limit, with a measurer where each character is as wide as a capital W, the stack still fits at scale 1.00, because fitText has already shrunk each block individually. The only input I could find that reaches step two is contrived: a venue, tagline and team line made of two letter words, so wrapping produces the maximum number of lines at a size the per-block shrink is happy with. That lands at 0.95. I have kept the other nine steps anyway. They cost one loop and they are the only thing standing between a field combination nobody has tried and text on top of a QR code.
What jsPDF's built-in Helvetica cannot draw
The PDF declares the base 14 fonts and embeds nothing, which keeps the download small and the fonts to what a viewer already has. The cost is the character set: Latin-1 plus the extra punctuation in Windows-1252, 27 characters of it, and nothing else. Anything outside that comes out as junk, so it is dropped before layout rather than printed wrongly:
"Quiz Night 🍺 at Café Zürich, 8pm" 32 chars -> 31 (the pint goes, Café and Zürich stay)
"£2 entry, €50 prize" 19 chars -> 19 (both currency symbols survive)
"Tuesdays at 8pm" 20 chars -> 15 (runs of whitespace collapse)
And the honest one:
"クイズナイト" 6 chars -> 0
A venue that types its name in Japanese gets an empty field and no warning. That is a real limitation of a tool built on the base 14 fonts, and the fix is not a clever one: embed a font with the coverage, pay the bytes, lose the "nothing to download" property of the page. For now the tool is honest about what it is, English and Latin script, and I would rather write that down than pretend the set is universal.
The tests are the pure part
lib/tools/quiz-night-poster.ts is 467 lines and imports nothing. The component is 382 lines and does the DOM work. The test file is 193 lines, 16 tests, and it does two things worth copying.
It measures with the real thing. The test file constructs a jsPDF document and uses getTextWidth, so the assertions are about the font the PDF will actually be set in, not an approximation of it.
And it has an adversarial measurer, every character as wide as a capital W:
const widest: Measure = (text, _style, size) =>
Array.from(text).length * 0.95 * size * PT_TO_MM
Then the assertion that matters, run for all four themes, with and without a QR code, for both an ordinary poster and one with every field at its limit: every text element's measured box is inside the page margins, and the QR code's bottom edge is above 297mm. Not a snapshot of the layout, which would fail on every design tweak, but the one property the design is not allowed to break.
Have a go
Open the poster maker with the Network tab open and type. I said nothing goes out, and then I checked, so here is the accurate version: typing a 46 character venue name produced exactly one request, to our own /monitoring path, which is the tunnel our error reporting posts performance traces through. It carries timings, not your poster. No other request fires on a keystroke or a theme change, and the only thing the Download button fetches is the jsPDF chunk, plus the qrcode chunk if you gave it a link, because both are dynamic imports and nobody who leaves without pressing it pays for it.
While you are in there, paste that 46 character venue name in and watch the preview go to two lines at 39.5pt, which reads as font-size="13.934722222222222" in the SVG. Then paste an emoji into the tagline and watch it vanish. Both are the code above doing what it says.
The rest of the free tools, a QR code sheet, a scoresheet generator, a round planner, a team name generator and a profit calculator, are at pub-trivia.app/tools, and none of them have a backend either.
Top comments (0)