Barcode Generation Guide

Updated Mar 28, 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
  9. Real-World Examples
  10. Best Practices

1. Supported Barcode Types

TypeFunctionUse Case
Code 128code128General purpose, alphanumeric
Code 39code39Industrial, alphanumeric
QR CodeqrcodeURLs, large data, mobile scanning
Data MatrixdatamatrixSmall items, compact encoding
EAN-13ean13Retail products (13 digits)
PDF417pdf417Large data capacity, 2D

All barcodes are rendered as base64-encoded images embedded directly in your HTML/PDF.

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. Input auto-converts to uppercase.

Syntax

{{code39 .BarcodeData width height}}

Supported Characters

A-Z, 0-9, - . $ / + % and space.

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: checksum calculated automatically
  • 13 digits: last digit is checksum (recalculated)

7. PDF417

Stacked linear barcode with high data capacity.

Syntax

{{pdf417 .Data width height}}

Capacity

  • Up to 1,850 text characters
  • Up to 2,710 numeric digits

8. Generic Barcode Function

{{barcode "type" .Data width height}}

<!-- 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}}

9. Real-World Examples

Shipping Label

<div class="label">
  <h1>{{.Carrier | upper}} - {{.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 | upper}}</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)
Was this page helpful?