Laravel Barcode Generator Tutorial With Example

Hello developers, In this example, I will explain to you how to do Laravel Barcode Generator Tutorial With ExampleWe will use milon/barcode package for generating barcode in laravel application. We generate barcode in HTML, but you can also generate code in png embedded base64 code or SVG canvas as per your requirement. There are two major barcode types.

  1. One-Dimensional (1D) Barcode Types
  2. Two-Dimensional (2D) Barcode Types

1) One-Dimensional(1D) Barcode Types:

One-dimensional, or 1D barcodes, systematically present data by modifying the widths and spacings of parallel lines, and may be referred to as one-dimensional or linear. These cover some of the traditional or most well-recognized barcode types such as the EAN and UPC types.UPC is used to label and scan consumer goods at points-of-sale around the world. It is mostly used in the United States, but also in the United Kingdom, New Zealand, Australia, and other countries.EAN is used to label consumer goods worldwide for point-of-sale scanning. It is mostly used in Europe. There are different types of 1D like ‘C39′,’S25′,’I25′,’MSI+’,’POSTNET’.  

2) Two-Dimensional(2D) Barcode Types:

Two-dimensional, or 2D barcodes, systematically describe data using two-dimensional symbols and shapes. They are related to a linear 1D barcode but can represent more data per unit area. There are 3 types of 2D like ‘QRCODE’,’PDF417′,’DATAMATRIX’. QRCODE used in retail, entertainment and advertising industry. PDF417 is used in logistics and government industry. DATAMATRIX is used in electronics, retail, and government industry.

Laravel Barcode Generator Tutorial With Example

We are going to Configure Laravel Project.

#1: Download Laravel Project

Establish  Laravel Project by the typing following command.

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

#2: Install milon/barcode Package

First, install milon/barcode package via the Composer package manager.

composer require milon/barcode

Let’s Add BarcodeServiceProvider in config/app.php

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

And finally, add in the alias section config/app.php

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

#3: Create a Controller

php artisan make:controller BarcodegeneratorController

It will build a controller file called BarcodegeneratorController.php.

Create a barcode() function in BarcodegeneratorController.

//BarcodegeneratorController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class BarcodegeneratorController extends Controller
{
    public function barcode()
    {
        return view('barcodegenerator');
    }
}

barcode() function return view called barcodegenerator which we create shortly.

#4: Define Route

We register all route in a web.php file.

//web.php

Route::get('/barcode','BarcodegeneratorController@barcode');

#5: Create a View File

Create a file in resources/ views/barcodegenerator.blade.php and put this following code in it.

<!-- barcodegenerator.blade.php --> 

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Laravel Barcode Generator Tutorial With Example </title>
    <link rel="stylesheet" href="{{asset('css/app.css')}}">
   </head>
<body>
   <h2>Laravel Barcode Generator Tutorial With Example</h2><br/>
<div class="container text-center">
<h2>One-Dimensional (1D) Barcode Types</h2><br/>
   <div>{!!DNS1D::getBarcodeHTML(8889899, 'C39')!!}</div></br>
   <div>{!!DNS1D::getBarcodeHTML(5436564, 'S25')!!}</div></br>
   <div>{!!DNS1D::getBarcodeHTML(77656765, 'I25')!!}</div></br>
   <div>{!!DNS1D::getBarcodeHTML(6435636, 'MSI+')!!}</div></br>
   <div>{!!DNS1D::getBarcodeHTML(25547, 'POSTNET')!!}</div></br>
 </div>
</body>
</html>

Here we can add One-Dimensional(1D) barcode types. We can add 1D barcode types like ‘C39′,’S25′,’I25′,’MSI+’,’POSTNET’. Your application as viewed in the screenshot below.

laravel barcode generator example

Next, we can add Two-Dimensional(2D)barcode types. We can add 2D barcode types like ‘QRCODE’,’PDF417′,’DATAMATRIX’.

<!-- barcodegenerator.blade.php --> 

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Laravel Barcode Generator Tutorial With Example </title>
    <link rel="stylesheet" href="{{asset('css/app.css')}}">
   </head>
<body>
   <h2>Laravel Barcode Generator Tutorial With Example</h2><br/>
<div class="container text-center">
   <h2>Two-Dimensional (2D) Barcode Types</h2><br/>
   <div>{!!DNS2D::getBarcodeHTML(335553, 'QRCODE')!!}</div></br>
   <div>{!!DNS2D::getBarcodeHTML(142535, 'PDF417')!!}</div></br>
   <div>{!!DNS2D::getBarcodeHTML(646, 'DATAMATRIX')!!}</div></br>
 </div>
</body>
</html>

laravel barcode tutorial

Final Code of barcodegenerator.blade.php looks like that.

<!-- barcodegenerator.blade.php -->

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Laravel Barcode Generator Tutorial With Example </title>
    <link rel="stylesheet" href="{{asset('css/app.css')}}">
   </head>
<body>
   <h2>Laravel Barcode Generator Tutorial With Example</h2><br/>
<div class="container text-center">
<h2>One-Dimensional (1D) Barcode Types</h2><br/>
	<div>{!!DNS1D::getBarcodeHTML(8889899, 'C39')!!}</div></br>
   <div>{!!DNS1D::getBarcodeHTML(5436564, 'S25')!!}</div></br>
   <div>{!!DNS1D::getBarcodeHTML(77656765, 'I25')!!}</div></br>
   <div>{!!DNS1D::getBarcodeHTML(6435636, 'MSI+')!!}</div></br>
   <div>{!!DNS1D::getBarcodeHTML(25547, 'POSTNET')!!}</div></br>
	<br/>
	<br/>
   <h2>Two-Dimensional (2D) Barcode Types</h2><br/>
   <div>{!!DNS2D::getBarcodeHTML(335553, 'QRCODE')!!}</div></br>
   <div>{!!DNS2D::getBarcodeHTML(142535, 'PDF417')!!}</div></br>
   <div>{!!DNS2D::getBarcodeHTML(646, 'DATAMATRIX')!!}</div></br>
 </div>
</body>
</html>

laravel barcode example

If you want to get more detail, then go to Github.

At last, our Laravel Barcode Generator Tutorial With Example is over.

Add a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.