How to Generate Barcode in Laravel 11?

By Hardik Savani November 6, 2024 Category : Laravel

In this post, I will show you how to generate barcode in laravel 11 application. we will use picqer/php-barcode-generator composer package to create barcode in laravel 11.

In this example, we will generate a barcode using the picqer/php-barcode-generator composer package. I will give you a very simple example of generating a Barcode 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.

laravel 11 generate barcode

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 Generate Bar Code in Laravel 11? - ItSolutionStuff.com</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/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 Generate Bar Code in Laravel 11? - ItSolutionStuff.com</h3>
        <div class="card-body">
            <h3>Product: 0001245259636</h3>  
            {!! $barcode !!}
        </div>
    </div>
</div>
  
</body>
</html>

Output:

Output:

I hope it can help you...

Shares