Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Barcode Bundle Laravel Package

druidvav/barcode-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. 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],
    ];
    
  2. 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.

  3. 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
    

Implementation Patterns

Core Workflows

  1. 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');
    }
    
  2. Twig Integration Pass dynamic data to Twig:

    {% for product in products %}
        {{ barcode('ean13', product.sku, {
            'width': 300,
            'height': 100,
            'format': 'png'
        }) }}
    {% endfor %}
    
  3. 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'
        ]);
    }
    

Advanced Patterns

  • Custom Formats: Extend the bundle by adding new barcode types via services:
    # config/services.yaml
    services:
        App\Barcode\CustomBarcodeGenerator:
            tags: ['druidvav_barcode.generator']
    
  • API Responses: Return barcodes as base64-encoded images:
    $barcode = $generator->generate('ean13', '123456789', ['format' => 'png']);
    return response()->json(['barcode' => 'data:image/png;base64,' . base64_encode($barcode)]);
    

Gotchas and Tips

Common Pitfalls

  1. 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.

  2. File Permissions If saving barcodes to disk, ensure storage/ is writable:

    mkdir -p storage/barcodes && chmod -R 775 storage/
    
  3. 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}) }}
    

Debugging Tips

  • Log Errors: Wrap generation in a try-catch to log unsupported formats:
    try {
        $generator->generate('unsupported', '123');
    } catch (\Exception $e) {
        $this->logger->error('Barcode generation failed: ' . $e->getMessage());
    }
    
  • Inspect Output: Use var_dump() to check raw barcode data before rendering:
    $rawBarcode = $generator->generate('ean13', '123');
    file_put_contents('debug_barcode.png', $rawBarcode);
    

Extension Points

  1. 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.

  2. Override Defaults Extend the bundle’s configuration via config/packages/druidvav_barcode.yaml:

    druidvav_barcode:
        formats:
            custom:
                class: App\Barcode\CustomGenerator
                options: { /* ... */ }
    
  3. 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
    }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle