How to Generate Bar Code in Laravel

AuthorSumit Dey Sarkar

Pubish Date22 Mar 2023


In this tutorial we will learn how to generate bar code in laravel.


How to generate bar code in laravel

Generating a barcode in Laravel can be achieved by using a barcode library like "Picqer/php-barcode-generator".

Follow below steps to generate a barcode in Laravel:


Step 1- Install the library via Composer by run below command in your terminal:

composer require picqer/php-barcode-generator


Step 2- Create a controller and add the following code to generate a barcode:

use Picqer\Barcode\BarcodeGeneratorPNG;

class BarcodeController extends Controller
    public function generateBarcode($code)
        $generator = new BarcodeGeneratorPNG();
        header('Content-Type: image/png');
        echo $generator->getBarcode($code, $generator::TYPE_CODE_128);

In this code, we are using the BarcodeGeneratorPNG class from the Picqer\Barcode namespace to generate the barcode. We then set the content type to image/png and output the generated barcode.


Step 3- Create a route in web.php to call the controller method:

Route::get('/barcode/{code}', 'BarcodeController@generateBarcode');

This will allow us to call the generateBarcode method by passing a code parameter to the URL like http://localhost/barcode/123456.


Step 4 - Call the URL in the browser to see the generated barcode.


Note: You may need to adjust the namespace and use statements based on your application's folder structure.


That's it! You now have a basic implementation of generating a barcode in Laravel.

Comments 0

Leave a comment