To generate a barcode in code you pick a symbology, encode your data, and render it at a size that keeps the narrow bars wide enough to scan. The three decisions that determine whether it works are the symbology, the module width, and the quiet zone — and the last two are what most implementations get wrong.
A barcode that looks perfect on screen can be completely unscannable when printed. This post covers why, and how to avoid it.
Which barcode format should I use?
| Symbology | Encodes | Use it for | Notes |
|---|---|---|---|
| Code 128 | Full ASCII, variable length | Shipping labels, internal tracking, asset tags | The default choice for most business use |
| Code 39 | Alphanumeric, uppercase | Legacy systems, some automotive and defence | Lower density than Code 128; use only if required |
| EAN-13 | 13 digits | Retail products outside North America | Requires a registered GS1 prefix |
| UPC-A | 12 digits | Retail products in North America | Also requires GS1 registration |
| ITF-14 | 14 digits | Shipping cartons and cases | Tolerates poor print quality well |
| QR Code | Up to ~4,296 characters | URLs, payment links, product passports | Two-dimensional; scannable from a phone |
| GS1-128 | Structured GS1 data | Supply chain with application identifiers | Code 128 plus a defined data structure |
The short version: internal use gets Code 128. Retail products need a GS1-registered EAN or UPC. Anything scanned by a phone gets a QR code.
Do I need to register for a barcode number?
Only for retail.
If a product will be scanned at a shop checkout, the number must be a real GTIN issued through GS1. You cannot invent one — it may already belong to someone else, and retailers verify.
For internal use — warehouse locations, asset tags, work orders, event tickets — you can encode whatever you like. No registration, no fee.
This distinction saves a lot of confusion. Most barcode generation is internal, and most internal barcode generation needs no registration at all.
What size does a barcode need to be?
This is the section worth reading twice, because it is where scannable and unscannable are decided.
Module width is the width of the narrowest bar. For Code 128 printed on a standard laser printer, keep it at or above 0.25 mm (about 10 mil). Below that, print bleed merges adjacent bars and scanners fail.
Height should be at least 15% of the barcode's length, with a practical minimum of around 12 mm. Short barcodes are hard to scan at an angle.
Quiet zone — the blank margin either side — must be at least 10× the module width, and never less than 2.5 mm. This is the single most commonly omitted requirement. A barcode butted against a table border or a page edge will not scan, no matter how well the bars themselves are printed.
Resolution for print: render at 300 DPI minimum. A 96 DPI screen image scaled up for print produces soft edges that scanners reject.
How do I put a barcode in a PDF?
Generate the barcode as an image and embed it. In an HTML template:
<div class="label">
<h2>{{product_name}}</h2>
<img src="{{barcode_image}}" alt="Barcode {{sku}}" width="180">
<p class="sku">{{sku}}</p>
</div>
@page { size: 100mm 50mm; margin: 4mm; }
.label { text-align: center; }
img { image-rendering: crisp-edges; }
image-rendering: crisp-edges matters — without it some renderers apply smoothing that blurs bar boundaries.
How do I generate barcodes in bulk?
For a batch — a thousand asset tags, a product catalogue, an event's tickets — three things matter:
Batch the requests. One call per barcode with no concurrency control will hit rate limits. Process in chunks with a short delay between them.
Lay out multiple labels per page. A CSS grid on an A4 sheet with the correct label dimensions, rather than one PDF per label. Fewer files, less handling, and it matches the label stock people actually buy.
Validate before printing. Generate the batch, scan a random sample with a phone, and only then commit to a print run. Discovering a sizing problem after printing two thousand labels is expensive.
What usually goes wrong
The barcode scans on screen but not on paper. Almost always module width or resolution. Phone cameras are forgiving; laser scanners are not. Render at 300 DPI and keep narrow bars at 0.25 mm or wider.
Scanners read the wrong value. Check for whitespace. A trailing space or newline in your data gets encoded, and the scanner returns it. Trim your input.
Special characters fail. Code 39 handles only uppercase alphanumerics and a few symbols. If your data has lowercase letters or punctuation, use Code 128.
The quiet zone is missing. A barcode placed flush against a table cell border will not scan. Add explicit padding — at least 2.5 mm of white space on both sides.
EAN-13 is rejected at checkout. The number is not a registered GTIN, or the check digit is wrong. The final digit is calculated, not chosen; most libraries compute it for you if you supply twelve digits.
Barcodes look blurry in the PDF. The image is being scaled up. Generate at the final print size or larger, never smaller.
Frequently asked questions
Can I generate a barcode for free?
Yes, for any internal use. Generating the image costs nothing and requires no registration. You only pay GS1 if the barcode will be scanned at retail, and that fee is for the number, not the image.
What is the difference between a barcode and a QR code?
A traditional barcode is one-dimensional and encodes a short value, typically read by a dedicated laser scanner. A QR code is two-dimensional, holds far more data including URLs, and is readable by any phone camera.
Which barcode format is most widely supported?
Code 128 for general business use — it encodes full ASCII, is compact, and every modern scanner reads it. For retail products, EAN-13 outside North America and UPC-A within it.
How small can a barcode be?
The limit is the narrowest bar, not the overall size. Keep it at or above 0.25 mm for standard laser printing. Thermal label printers can go smaller; verify with a test scan on your actual hardware.
Can I add a barcode to an existing PDF?
Yes. Generate the barcode image, then composite it onto the page at chosen coordinates. This is how asset tags are added to pre-printed forms and how tracking codes are stamped onto shipping documents.
In short
Pick Code 128 unless retail requires otherwise. Keep the narrow bar at 0.25 mm or wider, render at 300 DPI, and leave a quiet zone of at least 2.5 mm. Test-scan a sample before any large print run.