Add QR Code in PDF using DomPDF Laravel?

29-Sep-2023

.

Admin

Add QR Code in PDF using DomPDF Laravel?

Hello Folks,

We'll teach you Laravel dompdf QR code in this short tutorial. Barryvdh dompdf pdf QR code can be learned right here. You will discover Laravel pdf qr code. In the demonstration, you will see how to add a QR code to a PDF file using Laravel. For instructions on adding a QR code to a PDF file using Laravel's dompdf, see the steps below.

In this guide, I will show you how to seamlessly integrate a QR code into a PDF document. To accomplish this, we will utilize two composer packages: simplesoftwareio/simple-qrcode for generating the QR code and barryvdh/laravel-dompdf for crafting the PDF file. Let's begin with these easy-to-follow steps:

Now, let's see an example step by step, you can use this example with laravel 6, laravel 7, laravel 8, laravel 9 and laravel 10 versions:

Step 1: Install Laravel


This step is not required; however, if you have not created the laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

Step 2: Install DomPDF Package

next, we will install the DomPDF and qr code package using the following composer command, let's run the below command:

composer require barryvdh/laravel-dompdf

composer require simplesoftwareio/simple-qrcode

Step 3: Create Controller

In this step, we will create PDFController with generatePDF() where we write the code of generate pdf. so let's create controller using bellow command.

php artisan make:controller PDFController

Now, update the code on the controller file.

app/Http/Controllers/PDFController.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 ItSolutionStuff.com',

'qrcode' => $qrcode

];

$pdf = PDF::loadView('myPDF', $data);

return $pdf->download('itsolutionstuff.pdf');

}

}

Step 4: Add Route

Furthermore, open routes/web.php file and update code on it.

routes/web.php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\PDFController;

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/

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

Step 5: Create View File

In Last step, let's create myPDF.blade.php for layout of pdf file and put following code:

resources/views/myPDF.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel 10 Generate PDF Example - ItSolutionStuff.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:

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

php artisan serve

Now, Go to your web browser, type the given URL and view the app output:

http://localhost:8000/generate-pdf

Now we are ready to run this example and check it...

I hope it can help you...

#Laravel 10

#Laravel