Laravel 9 Generate PDF File using DomPDF

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


Now, update the code on the controller file.

Read Also :  Laravel 8 Highcharts Example


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('Web-Tuts.pdf');

Step 4: Add Route

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

Read Also :  Laravel Arr hasAny() function Example


use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PDFController;
Route::get('generate-pdf', [PDFController::class, 'generatePDF']);

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:


<!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>


<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>Laravel 9</title>

    <body >

<a href="http://localhost:8000/generate-pdf"><button>Download</button></a>


Run Laravel App:

Read Also :  PayPal Integration in Laravel Example

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:


I hope you understand of laravel 9 dompdf example and it can help you…