Dev.to WebDev πŸ›  Dev πŸ‘ 0 πŸ“– 5 min read

Generating 1:1 Vector PDF and CAD DXF Packaging Dielines in Pure Browser JS (Zero Dependencies)

When structural packaging engineers, prepress operators, or indie hardware/CPG makers need a quick 1:1 flat dieline net to test product fit or calculate sheet imposition, most web tools either rasterize a preview or lock

When structural packaging engineers, prepress operators, or indie hardware/CPG makers need a quick 1:1 flat dieline net to test product fit or calculate sheet imposition, most web tools either rasterize a preview or lock vector CAD exports behind subscriptions and server queues.

Because structural box netsβ€”such as RSC shipping cartons, mailer boxes, tuck-end folding cartons, collapsible magnetic rigid boxes, and stand-up pouchesβ€”are purely parametric 2D geometry, we built a 100% client-side, zero-dependency JavaScript geometry engine that emits live interactive SVG alongside 1:1 scale PDF 1.4 and AutoCAD DXF files directly in the browser.

Here is how the unified segment architecture, PDF coordinate math, and millimeter DXF layer serialization work under the hood.

1. One Intermediate Geometry Model: { x1, y1, x2, y2, kind }

Rather than writing separate template logic for SVG paths, PDF streams, and CAD entities, every parametric box generator outputs a normalized flat array of millimeter line segments (and discretized arc polylines for thumb notches, dust-flap radii, or hang holes):

/**
 * @typedef {'CUT' | 'FOLD' | 'ALLOWANCE'} LineKind
 * @typedef {{ x1: number, y1: number, x2: number, y2: number, kind: LineKind }} Segment
 */

export function generateTuckEndSegments({ L, W, H, caliper = 0.45, glueFlap = 15, tuckFlap = 18 }) {
  /** @type {Segment[]} */
  const segs = [];
  const add = (x1, y1, x2, y2, kind) => segs.push({ x1, y1, x2, y2, kind });

  // Panel X stations (left-to-right: Glue Flap | Front L | Side W | Back L | Side W)
  const x0 = glueFlap;
  const x1 = x0 + L;
  const x2 = x1 + W;
  const x3 = x2 + L;
  const x4 = x3 + W;

  // Vertical score lines (FOLD) with board-caliper allowance offsets where needed
  add(x0, 0, x0, H, 'FOLD');
  add(x1, 0, x1, H, 'FOLD');
  add(x2, 0, x2, H, 'FOLD');
  add(x3, 0, x3, H, 'FOLD');

  // Outer perimeter & tapered glue flap (CUT)
  const taper = Math.min(8, H * 0.12);
  add(x0, 0, 0, taper, 'CUT');
  add(0, taper, 0, H - taper, 'CUT');
  add(0, H - taper, x0, H, 'CUT');
  add(x4, 0, x4, H, 'CUT');

  return segs;
}

Using three semantic layer kinds maps directly onto real die-cutting and prepress workflows:

  • CUT: Through-knife steel rule (0.71mm / 2pt rule) β€” rendered solid magenta/red in prepress PDFs and assigned its own CAD layer.
  • FOLD: Creasing rule / score matrix β€” rendered dashed cyan/green so press operators and CAD/CAM plotters never confuse scores with knife cuts.
  • ALLOWANCE: Bleed (3mm outer offset), varnish/glue-free knock-out zones, or caliper bend compensation callouts.

Once a generator returns segments, we compute a tight bounding box { minX, minY, maxX, maxY } plus a uniform artboard margin (e.g., 15mm) so all three exporters share identical physical coordinates.

2. Raw PDF 1.4 Vector Serialization (72 / 25.4 Points per Millimeter)

Bringing in a 400 KB PDF library just to stroke 2D vector segments is unnecessary. PDF 1.4 content streams use PostScript-style path operators in user space points (1 pt = 1/72 inch). Since 1 inch = 25.4 mm, the exact millimeter-to-point scale factor is:

const PT_PER_MM = 72 / 25.4; // 2.834645669291339

Two subtle details matter when printing 1:1 dielines on desktop printers or opening them in Adobe Illustrator:

  1. Y-Axis Inversion: Browser/SVG coordinates grow downward from top-left, whereas PDF user space grows upward from bottom-left: pdfY = pageHeightPt - (mmY - minY + marginMm) * PT_PER_MM.
  2. Stroke Styling by Layer: Grouping segments by kind avoids redundant graphics-state switches inside the stream:
