How to generate barcode using Laravel 8?

How to generate barcode using Laravel 8?

Let's discuss, how to generate barcode using Laravel 8?


Hello developers, I'm Tridev Sharma(Software Developer @Laysan Technologies Pvt Ltd). Recently I worked on a Laravel project and I got a new feature to develop in this project that is Barcode generation. So, I thought to write a blog to let you know how i did it?

This blog will guide you step by step to generate a barcode.
So let's start it.

Step 1

Open you terminal and write the following command to generate a fresh Laravel 8 Project.

$ compoer create-project laravel/laravel barcode-generation












When you run this command then it will generate a fresh Laravel Project. And you will have a success status like shown in below image. 

















If you have a running project then you may skip this project. Now let's move to the next step. After successful generation of project open it in a code editor. Here i am going to open it in Visual Studio Code.

Step 2

Now execute the following command in you command prompt by changing your directory to created project. If already you are ignore it.

$ composer require picqer/php-barcode-generator










Now let's run your application by running following command.

$ php artisan serve











Open your browser and open http://127.0.0.1:8000
If everything is fine then you will get Laravel welcome screen. Let's take another step.

Step 3

Let's open web.php from routes folder of your Laravel project and import install package for Barcode generation.

use Picqer\Barcode\BarcodeGeneratorHTML;
























Now simple write following code in home route function to generate html output for barcode.

$generator = new BarcodeGeneratorHTML;
return $generator->getBarcode('081231723897', $generator::TYPE_CODE_128);


use Picqer\Barcode\BarcodeGeneratorHTML;

Route::get('/', function () {
    // This will output the barcode as HTML output to display in the browser
    $generator = new BarcodeGeneratorHTML;
    return $generator->getBarcode('081231723897', $generator::TYPE_CODE_128);
});


Save the file and check your browser. Congratulations you successfully generated first Barcode in Laravel. It looks like following image.









If you want to save any barcode to specific file then use following code.


Route::get('/', function () {
    $generator = new Picqer\Barcode\BarcodeGeneratorPNG();
    file_put_contents('barcode.png', $generator->getBarcode('081231723897',
    $generator::TYPE_CODE_128, 3, 50));
});



After running this code a file will be created in public folder of you project if you don't specify path to save this barcode.

Hope this blog solved your problem.


Comments

Post a Comment