Barcode Generation Guide
Barcode Generation Guide
Generate various types of barcodes in your document templates for tracking, identification, and automation.
Table of Contents
- Supported Barcode Types
- Code 128
- Code 39
- QR Codes
- Data Matrix
- EAN-13
- PDF417
- Generic Barcode Function and Options
- Real-World Examples
- Best Practices
1. Supported Barcode Types
| Type | Function | Use Case |
|---|---|---|
| Code 128 | code128 | General purpose, alphanumeric |
| Code 39 | code39 | Industrial, alphanumeric |
| Code 93 | code93 | Denser alternative to Code 39 |
| Codabar | codabar | Blood banks, libraries, logistics |
| QR Code | qrcode | URLs, large data, mobile scanning |
| Data Matrix | datamatrix | Small items, compact encoding |
| Aztec | aztec | Transport and ticketing; needs no quiet zone |
| EAN-13 | ean13 | Retail products (13 digits) |
| EAN-8 | ean8 | Small retail (7–8 digits) |
| UPC-A | upcA | North American retail (11–12 digits) |
| UPC-E | upcE | Short UPC for small packages |
| PDF417 | pdf417 | Large data capacity, 2D |
| Interleaved 2 of 5 | itf | Numeric, shipping cartons |
| Industrial 2 of 5 | code25 | Numeric, older equipment |
These types are available only through the generic barcode function (Section 8):
| Type | Name | Use Case |
|---|---|---|
| GS1-128 | "gs1-128" | Shipping and supply-chain labels with GS1 application identifiers |
| GS1 DataMatrix | "gs1-datamatrix" | GS1 data in a Data Matrix, such as healthcare and parts |
| GS1 QR Code | "gs1-qr" | GS1 data in a QR code |
| GS1 DataBar | "databar", "databar-expanded" | Retail and fresh food |
| MicroPDF417 | "micropdf417" | Small amounts of data in little space |
| MaxiCode | "maxicode" | Parcel sorting (UPS) |
| CODABLOCK-F | "codablock-f" | Stacked Code 128 for longer text |
| Code 11 | "code11" | Telecommunications equipment |
| MSI | "msi" | Retail shelves and inventory |
| Plessey | "plessey" | Older retail and library systems |
| POSTNET | "postnet" | Older US Postal Service ZIP codes |
| Intelligent Mail barcode | "imb" | US Postal Service mail |
GS1 data is written with application identifiers in parentheses, for example (01)09501101020917(10)LOT1.
All barcodes are rendered as images embedded directly in your HTML/PDF. They are drawn by the same engine that DataMagik uses for Zebra labels, so a barcode on a document scans the same as the same barcode on a label.
2. Code 128
High-density linear barcode supporting all ASCII characters.
Syntax
{{code128 .BarcodeData width height}}Examples
<div class="barcode-container">
<p>Order Number:</p>
{{code128 .OrderNumber 300 100}}
</div>Size Recommendations
- Small labels: 200x60 pixels
- Standard labels: 300x100 pixels
- Large labels: 400x150 pixels
3. Code 39
Widely used in automotive and defense.
Syntax
{{code39 .BarcodeData width height}}
{{code39 .BarcodeData width height "checksum=true ratio=3"}}Supported Characters
By default Code 39 is encoded in full-ASCII mode, so lowercase letters and other ASCII characters are kept as they are. Pass fullascii=false to use standard Code 39 only; the data is then converted to uppercase, and the characters allowed are A-Z, 0-9, - . $ / + % and space. Some older scanners read only standard Code 39.
Options
checksum=trueadds a check character.ratio=sets how much wider the wide bars are than the narrow ones, from 2 to 3. The default is 2.fullascii=false, as above.
4. QR Codes
2D barcodes ideal for URLs, contact information, and mobile scanning.
Syntax
{{qrcode .Data size}}Examples
<!-- Website URL -->
{{qrcode .WebsiteURL 200}}
<!-- Event ticket -->
<div class="ticket">
<h1>{{.EventName}}</h1>
{{qrcode .TicketCode 250}}
</div>
<!-- vCard contact -->
{{qrcode .VCard 200}}Size Recommendations
- Small: 150px (simple URLs)
- Medium: 200px (standard)
- Large: 300px (complex data)
5. Data Matrix
Compact 2D barcodes for small items and high-density data.
Syntax
{{datamatrix .Data size}}Use Cases
Electronic components, medical devices, pharmaceutical packaging, manufacturing parts.
6. EAN-13
Standard retail barcode. Requires exactly 12 or 13 digits (numeric only).
Syntax
{{ean13 .ProductCode width height}}Format
- 12 digits: the check digit is calculated automatically.
- 13 digits: the last digit is the check digit, and it is checked. If it is wrong, an error is shown in place of the barcode, so a barcode never scans as a different number than your data.
EAN-8 and UPC-A work the same way: 7 or 8 digits for EAN-8, and 11 or 12 digits for UPC-A.
7. PDF417
Stacked linear barcode with high data capacity.
Syntax
{{pdf417 .Data width height}}
{{pdf417 .Data width height "security=5"}}Capacity
- About 2,500 digits, about 1,700 uppercase letters, or about 1,200 characters of mixed text.
- Data that does not fit shows an error in place of the barcode.
security=sets the error correction level from 0 to 8. Without it, the recommended level for the amount of data is used.
8. Generic Barcode Function and Options
{{barcode "type" .Data width height}}
{{barcode "gs1-128" .ShipmentData 400 100}}
<!-- Conditional barcode type -->
{{if eq .Region "US"}}
{{code128 .ProductCode 300 100}}
{{else if eq .Region "EU"}}
{{ean13 .EAN 300 100}}
{{else}}
{{qrcode .ProductURL 200}}
{{end}}Options
Every barcode function takes an optional last argument of options, written as key=value pairs separated by spaces, for example {{code128 .Part 300 80 "fg=#1f2937"}}.
| Option | Applies to | Meaning |
|---|---|---|
fg, bg | All | Bar and background colors, such as #000000 or white. |
ratio | Code 39, Codabar, Interleaved 2 of 5 | Wide-to-narrow bar ratio, 2 to 3. Defaults: 2 for Code 39 and Codabar, 3 for Interleaved 2 of 5. |
checksum | Code 39, Interleaved 2 of 5, Industrial 2 of 5 | Adds a check character. |
fullascii | Code 39, Code 93 | false limits the data to standard characters and uppercases it. |
gs1 | Code 128, Data Matrix | Encodes GS1 application identifiers. |
ecc | QR Code, Aztec | Error correction: L, M, Q or H for QR; a percentage for Aztec. |
security | PDF417 | Error correction level, 0 to 8. |
mode | MaxiCode | MaxiCode mode. |
9. Real-World Examples
Shipping Label
<div class="label">
<h1>{{.Carrier}} - {{.ServiceType}}</h1>
<div class="addresses">
<div><h3>FROM:</h3><p>{{.Sender.Name}}</p></div>
<div><h3>TO:</h3><p>{{.Recipient.Name}}</p></div>
</div>
{{code128 .TrackingNumber 450 120}}
<p>{{.TrackingNumber}}</p>
{{qrcode .TrackingURL 150}}
</div>Product Label with Multiple Barcodes
<div class="product-label">
<h1>{{.ProductName}}</h1>
<p>${{.Price | printf "%.2f"}}</p>
<div><p>Retail:</p> {{ean13 .EAN 300 90}}</div>
<div><p>SKU:</p> {{code128 .SKU 250 70}}</div>
<div><p>Info:</p> {{qrcode .ProductInfoURL 120}}</div>
</div>10. Best Practices
Sizing Guidelines
| Barcode Type | Minimum Size |
|---|---|
| Code 128/39 | 200x60 pixels |
| EAN-13 | 250x80 pixels |
| QR Codes | 150x150 pixels |
| Data Matrix | 80x80 pixels |
| PDF417 | 300x100 pixels |
Guidelines
- Validate Input — Ensure data matches barcode type requirements
- Test Scanning — Always test with actual scanners
- Include Human-Readable — Display text below barcodes
- White Space — Leave quiet zones around barcodes
- High Contrast — Black on white is ideal
Troubleshooting
- Not scanning? — Increase size, verify data, check contrast
- Not displaying? — Check syntax, validate data exists in sample JSON
- Invalid characters? — Match data to barcode type (e.g., EAN-13 = digits only)
- Error in place of an EAN or UPC barcode? — Check the digit count and the check digit
For Zebra labels, see the Visual ZPL label designer.