When I added printable Sudoku sheets to Quietdoku, my first instinct was to reach for pdf-lib, jsPDF, or a server-side Puppeteer worker.
None of them felt right:
- Puppeteer / headless Chrome adds server cost, cold starts, and heavy memory usage just to draw lines and digits on an A4 sheet.
- Client-side PDF libraries ship hundreds of kilobytes of JS (plus font subsets) for something whose entire visual vocabulary is straight lines, dashed lines, solid rectangles, and Helvetica digits
1–9.
It turns out that raw PDF-1.4 is shockingly readable when you stick to the built-in Type1 Helvetica and Helvetica-Bold fonts and pure ASCII text. The entire generator—supporting A4 and US Letter, 1/2/4/6 puzzles per page, multi-page booklets with grouped answer keys at the back, Killer Sudoku dashed cages, and a vector QR code—ended up being 189 lines of TypeScript with zero dependencies.
And because many people still press Cmd+P / Ctrl+P directly in the browser, I also built a WYSIWYG print workbench—which exposed four subtle Chromium print-engine bugs on real inkjet printers. Here is everything I learned from both sides.
Part 1: Raw PDF-1.4 in 30 Lines of Assembly
A valid PDF is just numbered objects, a byte-offset table (xref), and a trailer. If your content streams are pure ASCII, JavaScript string .length equals the exact UTF-8 byte length, so calculating xref offsets takes five lines:
export function assemblePdf(pages: string[], w: number, h: number, title: "string): Uint8Array {" const objects: string[] = []; objects[1] = '<< /Type /Catalog /Pages 2 0 R >>'; objects[3] = '<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica /Encoding /WinAnsiEncoding >>'; objects[4] = '<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica-Bold /Encoding /WinAnsiEncoding >>'; objects[5] = `<< /Title (${pdfText(title)}) /Author (Quietdoku) /Creator (quietdoku.com) >>`; const kids: string[] = []; pages.forEach((content, i) => { const pageId = 6 + i * 2; kids.push(`${pageId} 0 R`); objects[pageId] = `<< /Type /Page /Parent 2 0 R /MediaBox [0 0 ${num(w)} ${num(h)}] ` + `/Resources << /Font << /F1 3 0 R /F2 4 0 R >> >> /Contents ${pageId + 1} 0 R >>`; objects[pageId + 1] = `<< /Length ${content.length} >>\nstream\n${content}\nendstream`; }); objects[2] = `<< /Type /Pages /Kids [${kids.join(' ')}] /Count ${pages.length} >>`; let out = '%PDF-1.4\n'; const offsets: number[] = []; for (let id = 1; id < objects.length; id++) { offsets[id] = out.length; out += `${id} 0 obj\n${objects[id]}\nendobj\n`; } const xref = out.length; out += `xref\n0 ${objects.length}\n0000000000 65535 f \n`; for (let id = 1; id < objects.length; id++) { out += `${String(offsets[id]).padStart(10, '0')} 00000 n \n`; } out += `trailer\n<< /Size ${objects.length} /Root 1 0 R /Info 5 0 R >>\nstartxref\n${xref}\n%%EOF\n`; return new TextEncoder().encode(out); } Drawing Grids, Killer Cages, and Vector QR Codes
PDF coordinates start at the bottom-left (y = 0 at the bottom edge). A few tiny helpers make drawing crisp puzzles effortless:
- Visual hierarchy matters on paper: Thin internal lines are light gray (
0.55 G 0.45 w), while 3×3 box dividers and the outer border are pure black and thick (0 G 2 w). For Killer Sudoku, internal cell borders drop even lighter (0.68 G 0.4 w) so the dashed cage borders ([dash gap] 0 d) stand out cleanly. - Multi-page booklets: Collect all
puzzlePagesfirst, then allanswerPagesat the back, incrementing the puzzle counter continuously (Puzzle 1..30, thenAnswer 1..30) so players don't accidentally see solutions while turning pages. - Vector QR codes: Instead of embedding a raster PNG (which needs deflate streams and turns fuzzy on cheap printers), iterate over the QR boolean matrix and emit solid filled rectangles (
x y w h re f) at0 g(100% black):
function qrOps(qr: string[], right: number, top: number, size: number): string[] { const m = size / qr.length; const ops = ['0 g']; qr.forEach((row, r) => { for (let c = 0; c < row.length; c++) { if (row[c] === '1') { ops.push(`${num(right - size + c * m)} ${num(top - (r + 1) * m)} ${num(m)} ${num(m)} re f`); } } }); return ops; } The same code runs both at build time (generating static A4/Letter PDFs) and live in the browser when a user clicks Download PDF on a freshly shuffled 5-page booklet—generating a crisp multi-page PDF in under 2 milliseconds.
Part 2: Four Browser @media print Bugs I Hit on Real Printers
Direct PDF download works great, but many users still hit Cmd+P / Ctrl+P in Chrome. Getting browser print to match a real HP DeskJet inkjet printer taught me four hard lessons:
1. Never Gate Printable Markup Behind a JS Click Handler or display: flex + break-inside: avoid
Originally I used a React portal plus an html.pw-printing class added on button click, with display: flex; break-inside: avoid on each .paper page.
Two things broke immediately:
- Pressing
Cmd+Pbypassed the click handler and printed a completely blank page. - On inkjet printers with wider non-printable hardware margins (like an HP DeskJet), a
flexcontainer withbreak-inside: avoidthat exceeded the printable height by even 2mm caused Chromium to push the entire sheet off page 1—again producing a blank preview.
Fix: Keep the print DOM mounted unconditionally (display: none on screen, @media print { display: block !important; }), use display: block on each .paper sheet, and never put break-inside: avoid on the full page wrapper.
2. Chromium's Print Engine Ignores aspect-ratio: 1 Inside Grid/Flex Containers
On screen, a 9×9 CSS Grid with aspect-ratio: 1 renders as a perfect square. In Chromium's print layout pass, aspect-ratio: 1 frequently collapses vertically or stretches awkwardly when printing 2-per-page large-print grids.
Fix: Set explicit width and height in container-query units (cqi) on the grid wrapper (for example, width: 36.2cqi; height: 36.2cqi; for A4 6-per-page, or width: 57cqi; height: 57cqi; for 2-per-page large print). Both screen preview and print output now render at identical proportions.
3. Don't Use border-color Shorthand for 3×3 Sudoku Box Dividers
If your 3×3 thick dividers use border-color: #000 on .cell.col-3, you accidentally paint all four sides of that cell dark black—turning the 3×3 line into a muddy double-bordered stripe on paper. Target only border-right and border-bottom (2.4px solid #000) against light interior borders (0.75px solid #bcbcbc).
4. Inkjet Printers Destroy Tiny QR Codes
A 9.5mm QR code looks crisp on a Retina display and even in macOS Preview, but a home inkjet printer using CMYK dithering turns dark-gray (#1a1d22) sub-millimeter modules into an unscannable blur.
Fix:
- Enlarge printed QR codes to 17–18mm square (
48ptin PDF,9.5cqiin browser print). - Add an explicit pure-white quiet zone (
0.9cqipadding). - Force
shape-rendering: crispEdgesand pure#000000ink in@media printso the printer uses pure black pigment instead of composite CMYK dots.
Try It Live
You can test both the zero-dependency PDF generator and the browser print layout (including 2-per-page large print and 1/3/5-page booklets) here—no sign-up and no ads:
- Printable Sudoku & Killer Sudoku Workbench: https://quietdoku.com/printable
- Play Ad-Free Online (Classic & Killer Sudoku): https://quietdoku.com/no-ads
- Interactive Killer Sudoku Cage Combination Calculator: https://quietdoku.com/killer/how-to-play#combinations
If you've wrestled with Chromium's @media print quirks or hand-rolled PDF streams for other tools, I'd love to hear what tricks worked for you.