Barcode Generation Guide

Updated Nov 1, 2026
DataMagik Documents

Barcode Generation Guide

Generate various types of barcodes in your document templates for tracking, identification, and automation.

Table of Contents

  1. Supported Barcode Types
  2. Code 128
  3. Code 39
  4. QR Codes
  5. Data Matrix
  6. EAN-13
  7. PDF417
  8. Generic Barcode Function and Options
  9. Real-World Examples
  10. Best Practices

1. Supported Barcode Types

TypeFunctionUse Case
Code 128code128General purpose, alphanumeric
Code 39code39Industrial, alphanumeric
Code 93code93Denser alternative to Code 39
CodabarcodabarBlood banks, libraries, logistics
QR CodeqrcodeURLs, large data, mobile scanning
Data MatrixdatamatrixSmall items, compact encoding
AztecaztecTransport and ticketing; needs no quiet zone
EAN-13ean13Retail products (13 digits)
EAN-8ean8Small retail (7–8 digits)
UPC-AupcANorth American retail (11–12 digits)
UPC-EupcEShort UPC for small packages
PDF417pdf417Large data capacity, 2D
Interleaved 2 of 5itfNumeric, shipping cartons
Industrial 2 of 5code25Numeric, older equipment

These types are available only through the generic barcode function (Section 8):

TypeNameUse 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=true adds 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"}}.

OptionApplies toMeaning
fg, bgAllBar and background colors, such as #000000 or white.
ratioCode 39, Codabar, Interleaved 2 of 5Wide-to-narrow bar ratio, 2 to 3. Defaults: 2 for Code 39 and Codabar, 3 for Interleaved 2 of 5.
checksumCode 39, Interleaved 2 of 5, Industrial 2 of 5Adds a check character.
fullasciiCode 39, Code 93false limits the data to standard characters and uppercases it.
gs1Code 128, Data MatrixEncodes GS1 application identifiers.
eccQR Code, AztecError correction: L, M, Q or H for QR; a percentage for Aztec.
securityPDF417Error correction level, 0 to 8.
modeMaxiCodeMaxiCode 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 TypeMinimum Size
Code 128/39200x60 pixels
EAN-13250x80 pixels
QR Codes150x150 pixels
Data Matrix80x80 pixels
PDF417300x100 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.

Was this page helpful?