Laravel 10 Generate PDF File using DomPDF

By Ved Prakash N | Apr 26, 2023 | Laravel
Share :

https://www.fundaofwebit.com/post/laravel-10-generate-pdf-file-using-dompdf-example

Laravel 10 Generate PDF File using DomPDF Example


In this post, you will be learning how to create or generate PDF file using DomPDF in Laravel application

I will give you a simple example of how to generate a pdf file in laravel 10. we will use the DomPDF composer package to generate a pdf file in laravel 10. let start with the below step and get a simple pdf using laravel 10.

Step 1: Create a laravel application OR you might be already working on a project.

Step 2: Install DomPDF Package

we will install DomPDF package via composer, by the artisan command as below:

composer require barryvdh/laravel-dompdf

after successful installation, let's do the Configuration by the following artisan command:

php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"


Step 3: Create the Controller

we will create a controller named PDFController with the following command:

php artisan make:controller PDFController

once the controller is created successfully, go to the path : app/Http/Controllers/PDFController.php and paste the below code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use Barryvdh\DomPDF\Facade\Pdf;

class PDFController extends Controller
{
    public function generatePDF()
    {
        $users = User::get();

        $data = [
            'title' => 'Welcome to Funda of Web IT - fundaofwebit.com',
            'date' => date('m/d/Y'),
            'users' => $users
        ];

        $pdf = PDF::loadView('pdf.usersPdf', $data);
        return $pdf->download('users-lists.pdf');
    }
}


Step 4: Add Route

open the file web.php in the path: routes/web.php and add the below route

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


Step 5: Create a view blade file for generate PDF

let's create usersPdf.blade.php file ( resources/views/pdf/usersPdf.blade.php ) for layout of pdf file and put following code:

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 10 Generate PDF Example - fundaofwebit.com</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KK94CHFLLe+nY2dmCWGMq91rCGa5gtU4mk92HdvYe+M/SXH301p5ILy+dN9+nJOZ" crossorigin="anonymous">
</head>
<body>

    <h1>{{ $title }}</h1>
    <p>{{ $date }}</p>
    <br/>
    <br/>

    <table class="table table-bordered">
        <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Email</th>
                <th>Created At</th>
            </tr>
        </thead>
        <tbody>
            @foreach($users as $user)
            <tr>
                <td>{{ $user->id }}</td>
                <td>{{ $user->name }}</td>
                <td>{{ $user->email }}</td>
                <td>{{ $user->created_at->format('d-m-Y') }}</td>
            </tr>
            @endforeach
        </tbody>
    </table>

</body>
</html>


Step 6: lets run the application by the following command:

php artisan serve

now, to download the users PDF file, open the below URL:

http://localhost:8000/generate-pdf


I hope, It will help you.

https://www.fundaofwebit.com/post/laravel-10-generate-pdf-file-using-dompdf-example

Share this blog on social platforms