tecnickcom/tc-lib-barcode
Pure-PHP barcode generator for linear and 2D symbologies. Spec-driven, deterministic encoding suitable for labels, tickets, logistics, and compliance docs. Generate barcode data once and render as vector or raster output.
Install the package via Composer:
composer require tecnickcom/tc-lib-barcode
First Use Case: Generate a QR code for a URL in a Laravel controller:
use Com\Tecnick\Barcode\Barcode;
public function generateQrCode()
{
$barcode = new Barcode();
$bobj = $barcode->getBarcodeObj(
type: 'QRCODE,H',
code: 'https://example.com',
width: 3,
height: 3,
color: 'black',
padding: [2, 2, 2, 2]
)->setBackgroundColor('white');
return response($bobj->getPngImage(), 200, ['Content-Type' => 'image/png']);
}
Where to Look First:
Barcode.php class for core methods like getBarcodeObj(), getHtmlDiv(), and getPngImage().$barcode = new \Com\Tecnick\Barcode\Barcode();
$bobj = $barcode->getBarcodeObj(
type: 'EAN13', // Format (see supported list)
code: '1234567890128', // Data to encode
width: 1, // Relative width (1 = default)
height: 1, // Relative height (1 = default)
color: 'black', // Foreground color
padding: [1, 1, 1, 1] // [top, right, bottom, left] padding
);
$bobj->setBackgroundColor('white')
->setDisplayText(true) // Show human-readable text
->setFontSize(10); // Adjust text size
// For web responses
return response($bobj->getPngImage(), 200, ['Content-Type' => 'image/png']);
// For Blade templates
echo $bobj->getHtmlDiv();
// For PDFs (via TCPDF or DomPDF)
$svg = $bobj->getSvgImage();
Register the barcode generator as a singleton in AppServiceProvider:
public function register()
{
$this->app->singleton(\Com\Tecnick\Barcode\Barcode::class);
}
Create a facade (php artisan make:facade Barcode) and bind it:
// app/Facades/Barcode.php
public static function ean13($code)
{
return app(\Com\Tecnick\Barcode\Barcode::class)
->getBarcodeObj('EAN13', $code)
->setDisplayText(true);
}
Usage in Blade:
{{ Barcode::ean13('1234567890128')->getHtmlDiv() }}
For batch processing:
// app/Jobs/GenerateBarcodeJob.php
public function handle()
{
$barcode = Barcode::ean13($this->code);
Storage::put("barcodes/{$this->id}.png", $barcode->getPngImage());
}
// routes/web.php
Route::get('/barcode/{type}/{code}', function ($type, $code) {
$barcode = app(\Com\Tecnick\Barcode\Barcode::class)
->getBarcodeObj($type, $code)
->getPngImage();
return response($barcode, 200, ['Content-Type' => 'image/png']);
});
Usage:
<img src="/barcode/QRCODE,https://example.com" alt="QR Code">
Create a reusable component (app/View/Components/Barcode.php):
public function render()
{
$barcode = app(\Com\Tecnick\Barcode\Barcode::class)
->getBarcodeObj($this->type, $this->code)
->getHtmlDiv();
return <<<HTML
<div class="barcode-container">
{$barcode}
</div>
HTML;
}
Usage in Blade:
<x-barcode type="EAN13" code="1234567890128" />
use TCPDF;
// Generate barcode as SVG
$svg = Barcode::ean13('1234567890128')->getSvgImage();
// Add to PDF
$pdf = new TCPDF();
$pdf->AddPage();
$pdf->writeHTML($svg, true, false, true, false, '');
$pdf->Output('invoice.pdf', 'D');
PHP Extensions Missing:
Class 'GdImage' not found or bcmath warnings.gd, bcmath, and pcre extensions are enabled. For Docker:
RUN docker-php-ext-install gd bcmath pcre
Memory Limits for Large Barcodes:
Allowed memory size exhausted when generating complex 2D barcodes (e.g., PDF417).memory_limit in php.ini or optimize dimensions:
$bobj->setScale(0.5); // Reduce size
Invalid Barcode Data:
if (!preg_match('/^\d{13}$/', $eanCode)) {
throw new \InvalidArgumentException('Invalid EAN-13 code');
}
SVG Output Issues:
Case Sensitivity in Formats:
getBarcodeObj('ean13') may fail if the format is case-sensitive.'EAN13', 'QRCODE,H').Postal Barcode Quirks:
$postnetCode = str_pad($zipCode, 5, '0', STR_PAD_LEFT);
$rawData = $bobj->getUnicode(); // Debug barcode content
\Com\Tecnick\Barcode\Barcode::setDebug(true); // Enable debug mode
CODE128) before complex ones like PDF417.width: -4) scale relative to the default. Use 1 for default size.'#000000'), named ('black'), or RGB ('rgb(0,0,0)') colors.setDisplayText(true) adds human-readable text below the barcode. Disable for space-sensitive labels.[top, right, bottom, left]. Use 0 for no padding.\Com\Tecnick\Barcode\Barcode and overriding getBarcodeObj().$svg = $bobj->getSvgImage();
$svg = str_replace('<svg', '<svg xmlns:xlink="http://www.w3.org/1999/xlink"', $svg);
$cacheKey = "barcode:{$type}:{$code}";
How can I help you explore Laravel packages today?