Hello friends, Today will show how to use laravel pdf creator using dompdf. In this example you will learn laravel generate pdf from view. I will explain step by step laravel generate pdf from html. Sometime we need to laravel generate pdf from template.
Here, you will see laravel pdf generator package, generate pdf laravel 9, laravel dompdf example, download pdf using dompdf in laravel, laravel dompdf tutorial. So let’s following my step to crate dynamic pdf in lravel 9 using dompdf.
Laravel 9 DomPDF Example
Step 1: Install Laravel 9
This step is not need. if you have not created the laravel app, then you may go ahead and execute the following command.
composer create-project laravel/laravel example-app
Step 2: Install DomPDF Package
Now, we will install DomPDF package using bellow composer command, let’s run following command:
composer require barryvdh/laravel-dompdf
Step 3: Create Controller
In this step, we will create PDFController with generatePDF() where we write code of generate pdf. so let’s create controller using bellow command.
php artisan make:controller PDFController
in PDFController, we also get users table data and display them into pdf file. so you can add some dummy data on the users table by using the following tinker command:
php artisan tinker User::factory()->count(10)->create()
Now, update the code on the controller file.
app/Http/Controllers/PDFController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; use PDF; class PDFController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function generatePDF() { $users = User::get(); $data = [ 'title' => 'Welcome to Web-Tuts.com', 'date' => date('m/d/Y'), 'users' => $users ]; $pdf = PDF::loadView('myPDF', $data); return $pdf->download('Web-Tuts.pdf'); } }
Step 4: Add Route
Furthermore, open routes/web.php file and update code on it.
routes/web.php
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\PDFController; Route::get('generate-pdf', [PDFController::class, 'generatePDF']); Route::View('/','welcome');
Step 5: Create View File
In Last step, let’s create myPDF.blade.php(resources/views/myPDF.blade.php) for layout of pdf file and put following code:
resources/views/myPDF.blade.php
<!DOCTYPE html> <html> <head> <title>Laravel 9 Generate PDF Example - ItSolutionStuff.com</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> </head> <body> <h1>{{ $title }}</h1> <p>{{ $date }}</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> <table class="table table-bordered"> <tr> <th>ID</th> <th>Name</th> <th>Email</th> </tr> @foreach($users as $user) <tr> <td>{{ $user->id }}</td> <td>{{ $user->name }}</td> <td>{{ $user->email }}</td> </tr> @endforeach </table> </body> </html>
resources/views/welcome.blade.php
<!DOCTYPE html> <html lang="{{ str_replace('_', '-', app()->getLocale()) }}"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Laravel 9</title> </head> <body > <a href="http://localhost:8000/generate-pdf"><button>Download</button></a> </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
I hope you understand of laravel 9 dompdf example and it can help you…