DEV Community

Bastien Sulyan
Bastien Sulyan

Posted on Edited on Originally published at bsulyan.github.io

Pure JavaScript Code 128B/C, EAN-13 & ITF-14 Vector SVG Barcode Encoding

Canvas-rendered barcodes blur at odd scaling factors inside thermal printers or PDF shipping labels. Emitting standalone vector <svg> rectangles directly from binary module bitstreams avoids Canvas, DOM APIs, and image artifacts while remaining lightweight for browser workers and Node CLIs.

We open-sourced the encoder powering the Sunasty Barcode Generator and CSV Label Sheet Builder as barcode128-svg on npm and JSR (@sunasty/code128-svg).

1. Automatic Subset B vs. Dense Numeric Subset C + Mod-103 Checksum

When an input payload contains four or more even-count digits, switching to Start Code C (105) packs two digits (00..99) per symbol instead of one character in Start Code B (104), halving barcode width while computing the weighted Mod-103 checksum:

export function computeCode128Checksum(text) {
  const { startSymbol, dataValues } = resolveCode128Payload(text);
  let sum = startSymbol;
  for (let i = 0; i < dataValues.length; i += 1) {
    sum += dataValues[i] * (i + 1);
  }
  return sum % 103;
}
Enter fullscreen mode Exit fullscreen mode

2. Run-Length Merged Vector Bars & Bulk Label Batching

Instead of outputting separate 1-module <rect> nodes, modulesToBarSegments merges adjacent black bars before rendering crisp SVG rectangles (including optional ITF-14 GS1 bearer frames and quiet zones):

import { renderBarcodeSvg, parseBulkBarcodeText, buildBulkBarcodeZipEntries } from 'barcode128-svg';

const svg = renderBarcodeSvg('code128', 'ORD-2026-0042', {
  moduleWidth: 2,
  barHeight: 90,
  quietZone: 11,
  showText: true,
});

const items = parseBulkBarcodeText('ean13,400638133393\nSKU-9981-A');
const entries = buildBulkBarcodeZipEntries(items);
Enter fullscreen mode Exit fullscreen mode

Web Tools & Registry Links

Top comments (0)