How to Generate PDF File by using DomPDF in Laravel 9 ?

Here today now in this post i will show you an example of How to Generate PDF File by using DomPDF in Laravel 9 ? So today our main topic is laravel 9 pdf file from the view by using DomPDF. Now in this article, i will implement how to generate pdf file in laravel 9. Here we can see how we can generate the pdf in laravel 9. Now we will learn about the laravel 9 to create a pdf from the view. So Let’s see the below example of laravel 9 pdf generator.

Here I will give you a very simple example about how we can generate any pdf file from the view in laravel 9. Here i will use DomPDF composer package for generate our pdf file in laravel 9 application. So just need to follow the below step and get a very simple pdf by using laravel 9.

Step 1: Install Laravel 9

This is first step. This step is not needed if you have already install in previous. However, if you don’t have then need to created the laravel application, then we need to go ahead and need to execute the below command:

composer create-project laravel/laravel example-app
Step 2: Install DomPDF Package

Now in the next, we need to install DomPDF package by using the following composer command, so let’s run the bellow command:

composer require barryvdh/laravel-dompdf
Step 3: Create Controller

Then now in this step, we need to create a controller as name PDFController also with the generatePDF() where we will write the code of generate pdf. So let’s create the controller by using the bellow command.

php artisan make:controller PDFController

Now in PDFController, we are also get the users table data and then display them into the pdf file. So we can add the some dummy data on the users table by using the following tinker command:

php artisan tinker

Now, need to update the code on the controller file.


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',
            'date' => date('m/d/Y'),
            'users' => $users
        $pdf = PDF::loadView('myPDF', $data);
        return $pdf->download('itsolutionstuff.pdf');
Step 4: Add Route

Now need to open the routes/web.php file and then update code on it.


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

Now in the last step, let’s need to create a blade file as name myPDF.blade.php(resources/views/myPDF.blade.php) for the layout of pdf file and then put the following code:


<!DOCTYPE html>
    <title>Laravel 9 Generate PDF Example -</title>
    <link rel="stylesheet" href="" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <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">
        @foreach($users as $user)
            <td>{{ $user->id }}</td>
            <td>{{ $user->name }}</td>
            <td>{{ $user->email }}</td>

Read Also: How to Create Custom Blade Directive in Laravel?

I hope it can help you. Follow us onĀ facebook


About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →