Hi dev,
Today, i show you how to add DomPDF add QR Code to PDF in laravel. In this article will tell you how to add DomPDF add QR Code to PDF in laravel. you will how to add DomPDF add QR Code to PDF in laravel. I will show in this example how to integrate QR code PDF. For this we will use two composer packages.
You can also use this example with Laravel 6, Laravel 7, Laravel 8, Laravel 9 and Laravel 10 versions.
So, let’s follow few steps to create example of how to add DomPDF add QR Code to PDF in laravel.
Step 1: Install Laravel App
composer create-project laravel/laravel example-qrcode
Step 2: Install DomPDF Package
composer require barryvdh/laravel-dompdf
composer require simplesoftwareio/simple-qrcode
Step 3: Create Controller
php artisan make:controller PDFController
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 Codeplaners.com', 'qrcode' => $qrcode ]; $pdf = PDF::loadView('myPDF', $data); return $pdf->download('codeplaners.pdf'); } }
Step 4: Add Route
routes/web.php
use Illuminate\Support\Facades\Route; use App\Http\Controllers\PDFController; Route::get('generate-pdf', [PDFController::class, 'generatePDF']);
Step 5: Create View File
resources/views/myPDF.blade.php
<!DOCTYPE html> <html> <head> <title>How to Add DomPDF Add QR Code to PDF in Laravel - codeplaners.com</title> </head> <body> <div> <h1>How to Add DomPDF Add QR Code to PDF in Laravel</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, Ezoic 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 }}" alt=""> </div> </body> </html>
Run Laravel App:
routes/web.php
php artisan serve
http://localhost:8000/generate-pdf
I hope it will assist you…