Skip to content

All topics  /  Laravel

How to Create Barcode Generator in Laravel 11?

Laravel ·

Teams applying “How to Create Barcode Generator in Laravel 11?” in a production project can use this delivery reference to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.

Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.

Hi, Dev

In this guide, I'll walk you through the process of generating barcodes within your Laravel 11 application. Our tool of choice will be the picqer/php-barcode-generator composer package, offering seamless barcode creation integration in Laravel 11.

In this demonstration, we'll create a barcode using the picqer/php-barcode-generator composer package. I'll provide a straightforward example showcasing the generation of barcodes with TYPE_CODE_128 and TYPE_CODE_39 types.

Let's see the steps below, and you can generate a barcode in your Laravel 11 projects as well.

Install Laravel 11


This step is not required; however, if you have not created the Laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

Install picqer/php-barcode-generator

In this step, we will install the `picqer/php-barcode-generator` package that provides a way to generate barcodes in a Laravel application. So, first, open your terminal and run the command below:

composer require picqer/php-barcode-generator

1: Laravel Generate Barcode Example

Here, we will create a simple route for generating a barcode. Then, I will show you the output below as well.

routes/web.php

<?php

  
use Illuminate\Support\Facades\Route;

  
Route::get('barcode', function () {
    $generatorPNG = new Picqer\Barcode\BarcodeGeneratorPNG();
    $image = $generatorPNG->getBarcode('000005263635', $generatorPNG::TYPE_CODE_128);
    return response($image)->header('Content-type','image/png');
});

Output:

2: Laravel Generate Barcode and Save Example

Here, we will create a simple route for generating a Barcode:

routes/web.php

<?php

  
use Illuminate\Support\Facades\Route;

  
Route::get('barcode-save', function () {
    $generatorPNG = new Picqer\Barcode\BarcodeGeneratorPNG();
    $image = $generatorPNG->getBarcode('000005263635', $generatorPNG::TYPE_CODE_128);
    Storage::put('barcodes/demo.png', $image);
    return response($image)->header('Content-type','image/png');
});

3: Laravel Generate Barcode with Blade Example

Here, we will create a simple route for generating a barcode. Then I will show you the output below as well.

routes/web.php

<?php

  
use Illuminate\Support\Facades\Route;

  
Route::get('barcode-blade', function () {
    $generatorHTML = new Picqer\Barcode\BarcodeGeneratorHTML();
    $barcode = $generatorHTML->getBarcode('0001245259636', $generatorHTML::TYPE_CODE_128);
    return view('barcode', compact('barcode'));
});

resources/views/barcode.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>How to Create Barcode Generator in Laravel 11? - NiceSnippets.com</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
</head>
<body>
<div class="container">
    <div class="card mt-5">
        <h3 class="card-header p-3">How to Create Barcode Generator in Laravel 11? - NiceSnippets.com</h3>
        <div class="card-body">
            <h3>Product: 0001245259636</h3>  
            {!! $barcode !!}
        </div>
    </div>
</div>

  
</body>
</html>

Output: