Weave 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

Laravel Barcode Generator Laravel Package

peal/laravel-barcode-generator

Generate barcodes in Laravel with a simple, fluent API. This package renders common barcode formats as SVG, PNG, or HTML, supports Blade directives, and lets you customize size, color, and encoding—making it easy to add scannable codes to invoices, labels, and product pages.

View on GitHub
Deep Wiki
Context7

Getting Started

Install and confirm what you actually got. The package is small (16 stars, no dependents), so read the source before you build on it.

composer require peal/laravel-barcode-generator

Verify auto-discovery registered a service provider:

php artisan about          # or:
composer show peal/laravel-barcode-generator

If the provider is not auto-discovered, add it to bootstrap/providers.php (Laravel 11+) or config/app.php.

Publish the config and look at it first. This is your fastest way to learn the real API surface — formats, symbologies, size options, and the underlying renderer are almost always declared there.

php artisan vendor:publish --tag=barcode-config

Where to look, in order:

  1. vendor/peal/laravel-barcode-generator/README.md — usually one or two examples only.
  2. vendor/peal/laravel-barcode-generator/config/*.php — the actual supported options.
  3. vendor/peal/laravel-barcode-generator/src/ — check whether it wraps an existing engine (likely picqer/php-barcode-generator or milon/barcode) or writes its own. This determines your renderer capabilities and bug surface.
  4. composer.json of the package — PHP version constraint and required extensions (ext-gd, ext-imagick).

First use case — render a SKU barcode in a Blade view. This is the 80% case: product/asset labels and invoice line items.

use Peal\Barcode\Barcode; // confirm the exact namespace/Facade alias from the provider

$svg = Barcode::generate('SKU-10042', 'C128', 2, 40);
{{-- Inline SVG scales cleanly and prints well --}}
{!! $svg !!}

If the package returns a Response or stores to disk instead of returning a string, adapt to .download() / Storage::url() accordingly — check the return type in src/ before wiring it into a view.


Implementation Patterns

1. Wrap it — do not call the facade from controllers and views directly

The package is a single-maintainer, low-adoption dependency. Put one thin abstraction in front of it so swapping to picqer/php-barcode-generator directly is a 20-line change, not a find-and-replace across the app.

namespace App\Support;

use Peal\Barcode\Barcode;

class BarcodeService
{
    public function svg(string $value, string $type = 'C128', int $width = 2, int $height = 40): string
    {
        return Barcode::generate($value, $type, $width, $height);
    }

    public function dataUri(string $value, string $type = 'C128'): string
    {
        return 'data:image/svg+xml;base64,' . base64_encode($this->svg($value, $type));
    }
}

Bind it as a singleton and inject it. Every call site stays engine-agnostic.

2. Model accessor for the barcode value

Keep symbology rules on the model so controllers and views never encode business logic about EAN-13 vs Code128.

public function getBarcodeAttribute(): string
{
    return $this->sku; // or $this->ean13 for EAN-13 symbology
}

public function barcodeSymbology(): string
{
    return strlen($this->sku) === 13 && ctype_digit($this->sku) ? 'EAN13' : 'C128';
}

3. Route + controller endpoint for on-demand generation

Never render barcodes as a side effect of a listing page — generate one per request, or cache. A dedicated endpoint gives you HTTP caching for free.

Route::get('/barcode/{product:sku}.svg', function (Product $product, BarcodeService $barcode) {
    return response($barcode->svg($product->sku, $product->barcodeSymbology()))
        ->header('Content-Type', 'image/svg+xml')
        ->header('Cache-Control', 'public, max-age=31536000, immutable');
});

4. Batch pre-generation into storage as a queued job

For label printing, warehouse runs, or PDF generation, pre-render assets rather than generating them inside a request.

Storage::disk('public')->put(
    "barcodes/{$product->sku}.svg",
    $barcode->svg($product->sku)
);

Wrap in a ShouldQueue job and dispatch per chunk of products. Store the path on the model so views resolve a URL, not a renderer call.

5. PDF invoices — use a data URI, not a URL

dompdf/snappy run offline or in a queue worker; they cannot fetch http://localhost/barcode/.... Base64-encode the SVG (or PNG) and inline it.

<img src="{{ $barcode->dataUri($order->reference) }}" style="width: 180px;">

Confirm your PDF driver supports SVG — many do not. In that case generate PNG via the package's png/toPng equivalent and base64 that instead.

6. Test the contract, not the package

it('renders a barcode for a product sku', function () {
    $product = Product::factory()->create(['sku' => 'SKU-10042']);

    $this->get("/barcode/{$product->sku}.svg")
        ->assertOk()
        ->assertHeader('Content-Type', 'image/svg+xml')
        ->assertSee('<svg', false);
});

Assert on shape (starts with <svg, returns 200, correct content type). Do not snapshot the SVG path data — layout will change with renderer upgrades.


Gotchas and Tips

Pin the version and vendor-audit the source. 16 stars and zero dependents means effectively zero community bug-finding. Use a caret constraint but commit composer.lock, and read src/ once end-to-end before shipping. If you find a blocking issue, patching your own thin wrapper is usually faster than waiting for an upstream release (last one was 2024-12-02).

Check the renderer's system requirements early. PNG output almost always needs ext-gd (or ext-imagick for some code paths). If your CI or production container is a minimal PHP image, you'll hit Class "Imagick" not found or a blank image at deploy time, not locally. SVG avoids this entirely — prefer it.

Symbology constraints are your problem, not the package's. Barcode libraries typically throw or silently produce unreadable output if you pass values that don't fit the symbology. EAN-13 requires exactly 13 digits (12 + check digit), UPC-A requires 11 or 12, Code128 rejects nothing but encodes inefficiently for long strings. Validate before calling:

if ($type === 'EAN13' && !preg_match('/^\d{13}$/', $value)) {
    throw new InvalidArgumentException("EAN-13 requires 13 digits, got: {$value}");
}

Whitespace before <?php breaks image responses. If you return raw image bytes via response(), any stray blank line in a config file, route file, or a dd() left behind produces a corrupted image. Returning SVG-as-string into a Blade view sidesteps this; returning PNG bytes does not.

Config caching hides published config changes. After editing config/barcode.php, run php artisan config:clear in development. In production, the config is cached at deploy — any runtime config() override won't apply until you re-cache.

DPI and module width matter more than "it looks fine on screen". A barcode that scans on a 96 DPI monitor can fail on a 300 DPI thermal printer. Set module width ($width) so the narrow bar is at least 2 printer dots, and always test with the actual scanner at the actual print size. SVG scales losslessly; rasterized PNGs do not — if you must use PNG, generate at final print resolution, never scale up.

Do not regenerate on every request in a loop. Rendering N barcodes per page (order line tables, asset lists) is a measurable CPU hit. Cache the file path or the SVG string keyed by md5($value . $type . $width . $height) and invalidate only when the underlying value changes.

Storage disk permissions are the usual failure mode for the "save to disk" API. Storage::url() will hand you a working path even when the public symlink is missing. Check php artisan storage:link and that storage/app/public is writable by the web user before assuming the generator is broken.

Watch for Facade alias collisions. If your app already uses milon/barcode or picqer, both may register a Barcode alias. Check config/app.php aliases and the package's extra.laravel.aliases in its composer.json before installing alongside an existing barcode dependency.

Extension points are likely thin. Given the package size, don't expect drivers, custom renderers, or events. If you need custom symbologies, multi-page label layouts, or QR codes, plan on picqer/php-barcode-generator (barcodes) plus endroid/qr-code (2D) directly — and treat this package as sugar you can drop.

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.
uni-method/symfony-lib
tacticmedia/scim-server-bundle
neika-ai/symfony-bundle
mattleads/agent-publishing-bundle
glavweb/framework-bundle
glavweb/extra-bundle
glavweb/api-bundle
glavweb/admin-bundle
adachsoft/api-version-bundle
elsayed85/laravel-api-exceptions
ouhssini/laravel-demo-mode
daworks/ncloud-outbound-mailer-driver
gp247/cms
ignibyte/aic-gates
nexa-merchant/blog
novasa/laravel-language-center
opencage/geocode
adolfoholzer/parameters
livenetworks/ln-starter
cgv/localized-resource