{barcode}

Draws a barcode as inline SVG - EAN-13, EAN-8, UPC-A or Code 128, encoded on the server.

Charts and codes Single tags

Syntax

{barcode 'number'}
{barcode $code, type='code128', height=60, scale=2, title='text', plain, color='#000', background='#fff'}

Parameters

code
The number or text. An EAN or UPC number may leave its check digit off - it is added; one given is checked.

Options

type
ean13, ean8, upca or code128. Left out, twelve or thirteen digits are an EAN-13 and anything else Code 128.
height
The height of the bars in pixels, default 60.
scale
The width of one module - the thinnest bar - in pixels, default 2 (1 to 10).
title
The accessible name, by default the code.
plain
Bare option: the bars only, no digits under them.
color
The colour of the bars, default #000.
background
The colour behind them, default #fff.

Description

{barcode} encodes its first parameter as a one-dimensional barcode and writes it as inline SVG (tags/_lib/barcode.php) - the bars of a product, a ticket or a parcel label, drawn on the server, no font or image needed.

Without type the tag decides by the text: twelve or thirteen digits are an EAN-13, anything else Code 128. An EAN-13, EAN-8 or UPC-A number may be written without its check digit, which is then computed and added; a number written with it has the digit checked. These codes draw their guard bars longer and set the digits under the bars in their groups. Code 128 takes printable ASCII, and packs a run of four digits or more two to a symbol, so long numbers stay short.

scale is the width of the thinnest bar; keep it at 2 or more for a printed label a scanner has to read. plain leaves the human-readable digits out. The colours are fixed - black on white unless color and background say otherwise - since a scanner needs the contrast.

A wrong check digit, a wrong length for the type, a character Code 128 cannot hold or an unknown type is an error under the strict check, and the tag writes nothing. For a code a phone camera reads, see {qr}.

Examples

An EAN-13 with its check digit added

Twelve digits are taken as an EAN-13; the thirteenth, the check digit, is computed.

Templateexamples/barcode/1-ean.pad
{barcode '871234567890'}
Resultopen alone
8712345678906 8 712345 678906

Code 128 for a parcel

Text with letters is Code 128; height and scale size the bars.

Templateexamples/barcode/2-code128.pad
{barcode 'PAD-2026-00042', type='code128', height=50, scale=1.5}
Resultopen alone
PAD-2026-00042 PAD-2026-00042

Plain and coloured

plain leaves the digits out; color and background change the black on white.

Templateexamples/barcode/3-plain.pad
{barcode '96385074', type='ean8', plain, color='#1a237e'}
Resultopen alone
96385074

See also

  • {qr} Draws a QR code as inline SVG, encoded on the server - no image library, no remote service.