I build small tools that make things to print: paper houses, gift boxes, cards. A paper house only folds up properly if a 70 mm wall comes out of the printer at 70 mm. This post is about the pattern the tools use for that, with no server involved. The code below is from the paper Christmas village tool.
Disclosure: these tools are built with the help of AI coding tools, and this post was written by an AI assistant from the real source code.
The problem
The page needs three things from the same design:
- a live preview on a
<canvas>, - a 300 dpi PNG,
- a PDF that prints at true size.
If the preview is drawn one way and the PDF another, they drift apart: a tab moves in one and not in the other, and nobody notices until someone prints it.
The fix: describe the sheet once, in millimetres
The layout code never touches a canvas or a PDF. It returns a plain list of drawing operations, and every number in it is in millimetres with y pointing down. Two operations look like this (the values here are made up for the example):
{ t: 'path', d: 'M10 10 L80 10 L80 57 Z', fill: '#f3e6c8', stroke: '#222222', w: 0.3, dash: [2, 1] } { t: 'text', text: 'Glue here', x: 45, y: 120, size: 3, anchor: 'start' } Two small functions consume that list. Because both read the same data, the preview, the PNG and the PDF cannot disagree.
Output 1: the PDF
A PDF measures in points, 72 to the inch, and its origin is the bottom-left corner. So the whole conversion is one constant and one flip:
var K = 72 / 25.4; // points per millimetre var page = doc.addPage([P.w * K, P.h * K]); // SVG path data in mm, y down: scale by K from the page's top-left page.drawSvgPath(op.d, { x: 0, y: P.h * K, scale: K, borderWidth: op.w }); drawSvgPath in pdf-lib takes SVG path data with y pointing down, so anchoring it at the page's top-left with scale: K is enough. Line widths and dash lengths stay in millimetres too, because they are scaled with the path.
Everything is drawn as vectors, so the cut lines are sharp at any zoom.
Output 2: the canvas preview and the PNG
The same list, painted at some number of pixels per millimetre:
function paint(ctx, ops, px, P) { ctx.fillStyle = '#ffffff'; ctx.fillRect(0, 0, P.w * px, P.h * px); ctx.scale(px, px); ops.forEach(function (op) { if (op.t === 'path') { var path = new Path2D(op.d); if (op.fill) { ctx.fillStyle = op.fill; ctx.fill(path); } // stroke, dash and text follow the same shape } }); } For the preview px is whatever fits the screen. For a 300 dpi PNG it is 300 / 25.4. Path2D accepts the same SVG path string that pdf-lib does, which is what makes the shared list cheap.
Details worth knowing
Fonts. The standard Helvetica in a PDF only speaks WinAnsi. Polish ł ś ż are simply not there. The English and French pages swap the few missing punctuation characters and use the built-in font, so no font file is embedded. The Polish page hands over fontkit and two Roboto files, and they are embedded with { subset: true }.
Load pdf-lib late. The library is only fetched when someone presses "Download PDF", so looking at the preview costs nothing extra.
Text fitting. Names are typed by the user, so text has an optional maxW. Both outputs measure the string and shrink the size to fit: font.widthOfTextAtSize() in the PDF, ctx.measureText() on the canvas.
Tell people to print at 100 %, and let them check. All of this is wasted if the print dialog is on "Fit to page". The page says to choose "Actual size", and every sheet carries a 50 mm bar at its foot, so a ruler shows at once whether the printer scaled it.
Make it testable. The module is UMD, so the same file loads in Node. A test can build the PDF and check the page size without opening a browser.
Why I like it
A new design is only new data: the layout code returns a different list, and the PDF, PNG and preview code do not change.
If you have solved the true-size printing problem another way, I would like to hear how.