Generate Barcode In Laravel 8

July 26, 2021
Laravel

In this tutorial, you will learn How to Generate Barcode in Laravel 8.

I will be using milon/barcode package in order to Generate Barcode in Laravel.

The best thing about this package is:

  1. It can work with all the Laravel versions starting from 4 to 8.
  2. This Package has varieties of barcode schemes including Qrcode, PDF417, C39, C39+, C39E, C39E+, C93, S25, S25+, I25, I25+, C128, C128A, C128B, C128C, 2-Digits UPC-Based Extention, 5-Digits UPC-Based Extention, EAN 8, EAN 13, UPC-A, UPC-E, MSI (Variation of Plessey code) and etc.
  3. Gives variates of output options in form of HTML, PNG embedded base64 code and SVG canvas.

Let’s get started!

Step 1: Install Laravel

The first step is to install the Laravel, I am installing the Laravel in the bar_code folder.

composer create-project --prefer-dist laravel/laravel bar_code

Step 2: Install Barcode Package

Then we need to install the milon/barcode package, by using the following command:

composer require milon/barcode

Step 3: Add Service Provider And Aliases

Then we need to add the service provider and aliases in config/app.php.

'providers' => [
	
	Milon\Barcode\BarcodeServiceProvider::class,
],

'aliases' => [
	
	'DNS1D' => Milon\Barcode\Facades\DNS1DFacade::class,
    'DNS2D' => Milon\Barcode\Facades\DNS2DFacade::class,
],

Step 4: Create Controller

Now we will create BarcodeController by using the following command:

php artisan make:Controller BarcodeController

Here is how the controller will look like:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class BarcodeController extends Controller
{
    //
    public function index(){
        return view('barcode');
    }
}

In the BarcodeController, I have added the index function in which we are returning the barcode.blade.php view.

Step 5: Create View

Now I will create the resources/views/barcode.blade.php and will add the following code into it.

<html>
<head>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
</head>
<h1 class="text-primary text-center mb-4">Laravel Barcode Generator</h1>
<div class="text-center">
    <img src="data:image/png;base64,{{DNS1D::getBarcodePNG('11', 'C39',1,55,array(0,0,0), true)}}" alt="barcode" /><br><br>
    <img src="data:image/png;base64,{{DNS1D::getBarcodePNG('123456789', 'C39+',1,33,array(0,0,0), true)}}" alt="barcode" /><br><br>
    <img src="data:image/png;base64,{{DNS1D::getBarcodePNG('4', 'C39+',3,33,array(0,0,0),true)}}" alt="barcode" /><br><br>
    <img src="data:image/png;base64,{{DNS1D::getBarcodePNG('12', 'C39+',3,33,array(0,0,0),true)}}" alt="barcode" /><br><br>
    <img src="data:image/png;base64,{{DNS1D::getBarcodePNG('23', 'POSTNET',3,33,array(0,0,0),true)}}" alt="barcode" /><br/><br/>
</div>
</html>

In the above code, I have included the bootstrap and I am generating the different types of barcodes in the form of base64 format.

The getBarcodePNG function takes the first parameter as the barcode number itself, then width and height, then the colour of the barcode in the form of RGB using array and the last flag are for showing the barcode number itself under the barcode.

You can check more configurations here.

Step 6: Create Route

Now we need to create the route in app/web.php.

Route::get('/barcode',[\App\Http\Controllers\BarcodeController::class,'index']);

Step 7: Run Application

Now, let’s run the application using the following command:

php artisan serve

Now if you type in the browser http://127.0.0.1:8000/barcode you will see something like this:

Conclusion:

Congratulations, you have just finished the post on Barcode generation in Laravel. Hope you have learned something great today.

Let me know in the comments if it was helpful and feel free to ask if you have any questions.

See you in the next one 🙂

Write a Reply or Comment

Your email address will not be published. Required fields are marked *


Icon