Installation Add the bundle via Composer:
composer require druidvav/barcode-bundle
Enable the bundle in config/bundles.php:
return [
// ...
Druidvav\BarcodeBundle\DruidvavBarcodeBundle::class => ['all' => true],
];
First Use Case Generate a simple barcode in a Twig template:
{{ barcode('code128', '123456789') }}
This renders a basic Code 128 barcode with the value 123456789.
Configuration
Check config/barcode.php for default settings (e.g., dimensions, formats). Override as needed:
# config/packages/druidvav_barcode.yaml
druidvav_barcode:
default_format: 'ean13'
default_width: 200
Dynamic Barcode Generation Use in controllers to generate barcodes on demand:
use Druidvav\BarcodeBundle\Generator\BarcodeGenerator;
public function generateBarcode(Request $request)
{
$generator = $this->get('druidvav_barcode.generator');
$barcode = $generator->generate('ean13', $request->input('value'));
return response()->streamDownload(function () use ($barcode) {
echo $barcode;
}, 'barcode.png');
}
Twig Integration Pass dynamic data to Twig:
{% for product in products %}
{{ barcode('ean13', product.sku, {
'width': 300,
'height': 100,
'format': 'png'
}) }}
{% endfor %}
Batch Processing Generate multiple barcodes in a loop (e.g., for invoices):
foreach ($orders as $order) {
$generator->generate('code39', $order->id, [
'save_path' => 'storage/barcodes/order_' . $order->id . '.png'
]);
}
# config/services.yaml
services:
App\Barcode\CustomBarcodeGenerator:
tags: ['druidvav_barcode.generator']
$barcode = $generator->generate('ean13', '123456789', ['format' => 'png']);
return response()->json(['barcode' => 'data:image/png;base64,' . base64_encode($barcode)]);
Unsupported Formats
The bundle defaults to a limited set of formats (e.g., ean13, code128). Verify support for your use case in the underlying library.
Fix: Use try-catch or validate formats before generation.
File Permissions
If saving barcodes to disk, ensure storage/ is writable:
mkdir -p storage/barcodes && chmod -R 775 storage/
Twig Caching
Barcodes generated in Twig may not update if the template is cached. Use {{ barcode(...) }} with is_escaped=false or disable caching for dynamic barcodes:
{{ barcode('ean13', product.sku, {'cache': false}) }}
try {
$generator->generate('unsupported', '123');
} catch (\Exception $e) {
$this->logger->error('Barcode generation failed: ' . $e->getMessage());
}
var_dump() to check raw barcode data before rendering:
$rawBarcode = $generator->generate('ean13', '123');
file_put_contents('debug_barcode.png', $rawBarcode);
Custom Generators
Implement Druidvav\BarcodeBundle\Generator\BarcodeGeneratorInterface to add proprietary formats:
class CustomGenerator implements BarcodeGeneratorInterface {
public function generate(string $type, string $value, array $options = []): string {
// Custom logic
}
}
Register the service with the druidvav_barcode.generator tag.
Override Defaults
Extend the bundle’s configuration via config/packages/druidvav_barcode.yaml:
druidvav_barcode:
formats:
custom:
class: App\Barcode\CustomGenerator
options: { /* ... */ }
Event Listeners Hook into barcode generation events (if the bundle supports them) to log or modify outputs. Example:
// src/EventListener/BarcodeListener.php
public function onBarcodeGenerate(BarcodeEvent $event) {
$event->setOption('width', 400); // Modify dynamically
}
How can I help you explore Laravel packages today?