How To Generate PDF Files using DomPDF In Laravel 9

Spread the love

we’ll guide you through a simple process to generate a PDF document using Laravel 9 and the DomPDF composer package. To create PDFs in Laravel 9, follow the comprehensive step-by-step instructions outlined below.

Step 1: Install Laravel 9

Also Read: How To Create Repeater Field Using Bootstrap

If you’ve already developed a Laravel application, there’s no requirement to run the following command:

composer create-project laravel/laravel example-app

Step 2: Install Laravel DomPDF Package

To add the Laravel DomPDF package to your project, simply execute the following Composer command. Let’s proceed by running the command below:

composer require barryvdh/laravel-dompdf

Step 3: Create Controller

In this stage, if you already possess a controller, there’s no necessity to fashion a new one. You can also bypass this phase if you don’t possess any existing controller. In such a case, kindly accompany me as we go through the process of crafting a PDFController with a generatePDF() method. This method is where we’ll compose the code responsible for generating PDFs. To initiate the controller creation process, execute the following command.

Note: Feel free to customize the controller name as per your preference for optimal flexibility.

php artisan make:controller PDFController

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

Step 4: Add Route

Begin by accessing your routes/web.php file and proceed to make the necessary updates to its code.

Also Read: How to use laravel cookies – Get, Set, Delete with example

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('generate-pdf', [PDFController::class, 'generatePDF']);

Step 5: Create View File

In this guide, we’ll walk you through the process of creating a file named “myPDF.blade.php” to serve as the layout template for your PDF document. You’ll also learn how to insert the necessary code within this file.

resources/views/myPDF.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>How To Generate PDF Files using DomPDF In Laravel 9 - itcodestuff.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 Your Laravel App:

Now that all the necessary steps have been completed, it’s time to launch your Laravel app using the following command:

php artisan serve

To access the application output, follow these steps:

  1. Open your preferred web browser.
  2. Enter the URL in the address bar.
  3. Observe the app’s output.
http://localhost:8000/generate-pdf

Download the PDF Output: Your Example Is Ready for Viewing

Embrace the elite realm of Laravel insights – your compass to the best Laravel blog awaits.

Leave a Comment