How to Generate Barcode & QR Code Stickers in Laravel Using Milon Barcode and Snappy PDF

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.

Muhammad Ishaq
Muhammad Ishaq
21 Sep 2025
4-5 minute read
How to Generate Barcode & QR Code Stickers in Laravel Using Milon Barcode and Snappy PDF

Barcodes and QR codes play a vital role in inventory management, product labeling, and retail operations. In Laravel, you can automate sticker generation so your system not only creates codes but also exports them as print-ready PDF labels - perfect for professional use.

This guide will walk you through:

  • Installing dependencies

  • Generating barcodes & QR codes in Laravel

  • Displaying them in Blade templates

  • Exporting labels as PDFs (custom size: 2in × 1in)

  • Adding extra metadata to QR codes

  • Printing professional stickers

By the end, you’ll have a production-ready sticker generation module inside your Laravel project.


1. Install Required Packages

We’ll use two Laravel-friendly libraries:

Install via Composer:

composer require milon/barcode
composer require barryvdh/laravel-snappy

Publish Snappy’s configuration:

php artisan vendor:publish --provider="Barryvdh\Snappy\ServiceProvider"

Snappy depends on wkhtmltopdf. Install it from wkhtmltopdf.org and update the binary path in config/snappy.php.


2. Generate Barcodes & QR Codes in Blade

Milon Barcode provides both HTML and Base64 PNG outputs.

Barcode Example (Code128):

{!! DNS1D::getBarcodeHTML('1234567890', 'C128') !!}

QR Code Example:

{!! DNS2D::getBarcodeHTML('https://yourapp.com', 'QRCODE') !!}

If you prefer images instead of inline HTML:

<img src="data:image/png;base64,{!! DNS1D::getBarcodePNG('1234567890', 'C128') !!}" />
<img src="data:image/png;base64,{!! DNS2D::getBarcodePNG('https://yourapp.com', 'QRCODE') !!}" />

3. Build a Controller for Sticker PDFs

public function generateStickers(Request $request)
{
    $request->validate([
        'code_type' => 'required|string|in:barcode,qrcode',
    ]);

    $products = Product::all();
    $code_type = $request->code_type;

    $pdf = PDF::loadView('products.stickers', compact('products', 'code_type'))
        ->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);

    $fileName = $code_type === 'qrcode' ? 'qrcodes.pdf' : 'barcodes.pdf';

    return $pdf->download($fileName);
}

4. Blade Template for Stickers

resources/views/products/stickers.blade.php

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Stickers</title>
    <style>
        html, body {
            width: 2in;
            height: 1in;
            margin: 0;
            padding: 0;
            font-family: Arial, sans-serif;
            font-size: 7px;
        }
        .sticker {
            width: 2in;
            height: 1in;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            padding: 0.05in;
            text-align: center;
            overflow: hidden;
        }
        .sticker img {
            max-width: 1.8in;
            max-height: 0.6in;
        }
        .label {
            margin-top: 0.05in;
            font-size: 7px;
            text-overflow: ellipsis;
            white-space: nowrap;
            overflow: hidden;
        }
    </style>
</head>
<body>
    @foreach($products as $product)
        @php
            $codeData = $product->product_code ?? 'UNKNOWN';
        @endphp
        <div class="sticker">
            @if($code_type === 'qrcode')
                <img src="data:image/png;base64,{!! DNS2D::getBarcodePNG($codeData, 'QRCODE') !!}" />
            @else
                <img src="data:image/png;base64,{!! DNS1D::getBarcodePNG($codeData, 'C128') !!}" />
            @endif
            <div class="label">{{ $product->product_name }}</div>
        </div>
    @endforeach
</body>
</html>

5. Adding Extra Metadata to QR Codes

Make your QR codes more powerful by encoding multiple fields:

$fields = [
    "Code:{$product->product_code}",
    "Name:" . substr($product->product_name, 0, 40),
];
if ($product->serial_number) $fields[] = "Serial:{$product->serial_number}";

$barcodeData = implode('|', $fields);

Now your QR codes carry product code, name, and serial number all in one scan.


6. Printing Professional Stickers

Once the PDF is ready:

  • Download it and open in a PDF reader

  • Print on Avery-style sticker sheets (2in × 1in)

  • Stick directly on products for instant scanning

This ensures uniform, scannable, and production-grade labels.


Conclusion

With Milon Barcode and Laravel Snappy, you can build a barcode + QR code sticker system in just a few steps.

  • Generate both 1D and 2D codes

  • Export print-ready PDFs with custom sticker sizes

  • Add product metadata into QR codes

  • Print professionally on label sheets

This solution is scalable, flexible, and ready for production — whether for inventory tracking, retail labeling, or warehouse management.