How to generator Barcode by using milon/barcode in Laravel?

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Today now in this post i am going to give you an example of how to generate the barcode in laravel application. We can easily create the barcode by using milon/barcode package that provide the several way to generate QR Code and also barcode. If we need to the generate barcode for our products then we can generate barcode sticker in our laravel application by using milon/barcode package. So here I will added a preview of barcode after the finish example. You can find a output like you want. Now It’s provide the several barcode as like Qr Code, PDF417, C39,C39+, C39E,C39E+, C93, S25,S25+, I25,I25+ etc.

Step 1: Installation

Today now in first step we will install the milon/barcode package for generate the bar-code image. So at first need to fire the bellow command in our cmd or terminal:

composer require milon/barcode

Okay now we need to add the provider path and alias path in the config/app.php file. So now open that file and add the bellow code.

config/app.php

return [
	......
	'provides' => [
		......
		......,
		Milon\Barcode\BarcodeServiceProvider::class
	],
	'aliases' => [
		......
		......,
		'DNS1D' => Milon\Barcode\Facades\DNS1DFacade::class,
        'DNS2D' => Milon\Barcode\Facades\DNS2DFacade::class,
	],
]
Step 2: Add Route

Now in the second step we will add the new two route for creating this small example on that way we can understand very well.

So at first add the bellow route in your routes.php file.

app/Http/routes.php

Route::get('barcode', '[email protected]');
Step 3: Add Controller Method

Now in this step we need to add barcode() method in our HomeController Controller file for our this example. We need to write the bellow method on HomeController.

Controller Method

public function barcode()
{
    return view('barcode');
}
Step 4: Add Blade file

Now we are in last step and we need to just create the new blade file as name barcode.blade.php and then put the bellow code on that file.

resources/views/barcode.blade.php

@extends('layouts.app')


@section('content')
<style type="text/css">
	img{
		padding-left: 20px;
	}
</style>
<div class="row">
    <div class="col-md-8 col-md-offset-2">
        <h1 class="text-primary" style="text-align: center;">Laravel 5 Barcode Generator Using milon/barcode</h1>
    </div>
</div>


<div class="container text-center" style="border: 1px solid #a1a1a1;padding: 15px;width: 70%;">
	<img src="data:image/png;base64,{{DNS1D::getBarcodePNG('11', 'C39')}}" alt="barcode" />
	<img src="data:image/png;base64,{{DNS1D::getBarcodePNG('12', 'C39+')}}" alt="barcode" />
	<img src="data:image/png;base64,{{DNS1D::getBarcodePNG('13', 'C39E')}}" alt="barcode" />
	<img src="data:image/png;base64,{{DNS1D::getBarcodePNG('14', 'C39E+')}}" alt="barcode" />
	<img src="data:image/png;base64,{{DNS1D::getBarcodePNG('15', 'C93')}}" alt="barcode" />
	<br/>
	<br/>
	<img src="data:image/png;base64,{{DNS1D::getBarcodePNG('19', 'S25')}}" alt="barcode" />
	<img src="data:image/png;base64,{{DNS1D::getBarcodePNG('20', 'S25+')}}" alt="barcode" />
	<img src="data:image/png;base64,{{DNS1D::getBarcodePNG('21', 'I25')}}" alt="barcode" />
	<img src="data:image/png;base64,{{DNS1D::getBarcodePNG('22', 'MSI+')}}" alt="barcode" />
	<img src="data:image/png;base64,{{DNS1D::getBarcodePNG('23', 'POSTNET')}}" alt="barcode" />
	<br/>
	<br/>
	<img src="data:image/png;base64,{{DNS2D::getBarcodePNG('16', 'QRCODE')}}" alt="barcode" />
	<img src="data:image/png;base64,{{DNS2D::getBarcodePNG('17', 'PDF417')}}" alt="barcode" />
	<img src="data:image/png;base64,{{DNS2D::getBarcodePNG('18', 'DATAMATRIX')}}" alt="barcode" />
</div>


@endsection

Read Also: Generate Captcha code and Validation by using BotDetect package in laravel

Thanks for read. I hope it help you. For more you can follow us on facebook

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →