Dev.to WebDev 🛠 Dev 👁 0 📖 3 min read

How I Built a Zero-Dependency Pure-JS Vector PDF 1.4 Engine for Print-Ready Puzzle Books

If you have ever tried building a print-ready puzzle book in the browser, you probably ran into two walls quickly: either you pay $19–$47/mo for a SaaS subscription just to export interiors, or you build a quick HTML5 <c

How I Built a Zero-Dependency Pure-JS Vector PDF 1.4 Engine for Print-Ready Puzzle Books

PuzzlePack Studio browser-local vector PDF workspace

If you have ever tried building a print-ready puzzle book in the browser, you probably ran into two walls quickly: either you pay $19–$47/mo for a SaaS subscription just to export interiors, or you build a quick HTML5 <canvas> generator, rasterize each page to a PNG, and wrap those bitmaps in a PDF.

On screen, <canvas> -> PNG -> PDF looks fine. On 300 DPI Amazon KDP paper, it immediately looks soft. Thin grid borders anti-alias into muddy gray pixels, small clue numbers blur when scaled, and a 150-page book balloons into tens of megabytes of compressed raster images.

That frustration is why I built PuzzlePack Studio at LessworkApps: a 100% browser-local puzzle book engine written in vanilla ES modules with zero server uploads, zero framework dependencies, and a custom zero-dependency PDF 1.4 vector writer.

Here is how the four core engineering pieces fit together under the hood.

1. Raw PDF 1.4 Vector Writer (puzzlepack-pdf.js)

Instead of pulling in heavy PDF libraries or rasterizing canvases, puzzlepack-pdf.js constructs a valid PDF 1.4 byte stream directly in memory using standard PostScript-style drawing operators (m, l, S, re, BT, ET, Tf, Td, Tj).

PDF coordinates run at 72 points per inch (72 pt = 1 in) from a bottom-left origin. That makes exact Amazon KDP trim boxes straightforward to express:

  • 6 × 9 in trim: /MediaBox [0 0 432 648]
  • 8.5 × 11 in trim: /MediaBox [0 0 612 792]

Every Sudoku cage, maze wall, and word-search cell is emitted as crisp vector strokes with explicit line widths, while numbers and letters use Type 1 Helvetica metrics centered mathematically inside each cell bounding box. Because every path and glyph is true vector data, the interior stays razor-sharp at arbitrary zoom and on 300 DPI print plates—while keeping file sizes tiny. The writer tracks object byte offsets in an xref table so the generated Blob opens cleanly in Acrobat, Preview, Chrome, and KDP's interior preflighter.

2. Unique-Solution 9×9 Sudoku with Backtracking Verification

A print puzzle cannot have two valid solutions. Generating a filled 9×9 grid is straightforward; removing clues while preserving uniqueness requires strict verification.

The Sudoku engine first builds a randomized complete board via constraint-aware backtracking, then removes clues across candidate passes. After each tentative clue removal, a dedicated solver counts solutions and short-circuits as soon as it finds a second valid completion. Only removals that maintain a strict solution count of 1 are kept.

Across Easy, Medium, Hard, and Expert tiers, difficulty is shaped by both clue floors and constraint depth—so Expert grids genuinely require deeper candidate elimination rather than simply looking sparse.

3. Algorithmic Word Search, Mazes, and 3×3 Math Squares + 4-Up Solutions

Puzzle books feel repetitive if every page is Sudoku, so the generator suite covers four complementary puzzle types built on the same vector layout primitives:

  • Word Search: Directional placement across 8 axes with overlap-maximizing retries, uppercase letter fills, and multi-column word banks.
  • Mazes: Clean single-solution path carving on rectangular grids with explicit start/finish openings and crisp outer walls.
  • 3×3 Math Squares: Arithmetic constraint grids (+, -, ×, ÷) with verified unique digit assignments from 1–9.

Every generated puzzle automatically queues its solved state into a paired 4-up solution appendix at the back of the PDF. Four compact vector grids per page reduce page count and print cost while keeping answer keys crisp and legible.

4. Mirrored KDP Inside Gutters & 300 DPI Spine Wrap Calculator

Print geometry breaks when left and right pages share identical horizontal margins. In a bound book, the inside edge sits in the spine glue—and thicker books require progressively deeper inside gutters.

PuzzlePack Studio automatically alternates odd (recto) and even (verso) mirrored page offsets using Amazon KDP's exact page-count tiers:

  • 24–150 pages: 0.375" inside gutter
  • 151–300 pages: 0.500" inside gutter
  • 301–500 pages: 0.625" inside gutter

Alongside the interior PDF engine, the workspace includes a free 300 DPI Cover Spine Wrap Calculator supporting B&W White, Cream, and Color paper stock thicknesses—computing full-wrap width, height, bleed, and spine centerlines before you export your cover artwork.

Try It Free in Your Browser

Everything runs locally in your tab with no account required:

If you are building browser-based document tools or print pipelines in vanilla JS, happy to share more details on the PDF 1.4 object stream serializer in the comments!

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.