Drawing a barcode in the browser is easy. JsBarcode does it in one line.
Getting that barcode to print at the right size, on a cheap thermal label printer, and still scan took far longer than the drawing part. This post covers the problems I ran into while building ownbarcode, a free barcode and label generator for small sellers, and how I fixed them in plain JavaScript.
All of it runs in the browser. No sign-up, no upload, no analytics script. Product names, prices and SKUs never leave the page.
Why client-side only
Most of my users are small shops and online sellers in India. They print MRP stickers, Amazon FNSKU labels and carton labels on 50 × 25 mm thermal rolls.
Many free generators I tried had at least one of these problems:
- A watermark, or "free for non-commercial use only"
- A sign-up wall before you can download
- Your whole product list goes to their server for a bulk job
- A PNG at screen resolution, which prints blurry
Rendering in the browser fixes the privacy problem and the cost problem in one move. A static site can't leak data it never receives. It also costs almost nothing to host, so there's no reason to add a paywall.
The stack is deliberately boring:
- Static HTML pages, built with a few Python scripts
- JsBarcode for 1D codes, bwip-js for PDF417, Data Matrix and GS1, a small QR library
- SheetJS (vendored, not from a CDN) for Excel import
- Everything else is hand-written
Problem 1: "Print" in the browser ruins the scale
The first version used window.print(). On paper the barcode came out 3–8% too small or too large. That depended on the browser, the printer driver and whether "Fit to page" was on (it usually is).
That's a real problem. An EAN-13 barcode has a minimum size: bars too thin, and cheap scanners fail.
The fix was to stop letting the browser decide the size. I wrote a small PDF writer that reads the barcode SVG and outputs a PDF whose page is exactly the label size in millimetres.
PDF units are points (1/72 inch), so the core of it is one constant:
var MM = 72 / 25.4; // points per millimetre // A 50 × 25 mm label becomes a 141.73 × 70.87 pt page var mediaBox = [0, 0, 50 * MM, 25 * MM]; Then each SVG <rect> and <path> becomes a PDF drawing command. Bars stay as vectors, so they're sharp at any printer resolution.
Two details made a big difference:
1. Force colours to pure black or white. Thermal printers have no grey. A #222 bar can come out dithered and fuzzy, so every colour is pushed to one of two values:
// returns null for no paint, 0 for black, 1 for white function tone(c) { if (c == null) return undefined; c = String(c).trim().toLowerCase(); if (!c || c === 'none' || c === 'transparent') return null; if (c === 'white') return 1; if (c === 'black' || c === 'currentcolor') return 0; // ...parse hex / rgb(), then threshold on brightness } 2. Tell the PDF viewer not to scale it. The PDF catalog gets /ViewerPreferences << /PrintScaling /None >>. Not every viewer respects it, so the print dialog still says to choose 100% / Actual size. Together, these remove the "Fit to page" default that was causing most of the size errors.
The whole writer is about 12 KB unminified, with no dependencies. It only supports what my own SVGs use: <rect>, <path> (M, L, H, V, C, Z), <g> transforms and <text>. Writing a full SVG-to-PDF converter would have been a much bigger project.
Problem 2: Excel eats barcodes
The bulk generator lets you upload a spreadsheet of products and get a sheet of labels. A typical spreadsheet of product codes, saved as CSV, looks like this:
8.90123E+12,Ajwain 50 g,45 That was meant to be 8901234567890. Excel had stored it as a number, displayed it in scientific notation, and the CSV export kept the display version. Leading zeros on UPC codes vanish the same way: 012345678905 becomes 12345678905.
Silently turning these into barcodes would be the worst possible outcome. The seller would print 500 labels with the wrong code.
So the importer does three things:
- Reads
.xlsxdirectly, not just CSV, so it can see the stored cell text and number format instead of the rounded value. - Flags anything that looks damaged. It checks for
E+notation, codes that are one digit short of a valid length, and failed check digits. It shows a warning instead of guessing. - Parses in a Web Worker, so a large sheet doesn't freeze the page.
The worker also checks magic bytes before handing the file to SheetJS. That way a renamed .csv or a random file gets a clear error:
const bytes = new Uint8Array(data.buffer); const zipped = bytes[0] === 0x50 && bytes[1] === 0x4b && bytes[2] === 3 && bytes[3] === 4; // .xlsx const compound = [0xd0,0xcf,0x11,0xe0,0xa1,0xb1,0x1a,0xe1].every((b, i) => bytes[i] === b); // old .xls if (!zipped && !compound) throw Error('This is not a valid .xlsx or .xls workbook.'); The file is read with file.arrayBuffer() and passed to the worker. No fetch, no upload.
Problem 3: printer dots are not millimetres
A 203 dpi thermal printer, the most common kind in small shops, prints dots that are 25.4 / 203 ≈ 0.125 mm wide. It cannot print a bar 0.33 mm wide, which is the nominal EAN-13 module width. It prints either 2 dots (0.25 mm) or 3 dots (0.375 mm).
If you send it 0.33 mm anyway, the driver rounds each bar on its own. Some bars become 2 dots, some 3. The bar-to-space ratios that the scanner depends on drift, and you get a barcode that looks fine but scans badly.
The fix is to choose the module width in whole printer dots first, then build everything else from that:
// simplified function snapModule(targetMm, dpi) { const dotMm = 25.4 / dpi; const dots = Math.max(1, Math.round(targetMm / dotMm)); return { dots, mm: dots * dotMm }; } snapModule(0.33, 203); // { dots: 3, mm: 0.375 } -> about 114% of nominal size, inside EAN's 80–200% range snapModule(0.33, 300); // { dots: 4, mm: 0.339 } The site also has a scan check page built on the same idea. It renders the barcode at the printer's real resolution and decodes the bars back from the bitmap. Then it repeats that test with half a dot of "ink spread" added to every bar, to see if it would still read on a slightly worn print head. It's not a replacement for a real scanner, but it catches the obvious mistakes before you waste a roll of labels.
Problem 4: Hindi on a printer with no Hindi font
Indian MRP labels often need Hindi text and the ₹ sign. Most thermal printers have built-in fonts for ASCII only. Send them Devanagari through TSPL or ZPL text commands and you get boxes or nothing.
The solution is to stop sending text at all. The browser already renders Hindi perfectly, so:
- Draw the whole label (text, ₹ price and barcode) on a canvas at the printer's exact dot size, for example about 400 × 200 dots for 50 × 25 mm at 203 dpi.
- Threshold it to a 1-bit image. No greys, just like the PDF writer.
- Send it as a bitmap:
BITMAPfor TSPL printers (TSC, TVS),^GFAfor ZPL (Zebra), with compression on the ZPL side to keep files small.
The printer doesn't need to know any Hindi. It just prints dots. The barcode data itself always stays ASCII digits, because that's what scanners and retail systems expect.
The PDF writer does the same thing on a smaller scale. Plain ASCII text is written as real PDF text in Helvetica or Courier. Any string with Hindi or ₹ is drawn as a sharp 1-bit image at 600 dpi instead, because embedding a Devanagari font would add hundreds of KB to every PDF.
What I'd do differently
- Test on a real printer much earlier. My own tests parse the generated PDF, rasterise it and decode the barcodes back, and they pass. But a real printer and a roll of labels will show problems that no unit test finds.
- Ship fewer formats at first. EAN-13, Code 128 and QR cover most of what small sellers need. ITF-14, GS1-128 and PDF417 came later, and they could have waited.
- Make the Hindi version from day one, not as a translation. Hindi-speaking sellers search differently and ask different questions. The Hindi pages are their own pages, not a copy of the English ones.
Try it, break it
The site is at ownbarcode.com (Hindi: ownbarcode.com/hi/). It covers EAN-13, UPC-A, Code 128, Code 39, ITF-14, QR, Data Matrix, PDF417 and GS1-128. It also has MRP labels, bulk labels from Excel, and TSPL/ZPL code for thermal printers.
Some honest gaps: the TSPL/ZPL output and the Hindi bitmap labels haven't been tested on every printer model yet. If you own a TSC, TVS, Zebra or any other label printer and something prints wrong, I'd really like to hear about it in the comments.
And if you've solved the "browser print scaling" problem in a better way than generating a PDF, please tell me how.