function buildPdfContentStream(segments, bounds, marginMm = 15) {
  const widthMm = (bounds.maxX - bounds.minX) + marginMm * 2;
  const heightMm = (bounds.maxY - bounds.minY) + marginMm * 2;
  const pageH = heightMm * PT_PER_MM;

  const tx = (x) => ((x - bounds.minX + marginMm) * PT_PER_MM).toFixed(3);
  const ty = (y) => (pageH - (y - bounds.minY + marginMm) * PT_PER_MM).toFixed(3);

  const lines = ['0.75 w', '1 J', '1 j']; // 0.75pt stroke, round cap/join

  const styles = {
    CUT:       '0.88 0.08 0.24 RG [] 0 d',    // Solid crimson cut rule
    FOLD:      '0.00 0.55 0.35 RG [5 3] 0 d', // Dashed crease rule
    ALLOWANCE: '0.15 0.45 0.85 RG [2 2] 0 d', // Dotted bleed/glue zone
  };

  for (const kind of ['ALLOWANCE', 'FOLD', 'CUT']) {
    const group = segments.filter((s) => s.kind === kind);
    if (!group.length) continue;
    lines.push(styles[kind]);
    for (const s of group) {
      lines.push(tx(s.x1) + ' ' + ty(s.y1) + ' m ' + tx(s.x2) + ' ' + ty(s.y2) + ' l S');
    }
  }
  return { stream: lines.join('\n'), widthPt: widthMm * PT_PER_MM, heightPt: pageH };
}

To assemble a valid standalone PDF file in memory, we concatenate the five minimal PDF objects (Catalog, Pages, Page with /MediaBox [0 0 widthPt heightPt], and the uncompressed /Length content stream), record each object's exact UTF-8 byte offset via TextEncoder, append the xref table with 10-digit zero-padded offsets, and hand the Blob to URL.createObjectURL(). Illustrator, Acrobat, and ArtiosCAD read the MediaBox at exact 1:1 millimeter dimensions.

3. Millimeter-Accurate AutoCAD DXF Export ($INSUNITS = 4)

When sending a dieline to a Kongsberg/Zund sample cutting table or AutoCAD, the #1 pitfall is unitless DXF files importing as inches (25.4x too large). Declaring $INSUNITS with group code 70 set to 4 (Millimeters) in the HEADER section, alongside named LAYER entries in TABLES, guarantees plug-and-play CAD import:

export function serializeDxf(segments, bounds) {
  const out = [
    '0', 'SECTION',
    '2', 'HEADER',
    '9', '$ACADVER', '1', 'AC1009',
    '9', '$INSUNITS', '70', '4', // 4 = Millimeters
    '0', 'ENDSEC',
    '0', 'SECTION',
    '2', 'TABLES',
    '0', 'TABLE', '2', 'LAYER', '70', '3',
    '0', 'LAYER', '2', 'CUT',       '70', '0', '62', '1', '6', 'CONTINUOUS', // ACI 1 = Red
    '0', 'LAYER', '2', 'FOLD',      '70', '0', '62', '3', '6', 'DASHED',     // ACI 3 = Green
    '0', 'LAYER', '2', 'ALLOWANCE', '70', '0', '62', '5', '6', 'CONTINUOUS', // ACI 5 = Blue
    '0', 'ENDTAB',
    '0', 'ENDSEC',
    '0', 'SECTION',
    '2', 'ENTITIES',
  ];

  for (const s of segments) {
    // Flip Y so CAD geometry upright-matches the 2D screen net
    const y1 = bounds.maxY - s.y1;
    const y2 = bounds.maxY - s.y2;
    out.push(
      '0', 'LINE',
      '8', s.kind,
      '10', s.x1.toFixed(4), '20', y1.toFixed(4), '30', '0.0',
      '11', s.x2.toFixed(4), '21', y2.toFixed(4), '31', '0.0'
    );
  }

  out.push('0', 'ENDSEC', '0', 'EOF');
  return out.join('\r\n');
}

Because every entity carries group code 8 (CUT, FOLD, or ALLOWANCE), sample-maker CAM software automatically maps CUT to the oscillating blade and FOLD to the creasing wheel without manual cleanup.

Try It Live, Grab the Specs, or Explore the Benchmark Data

We released both the interactive browser tool and the underlying reference algorithms openly for developers, designers, and packaging engineers:

If you are building custom WebGL 3D folding previews from 2D FOLD hinge graphs or adding ECMA/FEFCO parametric styles, feel free to open an issue or PR!

πŸ“° 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.