Barcode Generator: how it works and how to build one
Code 128, EAN-13 and UPC-A barcodes as PNG or SVG. This guide explains what happens behind the button and shows the code, so you can build your own.
What it does
Make barcodes for products, inventory and shipping labels. Supports Code 128 (any text), EAN-13 and UPC-A with check digits added for you. Set the size and colors and download PNG or SVG.
When to use it
- Inventory labels
- Shipping labels
- Product packaging
How to use it
- Pick the barcode type.
- Type the value.
- Download PNG or SVG.
Try it free in your browser, nothing is uploaded: https://eskillsfy.com/tools/barcode-generator
How it works
A 1D barcode is a sequence of bar and space widths. Each symbology has a table: Code 128 encodes each character as 3 bars and 3 spaces of widths 1–4, plus a start code, a checksum and a stop code. EAN-13 uses three digit tables (L, G, R) and the first digit picks the L/G pattern for the left half.
Drawing is simple: walk the widths and draw a rectangle for every bar. SVG keeps it sharp at any size.
Build it yourself
Here is the core of the tool. It has no dependencies unless noted, so you can paste it into any TypeScript or JavaScript project.
ean13.ts (ts)
const L = ["3211", "2221", "2122", "1411", "1132", "1231", "1114", "1312", "1213", "3112"];
const G = L.map((p) => [...p].reverse().join(""));
const PARITY = ["LLLLLL", "LLGLGG", "LLGGLG", "LLGGGL", "LGLLGG", "LGGLLG", "LGGGLL", "LGLGLG", "LGLGGL", "LGGLGL"];
export const checkDigit = (d: string) => (10 - ([...d].reduce((s, c, i) => s + +c * (i % 2 ? 3 : 1), 0) % 10)) % 10;
export function ean13Widths(d12: string) {
const d = d12 + checkDigit(d12);
const left = [...d.slice(1, 7)].map((c, i) => (PARITY[+d[0]][i] === "L" ? L : G)[+c]).join("");
const right = [...d.slice(7)].map((c) => L[+c]).join("");
return "111" + left + "11111" + right + "111"; // guards + digits; even positions are bars
}
Make it production-ready
- Render widths to SVG rects, then to PNG with a canvas.
- Add Code 128 with automatic switching to set C for digits.
Questions
Can I sell products with these?
Retail EAN and UPC numbers must be bought from GS1. The barcode image itself is standard.
Free, runs in your browser, nothing is uploaded.