Overview
tc-lib-barcode is a pure-PHP barcode generation library for industrial, retail, logistics, and document automation workflows.
Encoding follows the published specification for each symbology, and the result comes back as a bar grid you can render however the target needs: SVG or PDF operators for print, PNG for a web preview. Coverage spans 73 formats across the linear, 2D, and postal families, including the GS1 DataBar and GS1-128 sets, the HIBC health industry profiles, the Chinese Han Xin Code, and the national postal symbologies of the United States, the United Kingdom, Germany, the Netherlands, Japan, and Australia.
Why This Library
- One API for every format:
getBarcodeObj()returns the same object for a UPC-A and a Han Xin Code, andgetInlineSvgCode(),getPngData(),getGrid()andgetArray()work across all 73 types. - Pure PHP with one runtime dependency,
tecnickcom/tc-lib-color, plus thegd,ctypeandpcreextensions;bcmathandimagickare optional accelerators. - Each symbology follows its own published specification, named alongside the format where one applies.
- Per-format encoding, ECC, size and input-validation test suites, run under strict static analysis on every commit.
- LGPL-3.0, so it can be used in closed-source applications.
- Part of the tc-lib-pdf / TCPDF ecosystem, in continuous development since 2015.
Repository and API Docs
- GitHub: https://github.com/tecnickcom/tc-lib-barcode
- API docs: https://tcpdf.org/docs/srcdoc/tc-lib-barcode
- Packagist: https://packagist.org/packages/tecnickcom/tc-lib-barcode
Project Metadata
| Item | Value |
|---|---|
| Namespace | \Com\Tecnick\Barcode |
| License | GNU LGPL v3 |
Installation
composer require tecnickcom/tc-lib-barcode
Where It Fits
Anywhere the symbol has to be produced on the server: shipping labels, event tickets, warehouse manifests, and the barcodes tc-lib-pdf draws into documents.
Supported Formats
Linear
| Format | Description |
|---|---|
| C39 | CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9 |
| C39+ | CODE 39 + CHECKSUM |
| C39E | CODE 39 EXTENDED |
| C39E+ | CODE 39 EXTENDED + CHECKSUM |
| C32 | CODE 32 (Italian Pharmacode - IMH - Radix 32) |
| C49 | CODE 49 (ANSI/AIM BC6) |
| PZN | PZN (Pharmazentralnummer - IFA coding system) |
| LOGMARS | LOGMARS (CODE 39 profile of MIL-STD-1189B) |
| C93 | CODE 93 - USS-93 |
| S25 | Standard 2 of 5 |
| S25+ | Standard 2 of 5 + CHECKSUM |
| S25IATA | 2 of 5 IATA (Computer Identics 2 of 5) |
| S25MATRIX | 2 of 5 Matrix |
| S25DATALOGIC | 2 of 5 Datalogic (China Post Code) |
| I25 | Interleaved 2 of 5 |
| I25+ | Interleaved 2 of 5 + CHECKSUM |
| ITF14 | ITF-14 (GTIN-14 - GS1 General Specifications) |
| C128 | CODE 128 |
| C128A | CODE 128 A |
| C128B | CODE 128 B |
| C128C | CODE 128 C |
| C16K | CODE 16K (stacked CODE 128) |
| GS1128 | GS1-128 (CODE 128 with GS1 Application Identifiers) |
| SSCC18 | SSCC-18 (GS1-128 with the Application Identifier 00) |
| GS114 | GS1-14 - EAN-14 - SCC-14 (GS1-128 with the Application Identifier 01) |
| DATABAR | GS1 DataBar Omnidirectional (ISO/IEC 24724) |
| DATABARTRUNC | GS1 DataBar Truncated (ISO/IEC 24724) |
| DATABARSTACK | GS1 DataBar Stacked (ISO/IEC 24724) |
| DATABARSTACKOMNI | GS1 DataBar Stacked Omnidirectional (ISO/IEC 24724) |
| DATABARLIMITED | GS1 DataBar Limited (ISO/IEC 24724) |
| DATABAREXP | GS1 DataBar Expanded (ISO/IEC 24724) |
| DATABAREXPSTACK | GS1 DataBar Expanded Stacked (ISO/IEC 24724) |
| EAN2 | EAN 2-Digits UPC-Based Extension |
| EAN5 | EAN 5-Digits UPC-Based Extension |
| EAN8 | EAN 8 |
| EAN13 | EAN 13 |
| UPCA | UPC-A |
| UPCE | UPC-E |
| PLESSEY | Plessey Code |
| MSI | MSI (Variation of Plessey code) |
| MSI+ | MSI + CHECKSUM (modulo 11) |
| TELEPEN | Telepen (full ASCII) |
| CODABAR | CODABAR |
| CODE11 | CODE 11 |
| PHARMA | PHARMACODE |
| PHARMA2T | PHARMACODE TWO-TRACKS |
| HIBC39 | HIBC in CODE 39 (ANSI/HIBC 2.6 and ANSI/HIBC 1.3) |
| HIBC128 | HIBC in CODE 128 (ANSI/HIBC 2.6 and ANSI/HIBC 1.3) |
| LRAW | 1D RAW MODE (comma-separated rows of 01 strings) |
2D
| Format | Description |
|---|---|
| AZTEC | AZTEC Code (ISO/IEC 24778:2008) |
| AZTECRUNE | AZTEC Rune (ISO/IEC 24778:2008 Annex A) |
| DATAMATRIX | DATAMATRIX (ISO/IEC 16022) |
| DMRE | Data Matrix Rectangular Extension (ISO/IEC 21471) |
| PDF417 | PDF417 (ISO/IEC 15438:2006) |
| PDF417C | Compact PDF417 - truncated (ISO/IEC 15438:2006) |
| QRCODE | QR-CODE |
| MICROQR | Micro QR Code (ISO/IEC 18004) |
| HANXIN | Han Xin Code (GB/T 21049, ISO/IEC 20830) |
| HIBCDM | HIBC in DATAMATRIX (ANSI/HIBC 2.6 and ANSI/HIBC 1.3) |
| HIBCQR | HIBC in QR-CODE (ANSI/HIBC 2.6 and ANSI/HIBC 1.3) |
| HIBCAZ | HIBC in AZTEC Code (ANSI/HIBC 2.6 and ANSI/HIBC 1.3) |
| SRAW | 2D RAW MODE (comma-separated rows of 01 strings) |
Postal
| Format | Description |
|---|---|
| POSTNET | POSTNET |
| PLANET | PLANET |
| RMS4CC | RMS4CC (Royal Mail 4-state Customer Bar Code) |
| KIX | KIX (Klant index - Customer index) |
| JPPOST | Japan Post Customer Barcode |
| MAILMARK | Royal Mail Mailmark 4-state barcode (types C and L) |
| IDENTCODE | Deutsche Post Identcode |
| LEITCODE | Deutsche Post Leitcode |
| AUSPOST | Australia Post 4-State Customer Barcode |
| IMB | IMB - Intelligent Mail Barcode - Onecode - USPS-B-3200 |
| IMBPRE | IMB - Intelligent Mail Barcode pre-processed |
Rendering
Width, height, padding, foreground and background color are set per barcode.
Output Formats
- SVG image: file, inline code, or standalone document
- PNG image, rendered with GD or with Imagick when the extension is loaded
- GD image object
- HTML
divelements - Character grid string
- Array of bar coordinates, as
XYXYorXYWH
Typed Enums
Symbology and per-type options are also available as backed enums, so the type token no longer has to be a raw string. The string form remains fully supported: every affected parameter is declared as a string|Enum union.
| Enum | Purpose |
|---|---|
\Com\Tecnick\Barcode\BarcodeType | Supported symbologies; the backing value is the leading type token accepted by getBarcodeObj(). |
\Com\Tecnick\Barcode\Type\Square\QrCode\QrEccLevel | QR Code error correction level (L, M, Q, H). |
\Com\Tecnick\Barcode\Type\Square\QrCode\QrEncodingMode | QR Code data encoding mode. |
\Com\Tecnick\Barcode\Type\Square\MicroQrCode\MicroQrEccLevel | Micro QR Code error correction level. |
\Com\Tecnick\Barcode\Type\Square\MicroQrCode\MicroQrEncodingMode | Micro QR Code data encoding mode. |
\Com\Tecnick\Barcode\Type\Square\Datamatrix\DatamatrixShape | Datamatrix symbol shape. |
\Com\Tecnick\Barcode\Type\Square\Datamatrix\DatamatrixEncoding | Datamatrix encoding scheme. |
\Com\Tecnick\Barcode\Type\Square\Dmre\DmreSize | Data Matrix Rectangular Extension symbol size. |
\Com\Tecnick\Barcode\Type\Square\Aztec\AztecHint / AztecRange | Aztec encoding hints and ranges. |
\Com\Tecnick\Barcode\Type\Square\HanXin\HanXinEccLevel | Han Xin Code error correction level (L1, L2, L3, L4). |
Extra parameters are still appended to the type token after a comma (for example QRCODE,H).
Input Validation
Every type validates its payload and raises \Com\Tecnick\Barcode\Exception with the reason when the code cannot be represented, rather than emitting a symbol no scanner can read. Character set, length, and check digit are checked per symbology, GS1 Application Identifier strings are parsed against their element definitions, and Barcode::MAX_CODE_LENGTH caps the payload at 30000 bytes.
Validation happens at encode time, so a rejected code surfaces where the data is, not where the label is printed.
The Aztec, Datamatrix, Micro QR and GS1 DataBar Expanded encoders are verified by round-trip decoding tests that check both the decoded payload and the symbol structure.
Integration Notes
Pick the symbology from what the scanners in the field can read and from how much data the payload carries; those two constraints usually decide it between them.
Normalize the payload before encoding. A value that does not fit the symbology (wrong length, a character outside the charset, a bad check digit) raises an exception at encode time rather than producing a symbol that scans wrong.
The GS1 types split by input form. GS1128, DATABAREXP and DATABAREXPSTACK take the bracketed element string (ai)value(ai)value..., which is also the human readable interpretation returned by getExtendedCode(); the library inserts the FNC1 separators, and parentheses are reserved as delimiters so they cannot appear in a value. SSCC18, GS114, ITF14 and the five fixed-length DataBar variants take a plain digit string instead: a code of the full length must carry a valid check digit, a shorter one is left-padded with zeros and the check digit is appended.
The HIBC data structure is independent of the symbology that carries it, so the same string encodes as HIBC39, HIBC128, HIBCDM, HIBCQR, or HIBCAZ; the modulo 43 check character is appended for you.
Scale and quiet zone are the usual cause of labels that read on screen and fail on the printer. Fix them per printer DPI profile and keep the setting with the profile.
Requirements
- PHP 8.2 or later
- Extensions:
ctype,gd,pcre - Optional extensions:
bcmath: speeds up the arbitrary precision arithmetic used by the IMB and PDF417 types. When the extension is missing, a pure-PHP fallback (\Com\Tecnick\Barcode\Math) is used instead.imagick: alternative PNG renderer used bygetPngData()when the extension is loaded.
- Package dependency:
tecnickcom/tc-lib-color - Composer
Development and Packaging
- QA and local checks:
make deps,make help,make qa - Coverage report:
make qa-coverage - Local example server:
make server(ormake server PORT=8080) - Packaging:
make rpm,make deb
Identifiers Issued by a Registration Body
The library encodes the data it is given. Several formats carry a number that only its registration body can issue, so a symbol is valid in its scheme only once that number has been obtained:
| Format | Issued by |
|---|---|
| EAN, UPC, ITF-14, GS1-128, SSCC-18, GS1-14, GS1 DataBar | a GS1 member organisation, which issues the company prefix |
| HIBC39, HIBC128, HIBCDM, HIBCQR, HIBCAZ | HIBCC, which assigns the Labeler Identification Code |
| MAILMARK | Royal Mail, which issues the Supply Chain ID carried in the barcode |
| PZN | IFA GmbH, which allocates the Pharmazentralnummer |
| IDENTCODE, LEITCODE | Deutsche Post DHL, which assigns the customer number and the street codes |
| C32 | AIFA, which assigns the Italian AIC number |
Trademarks
The names below identify the symbologies this library encodes and belong to their respective owners. They are used descriptively; no affiliation with or endorsement by their owners is claimed or implied.
| Name | Owner |
|---|---|
| QR Code | DENSO WAVE INCORPORATED |
| GS1, GS1-128, GS1 DataBar | GS1 AISBL |
| Mailmark | Royal Mail Group Ltd |
| Intelligent Mail | United States Postal Service |
| PHARMA-CODE | Laetus GmbH |
| Telepen | S.B. Electronic Systems Ltd |
| HIBC, HIBCC | Health Industry Business Communications Council |
| KIX | PostNL |
| Identcode, Leitcode | Deutsche Post DHL |
GS1 licenses the claims necessary to implement its standards under its IP Policy, to GS1 members and to the participants of the work group that developed the standard, rather than to the public at large.
Support and Contribution
- Sponsor: https://github.com/sponsors/tecnickcom
- Contribution guide: https://github.com/tecnickcom/tc-lib-barcode/blob/main/CONTRIBUTING.md
- Security policy: https://github.com/tecnickcom/tc-lib-barcode/blob/main/SECURITY.md
Example
<?php
require_once __DIR__ . '/vendor/autoload.php';
$barcode = new \Com\Tecnick\Barcode\Barcode();
$bobj = $barcode->getBarcodeObj(
type: 'QRCODE,H',
code: 'https://tecnick.com',
width: -4,
height: -4,
color: 'black',
padding: [-2, -2, -2, -2],
)->setBackgroundColor('white');
echo $bobj->getInlineSvgCode();
The same call using the BarcodeType enum for the symbology token:
use Com\Tecnick\Barcode\BarcodeType;
$bobj = $barcode->getBarcodeObj(
type: BarcodeType::DATAMATRIX,
code: 'https://tecnick.com',
width: -4,
height: -4,
color: 'black',
);