Laravel 9 Generate PDF File using DomPDF

In this article, we will implement Laravel 9 to generate a pdf file. You will learn how to generate a pdf in Laravel 9. Let’s see below the example Laravel 9 pdf generator. We will provide you with a simple example of how to generate a pdf file in Laravel 9. We will be using the DomPDF composer package to generate a pdf file in Laravel 9.

Step 1: Install Laravel 9

we need to first install laravel using the below command:

composer create-project laravel/laravel example-app

Step 2: Install DomPDF Package

Install the DomPDF package using the following composer command, using below command:

composer require barryvdh/laravel-dompdf

Step 3: Create Controller

Create a PDF Controller with generating pdf() where we will be writing a code of generating pdf. We will be creating a controller using the below command.

php artisan make:controller PDFController

In PDFController, there are table data that are displayed in a pdf file. You can use some dummy data to the user’s table by using the following tinker command:

php artisan tinker
User::factory()->count(10)->create()

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 WebHostGuru',
            'date' => date('m/d/Y'),
            'users' => $users
        ]; 
            
        $pdf = PDF::loadView('myPDF', $data);
     
        return $pdf->download('webhostguru.pdf');
    }
}

Step 4: Add Route

Open the routes/web.php file and update the code on it.

routes/web.php

<?php
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 that
| contains the "web" middleware group. Now create something great!
|
*/
  
Route::get('generate-pdf', [PDFController::class, 'generatePDF']);

Step 5: Create View File

Create myPDF.blade.php(resources/views/myPDF.blade.php) for the layout of the pdf file and use the below code:

resources/views/myPDF.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 9 Generate PDF Example</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>

Run Laravel App

Type the given below command and hit enter to run the Laravel app:

php artisan serve

Move to your web browser, type the given URL and view the app output:

http://localhost:8000/generate-pdf

you see a download with the file below:

Laravel 9 Generate PDF File

Notice: ob_end_flush(): failed to send buffer of zlib output compression (1) in /home/u212091429/domains/mydevhelp.com/public_html/wp-includes/functions.php on line 5420

Notice: ob_end_flush(): failed to send buffer of zlib output compression (1) in /home/u212091429/domains/mydevhelp.com/public_html/wp-includes/functions.php on line 5420