Laravel 9 Generate PDF File using DomPDF

Laravel 9 Generate PDF File using DomPDF

Step 1: Install Laravel 9

created the new laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel app

Step 2: Install DomPDF Package

next, we will install DomPDF package using following composer command, let's run bellow command:

composer require barryvdh/laravel-dompdf

Step 3: Create Controller

run below command:

php artisan make:controller PDFController

Created some dummy data on the users table by using the following tinker command:

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

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 yoblogger.com',
            'date' => date('m/d/Y'),
            'users' => $users
        ]; 

        $pdf = PDF::loadView('myPDF', $data);

        return $pdf->download('yoblogger.pdf');
    }
}

Step 4: Add Route

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 which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('make-pdf', [PDFController::class, 'makePDF']);

Step 5: Create View File

resources/views/myPDF.blade.php

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

php artisan serve
http://localhost:8000/make-pdf

Output

Laravel-9-Generate-PDF-Example-yoblogger-com.png

Did you find this article valuable?

Support Mandeep Singh Blog by becoming a sponsor. Any amount is appreciated!