Watch Youtube Video (HINDI) :
How to Generate PDF File Using DomPDF In Laravel 9 Example, Using composer create-project laravel/laravel dompdf we will generate pdf. In this example, we will take an example of a user list. We will generate a pdf file with the user's data.
In laravel install composer create-project laravel/laravel dompdf then we will register the PDF namespace globally. At the end of the tutorial, you can easily create PDFs in your application.
Using domPDF pdf file generate is a powerful and popular tool in the laravel framework and also in core PHP. They provided many customization options and it is easy to integrate into your application.
Preview :
So let's start integrating dompdf into your laravel application.
Step 1. Clone Laravel Project
Let's start by cloning a new laravel application. Use the below-provided command, Also you may change the project name as per your requirement. Goto the terminal and run the command.
composer create-project laravel/laravel dompdf
Go to your project directory using "cd" and then give your project name and hit enter in your terminal.
cd dompdf
Step 2. Config Database
Once you create a new laravel project go to the .env file. The file you will find is the root of the application. add your database name and username and your database password to access your database in the laravel application.
If you haven't created a database go to "http://localhost/phpmyadmin" and create a new database and config in your application.
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=dompdf DB_USERNAME=root DB_PASSWORD=
Migrate all tables to the database using the below-suggested command in your terminal.
php artisan migrate
Step 3. Install Composer
Install dompdf using the below-provided command in your project terminal. This command will install dompdf composer in your project.
composer require barryvdh/laravel-dompdf
For giving a global namespace for the PDF class we will add an app.php file. Goto in your app.php file. The file you will find in the config file.
config/app.php'providers' => [ .... Barryvdh\DomPDF\ServiceProvider::class, ], 'aliases' => [ .... 'PDF' => Barryvdh\DomPDF\Facade::class, ]
Step 4. Create Controller
Use the below-provided command and create UserController in your application.
php artisan make:controller UserController
Goto web.php file is the route file you will find in the routes folder, just add two routes in this file. The first route for the view users list. The second route for the export or download pdf file.
routes/web.php<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\UserController; /* |-------------------------------------------------------------------------- | 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('user/list',[UserController::class,'userList'])->name('user.list'); Route::get('create/user/list/pdf',[UserController::class,'createUserListPDF'])->name('create.user.list.pdf');
Follow the controller like the below UserController Example given. Two methods are given here in this controller. The first method for the view users list and the second route for the download pdf file.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; use PDF; class UserController extends Controller { public function userList() { $users = User::all(); return view('user-list',compact('users')); } public function createUserListPDF() { $users = User::all(); view()->share('users',$users); $pdf = PDF::loadView('user-pdf-list') ->setPaper('a4','potrait') ->setOption(['dpi' => 150, 'defaultFont' => 'sans-serif']) ->setWarnings(false); //return $pdf->stream(); // OPEN return $pdf->download(); // DOWNLOAD } }
Step 5. Create Blade File
This is a blade file for listing users. The users fetch from the database and listed here. Here I imported bootstrap 5 CSS and script links the make a better HTML table.
<!doctype html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- Bootstrap CSS --> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"> <title>Laravel 9 PDF Generate Example - Webappfix</title> </head> <body> <div class="container mt-5"> <h2 class="text-center mb-3">Laravel 9 PDF Generate Example - Webappfix</h2> <div class="d-flex justify-content-end mb-4"> <a class="btn btn-primary" href="{{ route('create.user.list.pdf') }}">Export PDF</a> </div> <table class="table table-bordered mb-5"> <thead> <tr class="table-danger"> <th class="col">#</th> <th class="col">Name</th> <th class="col">Email</th> <th class="col">Verified At</th> </tr> </thead> <tbody> @foreach ($users as $key => $user) <tr> <th>{{++$key}}</th> <th>{{$user->name}}</th> <th>{{$user->email}}</th> <th>{{$user->email_verified_at}}</th> </tr> @endforeach </tbody> </table> </div> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script> </body> </html>
This below HTML code for the pdf view. The pdf file will generate from the blade file code. You can make changes according to your needs. Here is the same HTML table given in the preview. So the pdf file will generate from here.
<!doctype html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- Bootstrap CSS --> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"> <title>Laravel 9 PDF Generate Example - Webappfix</title> <style> .page-break { page-break-after: always; } </style> </head> <body> <div class="container-fluid"> <table class="table table-bordered mb-5"> <thead> <tr class="table-danger"> <th class="col">#</th> <th class="col">Name</th> <th class="col">Email</th> <th class="col">Verified At</th> </tr> </thead> <tbody> @foreach ($users ?? '' as $key => $user) <tr> <th>{{++$key}}</th> <th>{{$user->name}}</th> <th>{{$user->email}}</th> <th>{{$user->email_verified_at}}</th> </tr> @endforeach </tbody> </table> </div> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script> </body> </html>
Step 6. Create dummy data.
Open your terminal and run the below-suggested command. Once you enter the command it will ask you to enter query.
php artisan tinker
Run this query in PHP artisan tinker. Just paste code after running the tinker.
User::factory()->count(10)->create()
Step 7. Start Development Server
Start your application development server with the suggested command in your terminal.
php artisan server
Open your browser, and run the below-given URL in your browser.
http://127.0.0.1:8000/user/list
We always thanks to you for reading our blogs.
Dharmesh Chauhan
(Swapinfoway Founder)Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us
Haresh Chauhan
(Co-Founder)We Are Also Recommending You :
- Laravel 10 Restful API Passport Authentication Tutorial
- How to hide image URL from IMG tag src laravel
- Laravel Livewire CRUD Using Bootstrap Modal Full Example Guideline And Demo Source Code
- Laravel Eloquent Parent Child In Same Table With Example
- Arr::add() | Laravel Helper Function
- Laravel 9 File manager integration tutorial
- Error : Array to string conversion Laravel
- How to Upload Multiple Images In Laravel 10
- Laravel-9 Multi Authentication Guard Passport API Example
- Laravel Debugging Methods With Example