Share this:

Hello dev today we are going to learn how to add QR Code in PDF using DomPDF Laravel. In this tutorial, I will guide you through the process of integrating a QR code into a PDF document using Laravel.

We will achieve this by leveraging two essential composer packages: simplesoftwareio/simple-qrcode for generating the QR code and barryvdh/laravel-dompdf for creating the PDF file.

Follow these step-by-step instructions below:

Step 1: Install Laravel

This step is optional; if you haven’t created the Laravel app yet, you can proceed by running the following command:

composer create-project laravel/laravel example-app

Also Read: Laravel Send Welcome Email After Registration Example

Step 2: Install DomPDF Package

Next, install the DomPDF and QR code packages by using the following Composer command. Execute the command below:

composer require barryvdh/laravel-dompdf
composer require simplesoftwareio/simple-qrcode

Step 3: Create Controller

In this step, we’ll create a PDFController with a generatePDF() method, where we’ll write the code for generating the PDF. Let’s create the controller using the following command:

php artisan make:controller PDFController

Now, proceed to update the code in the controller file.

Also Read: Laravel Convert String to Lowercase Example

app/Http/Controllers/PDFController.php

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use PDF;
use SimpleSoftwareIO\QrCode\Facades\QrCode;    

class PDFController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function generatePDF()
    {
        $qrcode = base64_encode(QrCode::format('svg')->size(200)->errorCorrection('H')->generate('string'));
        $data = [
            'title' => 'Welcome to LaravelTuts.com',
            'qrcode' => $qrcode
        ]; 

        $pdf = PDF::loadView('myPDF', $data);
		
        return $pdf->download('laraveltuts.pdf');
    }
}

Step 4: Add Route

Moreover, open the routes/web.php file and proceed to update the code within it.

routes/web.php

<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PDFController;


Route::get('generate-pdf', [PDFController::class, 'generatePDF']);

Also Read: How to Install Sweetalert2 in Laravel 10 Vite?

Step 5: Create View File

In the final step, create myPDF.blade.php to define the layout of the PDF file. Insert the following code into the file:

resources/views/myPDF.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 10 Generate PDF Example - LaravelTuts.com</title>
</head>
<body>
<div>
    <h1>Laravel PDF with QR Code Example</h1>

    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod

    tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,

    quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo

    consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse

    cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non

    proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

    <img src="data:image/png;base64,{{ $qrcode }}">

</div>
</body>
</html>

Run Laravel App:

You have completed all the necessary steps. Now, simply enter the command provided below and press enter to run your Laravel app:

php artisan serve

Also Read: Installing Socialite Package in Laravel 10

Now, open your web browser, enter the provided URL, and explore the output of your application:

http://localhost:8000/generate-pdf

We are now prepared to run this example and observe the results.

I hope this information proves helpful to you.

Share this:

Categorized in: