Learn how to generate barcode and QR code stickers in Laravel using Milon Barcode and SnappyPDF. This step-by-step guide covers installation, PDF export, and custom sticker sizes (2x1 inch) for inventory and product labeling.
Barcodes and QR codes are an essential part of product management, inventory tracking, and retail operations. In Laravel, you can easily generate both barcodes and QR codes, and even export them as print-ready PDF stickers with custom sizes.
In this step-by-step guide, we’ll cover:
Installing dependencies
Generating barcodes and QR codes in Laravel
Displaying them in Blade templates
Exporting as PDFs
Customizing sticker sizes (2in x 1in)
By the end, you’ll have a working barcode/QR code sticker system inside your Laravel project.
We’ll use two packages:
Milon Barcode – for generating barcode/QR images
Snappy PDF (Laravel Snappy) - for generating PDFs with custom page sizes
Run the following commands:
composer require milon/barcode
composer require barryvdh/laravel-snappy
Next, publish the configuration for Snappy:
php artisan vendor:publish --provider="Barryvdh\Snappy\ServiceProvider"
Make sure you also have wkhtmltopdf installed, since Snappy depends on it.
Download wkhtmltopdf here and configure the binary path in config/snappy.php
.
If you haven’t worked with Snappy before, check out this detailed guide on how to generate multi-page PDFs with header and footer using SnappyPDF in Laravel. It covers installation, setup, and configuring the binary path.
Milon Barcode provides simple helper methods:
{{-- Barcode Example --}}
{!! DNS1D::getBarcodeHTML('1234567890', 'C128') !!}
{{-- QR Code Example --}}
{!! DNS2D::getBarcodeHTML('https://example.com', 'QRCODE') !!}
You can also generate PNG Base64 images:
<img src="data:image/png;base64,{!! DNS1D::getBarcodePNG('1234567890', 'C128') !!}" alt="barcode" />
<img src="data:image/png;base64,{!! DNS2D::getBarcodePNG('https://example.com', 'QRCODE') !!}" alt="qrcode" />
At this point, you can already show barcodes and QR codes on any Blade page without worrying about PDFs.
Now, let’s create a controller method that:
Fetches products
Chooses between barcode or QR code
Passes data to a Blade view
Generates a PDF
public function barcode(Request $request)
{
$request->validate([
'code_type' => ['required', 'string', 'in:barcode,qrcode'],
], [
'code_type.in' => 'Code Type must be in (barcode,qrcode)'
]);
$products = Product::get();
$code_type = $request->code_type ?? 'barcode';
$data = [
'products' => $products,
'code_type' => $code_type,
];
$pdf = PDF::loadView('products.barcode', $data);
$name = $code_type === 'qrcode'
? "qrcodes.pdf"
: "barcodes.pdf";
return $pdf->download($name);
}
Here’s a simple Blade file (resources/views/products/barcode.blade.php
):
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Stickers</title>
<style>
body {
font-family: Arial, sans-serif;
font-size: 10px;
text-align: center;
}
.barcode-page {
margin-bottom: 20px;
}
</style>
</head>
<body>
@foreach($products as $product)
@php
$barcodeData = $product->product_code ?? 'UNKNOWN';
@endphp
<div class="barcode-page">
@if($code_type === 'qrcode')
<img src="data:image/png;base64,{!! DNS2D::getBarcodePNG($barcodeData, 'QRCODE') !!}" alt="qrcode" />
@else
<img src="data:image/png;base64,{!! DNS1D::getBarcodePNG($barcodeData, 'C128') !!}" alt="barcode" />
@endif
<div>{{ $product->product_name }}</div>
</div>
@endforeach
</body>
</html>
This will generate a simple PDF with barcodes/QR codes without enforcing a sticker size.
Now let’s force exact sticker dimensions. We’ll set:
Page size = 2 inches wide x 1 inch high
No margins
Labels styled to fit perfectly
Update the controller:
$pdf = PDF::loadView('products.barcode', $data)
->setOption('page-width', '2in')
->setOption('page-height', '1in')
->setOption('margin-top', '0')
->setOption('margin-right', '0')
->setOption('margin-bottom', '0')
->setOption('margin-left', '0')
->setOption('disable-smart-shrinking', true)
->setOption('zoom', '1');
And the Blade template:
<style>
html, body {
width: 2in;
height: 1in;
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
font-size: 7px;
}
.barcode-page {
width: 2in;
height: 1in;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 0.04in;
overflow: hidden;
text-align: center;
}
.barcode-page img {
max-width: 1.8in;
max-height: 0.6in;
object-fit: contain;
}
.barcode-label {
margin-top: 0.02in;
font-size: 7px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
</style>
This ensures each page is a single sticker (2x1 inch), perfect for printing on label sheets.
You can store multiple fields in QR codes. For example:
$fields = [];
if (!empty($product->product_code)) $fields[] = "ProductCode:{$product->product_code}";
if (!empty($product->tag_number)) $fields[] = "Tag:{$product->tag_number}";
if (!empty($product->serial_number)) $fields[] = "Serial:{$product->serial_number}";
if ($code_type === 'qrcode' && !empty($product->product_name)) {
$fields[] = "N:" . substr($product->product_name, 0, 40);
}
$barcodeData = implode('|', $fields);
This makes your QR codes much more informative than barcodes.
Once the PDF is generated, you can:
Download and open it in a PDF reader
Print on Avery-style label sheets
Stick them on your products
This approach ensures consistent and scannable barcodes/QR codes.
Generating barcode and QR code stickers in Laravel is straightforward with Milon Barcode and Snappy PDF.
We first created simple barcodes and QR codes, then moved on to exporting them as PDFs. Finally, we customized the sticker size to 2in x 1in, making it ready for professional printing.
With this setup, you can:
Print barcodes for inventory management
Generate QR codes with product details
Export consistent sticker sheets
This solution is flexible, scalable, and production-ready. Whether you need basic barcodes or custom-sized QR labels, Laravel makes it possible in just a few steps.