EAN-13 barcodes
EAN-13 encodes 13 digits: 12 data digits and a check digit. It is the retail barcode used worldwide and is defined by GS1.
Input rules
- Pass 12 digits to have the check digit calculated, or 13 digits to have it verified.
- Check digit: weight the first 12 digits alternately 1 and 3 from the left, sum them, and take the amount needed to reach the next multiple of 10.
- The first digit is not drawn as bars. It sets the parity pattern of the left-hand digits.
Example
Input 400638133393 encodes as 4006381333931. The barcode is 95 modules wide.
- The example 400638133393 becomes 4006381333931 with check digit 1.
- The output is 95 modules, the same length as UPC-A.
Generate it in JavaScript
The same pattern applies in every language. Here is JavaScript as an example.
import { EAN } from "@openbarcode/javascript/linear";
const modules = new EAN("400638133393").barcode();Draw it as SVG
The library returns the module pattern, so you draw it yourself. This example writes one SVG bar for each module of a EAN-13 barcode.
import { EAN } from "@openbarcode/javascript/linear";
const modules = new EAN("400638133393").barcode();
const quiet = 10;
const width = modules.length + quiet * 2;
const bars = modules
.map((m, i) => (m ? `<rect x="${i + quiet}" width="1" height="100"/>` : ""))
.join("");
const svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${width} 100">${bars}</svg>`;Try it in the live generator, which renders this way in your browser.