Copied!
Laravel

The Ultimate Laravel Document Toolkit: PDF, Barcode, and QR Code Generation

laravel-barcode-qr-pdf-guide
Shahroz Javed
Dec 21, 2025 . 195 views

The Ultimate Laravel Document Toolkit: PDF, Barcode, and QR Code Generation

Modern Laravel applications frequently require barcodes, QR codes, and PDF generation for invoices, shipment labels, inventory tracking, and verification systems. This guide provides a complete, real-world implementation of all three using production-ready practices.

Introduction

Whether you are building an eCommerce platform, warehouse management system, or internal ERP, barcode and QR code generation is no longer optional. Laravel’s ecosystem provides mature, stable packages that allow you to implement these features cleanly and efficiently.

Problem Statement

Developers commonly face the following challenges when implementing document generation:

  1. Ensuring barcodes are compatible with real hardware scanners

  2. Generating QR codes that work across mobile devices

  3. Embedding barcodes and QR codes inside PDFs without rendering issues

  4. Avoiding custom image manipulation or unreliable libraries

⚠️ Note: Never implement barcode or QR logic manually. Always rely on a proven library to ensure scanner accuracy and long-term maintainability.

Recommended Packages

After evaluating ecosystem stability and long-term maintenance, the following packages are recommended:

  • milon/barcode – Barcode and QR code generation

  • barryvdh/laravel-dompdf – PDF generation

Installing Dependencies

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

Defining Routes for Barcode, QR Code, and PDF

The following routes demonstrate a complete working setup for all three outputs.

Route::get('/barcode', function () {
    $code = 'IB-2025-000001';
    return view('barcode.barcode', compact('code'));
});

Route::get('/qrcode', function () {
    $data = 'https://yourdomain.com/order/IB-2025-000001';
    return view('barcode.qrcode', compact('data'));
});

Route::get('/barcode-pdf', function () {
    $data = [
        'order_no' => 'IB-2025-000001',
        'url' => 'https://yourdomain.com/order/IB-2025-000001',
    ];

    $pdf = PDF::loadView('barcode.pdf', $data)
        ->setPaper('a4', 'portrait');

    return $pdf->download('barcode.pdf');
});

Generating Barcodes in Laravel

Code 128 is the most reliable barcode format for internal systems due to its high density and universal scanner support.

<!DOCTYPE html>
<html>
<head>
    <title>Barcode</title>
</head>
<body>

    <h3>Barcode (Code 128)</h3>

    {!! DNS1D::getBarcodeHTML($code, 'C128', 2, 60) !!}

    <p>{{ $code }}</p>

</body>
</html>

This output can be embedded directly into Blade views, emails, or PDF templates.

Generating QR Codes in Laravel

QR codes are best suited for URLs, tracking links, and verification data.

<!DOCTYPE html>
<html>
<head>
    <title>QR Code</title>
</head>
<body>
    <h3>QR Code</h3>

    {!! DNS2D::getBarcodeHTML($data, 'QRCODE', 6, 6) !!}

    <p>{{ $data }}</p>

</body>
</html>
💡 Read also: QR codes can also be generated as PNG or SVG files for storage, APIs, or external integrations.

Alternative QR Code Output (PNG / SVG)

If you need to store QR codes or return them via API, use PNG or SVG output instead of HTML.

// PNG output (Base64)
DNS2D::getBarcodePNG('https://example.com', 'QRCODE');

// SVG output
DNS2D::getBarcodeSVG('https://example.com', 'QRCODE');

Creating PDFs with Barcode and QR Code

PDF generation is essential for invoices, shipment labels, and official documents. DomPDF integrates seamlessly with Blade templates.

<!DOCTYPE html>
<html>
<head>
    <style>
        body {
            font-family: DejaVu Sans, sans-serif;
            font-size: 12px;
        }
        .box {
            border: 1px solid #000;
            padding: 15px;
            width: 300px;
        }
    </style>
</head>
<body>
    <h2>Shipment Label</h2>

    <div class="box">
        <strong>Order No:</strong> {{ $order_no }}
        {!! DNS1D::getBarcodeHTML($order_no, 'C128', 2, 50) !!}

        {!! DNS2D::getBarcodeHTML($url, 'QRCODE', 5, 5) !!}
        <small>{{ $url }}</small>
    </div>

</body>
</html>
⚠️ Always use DejaVu Sans or another Unicode-safe font in PDFs to avoid barcode and QR rendering issues.

Expected Output

  • Scannable Code 128 barcodes

  • Mobile-friendly QR codes

  • Printable, professional PDFs

Best Practices for Production

  1. Use Code 128 for all internal identifiers

  2. Use QR codes only for URLs or long strings

  3. Cache barcode output in high-traffic systems

  4. Generate bulk PDFs using queues

Conclusion

With the right packages and structure, Laravel becomes a powerful document generation platform. This toolkit-style approach ensures clean code, scanner compatibility, and long-term scalability.

🚀 Related: How to generate bulk shipment labels and invoices in Laravel using queues.
📑 On This Page