How to Create Barcode Generator in Laravel 11?

19-Apr-2024

.

Admin

How to Create Barcode Generator in Laravel 11?

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:

laravel-11-bar-code-generate

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/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 Create Barcode Generator in Laravel 11? - NiceSnippets.com</h3>

<div class="card-body">

<h3>Product: 0001245259636</h3>

{!! $barcode !!}

</div>

</div>

</div>

</body>

</html>

Output:

laravel-11-bar-code-generate-2

I hope it can help you...

#Laravel 11