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 Snappy PDF. Create print-ready PDFs, embed product metadata, and print professional labels for inventory and retail.

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 are crucial for inventory management, product labeling, and retail operations. Using Laravel, you can automate sticker generation, create print-ready PDFs, and print professional-quality labels efficiently.

In this guide, you’ll learn:

  • How to install required Laravel packages

  • Generate barcodes and QR codes

  • Display codes in Blade templates

  • Export labels as PDFs with custom sizes

  • Add metadata to QR codes

  • Print professional stickers

By the end, you’ll have a complete sticker generation system ready for production.


1. Install Required Laravel Packages

We’ll use two popular packages:

  1. Milon Barcode – Generate 1D/2D barcodes and QR codes.

  2. Laravel Snappy – Convert Blade views into PDFs with custom dimensions.

Install via Composer:

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

Publish Snappy’s configuration:

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

Note: Snappy requires wkhtmltopdf. Install it and set the binary path in config/snappy.php.


2. Generate Barcodes & QR Codes in Blade

Milon Barcode supports HTML output and Base64 images.

Barcode (Code128):

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

QR Code Example:

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

For 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

Create a controller method to generate 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

File: 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. Add Metadata to QR Codes

You can embed extra product details in QR codes:

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

if ($product->serial_number) {
    $fields[] = "Serial:{$product->serial_number}";
}

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

6. Printing Professional Stickers

Once your PDF is ready:

  1. Download and open it in a PDF viewer.

  2. Print on 2in × 1in Avery-style sticker sheets.

  3. Apply labels to products for instant scanning.

This ensures uniform, scannable, professional-grade labels for inventory or retail use.


Conclusion

By using Milon Barcode and Laravel Snappy, you can:

  • Generate 1D and 2D barcodes & QR codes

  • Export custom-sized, print-ready PDFs

  • Embed product metadata into QR codes

  • Print professional-quality stickers

This approach is scalable, flexible, and ready for production, making it ideal for inventory tracking, retail labeling, or warehouse management.

FAQ: Barcode & QR Code Stickers in Laravel

1. What packages are needed to generate barcodes and QR codes in Laravel?

You need Milon Barcode for generating 1D/2D codes and Laravel Snappy to export Blade templates as PDFs. Both are installable via Composer.


2. Can I include product information in QR codes?

Yes! You can embed multiple product fields such as product code, name, and serial number into a QR code. This makes scanning more informative and useful for inventory management.


3. What PDF dimensions should I use for stickers?

Common professional labels are 2 inches × 1 inch. Snappy allows you to set custom page width and height for each PDF.


4. How can I print barcode/QR code stickers professionally?

After generating the PDF:

  1. Download and open it in a PDF viewer.

  2. Print on Avery-style sticker sheets.

  3. Apply labels to products for uniform and scannable stickers.

6. Can this system be used for large inventories?

Yes. The combination of Milon Barcode and Laravel Snappy is scalable. You can generate PDFs in bulk for hundreds or thousands of products.