Pure JavaScript Code 128B/C, EAN-13, and ITF-14 Vector SVG Barcode Encoding
Raster barcodes rendered onto an HTML5 Canvas blur when scaled inside PDF shipping labels or thermal printers. Generating standalone SVG vector rectangles directly from raw binary module bitstreams ensures sharp edge alignment at any DPI without requiring Canvas, DOM APIs, or external font files.
We packaged the encoder behind the Sunasty Barcode Generator and CSV Label Sheet Builder as the zero-dependency MIT library barcode128-svg (JSR).
1. Selecting Subset B vs. Dense Numeric Subset C
Code 128 supports printable ASCII characters via Start Code B (104), and double-density two-digit encoding via Start Code C (105). When an input string contains four or more even-count digits (isCode128CText), packing digit pairs 00..99 halves bar width while computing the weighted Mod-103 checksum across symbol positions:
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;
}
2. Merging Adjacent Bar Modules into Vector Segments
Instead of emitting one narrow SVG <rect> per 1-module bit, modulesToBarSegments run-length merges consecutive 1 modules into single crisp rectangles with exact integer X offsets, flanked by configurable quietZone padding and optional ITF-14 GS1 bearer frames:
import { renderBarcodeSvg, parseBulkBarcodeText, buildBulkBarcodeZipEntries } from 'barcode128-svg';
const svg = renderBarcodeSvg('code128', 'ORD-2026-0042', {
moduleWidth: 2,
barHeight: 90,
quietZone: 11,
showText: true,
});
console.log(svg.svgWidth, svg.svgHeight);
// Batch parse mixed symbologies from CSV or newline lists
const batch = parseBulkBarcodeText('ean13,400638133393\nSKU-9981-A');
const zipFiles = buildBulkBarcodeZipEntries(batch);
3. Related Label, QR & Document Tools
Generate individual print-ready barcodes in the Barcode Generator, lay out multi-column shipping sheets from spreadsheets with the CSV Label Sheet Tool, or encode and decode 2D payloads with the QR Generator and QR Scanner.
4. Open-Source Package Links
Available under the MIT license on npm (barcode128-svg), JSR (@sunasty/code128-svg), and GitHub.