Barcode Generator Implementation
Barcodes are needed in inventory systems (label generation), e-commerce (price tag printing), asset tracking systems, and document workflows. The browser generates the barcode client-side without API requests — the user immediately sees the result and can print it.
Barcode Formats
| Format | Application | Data Length |
|---|---|---|
| Code 128 | Warehouse inventory, logistics | Any length |
| EAN-13 | Trade, food products | 13 digits |
| EAN-8 | Small packages | 8 digits |
| Code 39 | Manufacturing, medicine | Letters + digits |
| UPC-A | North America, retail | 12 digits |
| ITF-14 | Box packaging, SSCC | 14 digits |
| QR Code | "Honest Mark" labeling | Any data |
JsBarcode Library
JsBarcode supports all popular formats, works with <canvas>, <svg>, and <img>.
npm install jsbarcode
npm install -D @types/jsbarcode
Component
import JsBarcode from 'jsbarcode'
import { useEffect, useRef } from 'react'
interface BarcodeProps {
value: string
format?: string
width?: number
height?: number
displayValue?: boolean
lineColor?: string
background?: string
fontSize?: number
margin?: number
}
export function Barcode({
value,
format = 'CODE128',
width = 2,
height = 80,
displayValue = true,
lineColor = '#000000',
background = '#ffffff',
fontSize = 14,
margin = 10,
}: BarcodeProps) {
const svgRef = useRef<SVGSVGElement>(null)
useEffect(() => {
if (!svgRef.current || !value) return
try {
JsBarcode(svgRef.current, value, {
format,
width,
height,
displayValue,
lineColor,
background,
fontSize,
margin,
textMargin: 4,
fontOptions: 'bold',
font: 'monospace',
})
} catch (e) {
// Invalid value for format (e.g., letters in EAN-13)
console.warn(`Invalid barcode value "${value}" for format ${format}:`, e)
}
}, [value, format, width, height, displayValue, lineColor, background, fontSize, margin])
return <svg ref={svgRef} />
}
Batch label generation
For printing multiple barcodes on one page:
function LabelSheet({ items }: { items: { sku: string; name: string; price: number }[] }) {
const printRef = useRef<HTMLDivElement>(null)
function handlePrint() {
const content = printRef.current!
const win = window.open('', '_blank')!
win.document.write(`
<html>
<head>
<title>Labels</title>
<style>
body { margin: 0; }
.sheet { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4mm; padding: 10mm; }
.label { border: 0.5pt solid #ccc; padding: 3mm; text-align: center; page-break-inside: avoid; }
.label-name { font-size: 9pt; margin-bottom: 2mm; overflow: hidden; white-space: nowrap; }
.label-price { font-size: 14pt; font-weight: bold; margin-top: 2mm; }
@media print { @page { size: A4; margin: 0; } }
</style>
</head>
<body>
<div class="sheet">${content.innerHTML}</div>
</body>
</html>
`)
win.document.close()
win.focus()
win.print()
win.close()
}
return (
<div>
<button onClick={handlePrint} className="mb-4 px-4 py-2 bg-blue-600 text-white rounded">
Print labels
</button>
<div ref={printRef} className="grid grid-cols-3 gap-1">
{items.map((item) => (
<div key={item.sku} className="border p-2 text-center text-xs">
<div className="truncate text-xs">{item.name}</div>
<Barcode value={item.sku} format="CODE128" height={50} fontSize={10} margin={4} />
<div className="font-bold">{item.price.toLocaleString()} ₽</div>
</div>
))}
</div>
</div>
)
}
Server-side generation (Node.js)
npm install jsbarcode canvas
import { createCanvas } from 'canvas'
import JsBarcode from 'jsbarcode'
export function generateBarcodeBuffer(value: string, format = 'CODE128'): Buffer {
const canvas = createCanvas(400, 150)
JsBarcode(canvas, value, {
format,
width: 2,
height: 80,
displayValue: true,
fontSize: 14,
margin: 10,
})
return canvas.toBuffer('image/png')
}
// In API route:
const buffer = generateBarcodeBuffer(req.query.sku)
res.setHeader('Content-Type', 'image/png')
res.send(buffer)
EAN-13 with check digit
function calculateEAN13Checksum(digits: string): string {
if (digits.length !== 12) throw new Error('EAN-13 requires exactly 12 digits')
let sum = 0
for (let i = 0; i < 12; i++) {
sum += parseInt(digits[i]) * (i % 2 === 0 ? 1 : 3)
}
const checkDigit = (10 - (sum % 10)) % 10
return digits + checkDigit
}
// Generate barcode with check digit
const ean13 = calculateEAN13Checksum('460000000001')
// '4600000000015'
What we do
Connect JsBarcode, implement component with needed format, configure label printing (CSS Print, proper A4 spacing). For server-side generation — API endpoint returning PNG/SVG by SKU.
Timeline: 0.5–1 day.







