Site Admin
Site Admin Founder of MeaningArticles
1361 Views

How to generate a barcode in Laravel example

Hello Dev.

In this article i can display you a way to generate barcode the use of milon/barcode package. In this example we will use milon/barcode package and we are able to enforce it in our laravel barcode example. milon/barcode package provide many functionalities and diffrent alternatives to generate barcode, it's offer numerous barcode like C39E,Qr Code, C39, C39E+, C93, S25, S25+, C39+, I25, I25+, PDF417 and so forth...

So, permit's start and comply with below step to get output.

Step 1: Install Laravel 8

first of all we want to get clean and new Laravel eight version application the use of bellow command, now open your terminal OR command prompt and then fire bellow command and run it:

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

 

Step 2: Installation milon/barcode package

After set up of project you want to install milon/barcode package to your application

composer require milon/barcode

 

Step 3: Add Service Provider And Aliase

After package set up we need to add service provider and aliase in config/app.php.

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 create controller on this path app\Http\Controllers\BarcodeController.php and add under command.

app\Http\Controllers\BarcodeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

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

 

Step 5: Add Route

We want to add route for generating Barcode and view file.

<?php

use Illuminate\Support\Facades\Route;

Route::get('barcode', 'BarcodeController@barcode');

 

Step 6: Create Blade File

Now, create barcode.blade.php file for generate Barcode on this path resources\views\barcode.blade.php and add beneath html code.

resources\views\barcode.blade.php

<html>	
	<head>
		<title>Generate Barcode - meaningarticles.com</title>
		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
	</head>
	<h1 class="text-primary" style="text-align: center;margin-bottom: 20px;">Laravel Barcode Generator - meaningarticles.com</h1>
	<div style="text-align: center;">
		<img src="data:image/png;base64,{{DNS1D::getBarcodePNG('11', 'C39')}}" alt="barcode" /><br><br>
		<img src="data:image/png;base64,{{DNS1D::getBarcodePNG('123456789', 'C39+',1,33,array(0,255,0), true)}}" alt="barcode" /><br><br>
		<img src="data:image/png;base64,{{DNS1D::getBarcodePNG('4', 'C39+',3,33,array(255,0,0))}}" alt="barcode" /><br><br>
		<img src="data:image/png;base64,{{DNS1D::getBarcodePNG('12', 'C39+')}}" alt="barcode" /><br><br>
		<img src="data:image/png;base64,{{DNS1D::getBarcodePNG('23', 'POSTNET')}}" alt="barcode" /><br/><br/>
	</div>
</html>

i'm hoping it assist you to, thanks for visit my article if you like my article then proportion together with your friend and social platform.