Laravel 9 Generate Pdf file using Dompdf Step by Step

Laravel 9 Generate Pdf file example; In this tutorial you will learn how to create and download pdf file in laravel application using dompdf package. The dompdf liabrary is best to convert HTML view content to PDF in Laravel.

Using the berryvdh/dompf we will create pdf from a view, in this view, we will write the HTML code and load data dynamically from the database as per the requirement and we can export the view as a pdf file easily. So lets start for Laravel 9 Create HTML view to PDF file step by step tutorial example for you.

How to Generate PDF File in Laravel

Follow the below steps and generate pdf in laravel 8/9 using DOMPdf library:

  • Step 1 – Download Laravel App
  • Step 2 – Install DomPDF Package
  • Step 3 – Make Routes
  • Step 4 – Create PDF Controller
  • Step 5 – Create Blade View File
  • Step 6 – Run Development Server

Step 1: Download Laravel App

In the beginning, open a terminal window and use the suggested command to create a fresh laravel application:

composer create-project --prefer-dist laravel/laravel laravel-pdf

Go into the app:

cd laravel-pdf

Step 2. Install domPDF Package

Now execute the following command on terminal install the laravel-dompdf for generate or create pdf file in laravel:

composer require barryvdh/laravel-dompdf

In this step, registered this package in laravel application. So, Open the providers/config/app.php file and register the DOMPDF provider and aliases.

'providers' => [
    ....
    Barryvdh\DomPDF\ServiceProvider::class,
],
   
'aliases' => [
    ....
    'PDF' => Barryvdh\DomPDF\Facade::class,
]

Step 3. Add routes

Next, open the routes/web.php file, and add the following routes on it:

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

Step 4. Create PDF Controller

Now, generate a controller, this new controller will hold the generate pdf file in laravel programming logic.

php artisan make:controller PDFController

Plus, open app/Http/Controllers/PDFController.php file and place the following code.

<?php
   
namespace App\Http\Controllers;
   
use Illuminate\Http\Request;
 
use PDF;
   
class PDFController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = [
            'title' => 'Welcome to codingdriver.com',
            'date' => date('m/d/Y')
        ];
           
        $pdf = PDF::loadView('test-pdf', $data);
     
        return $pdf->download('codingdriver.pdf');
    }
}

Step 5. Create Blade File

Now go to resources/views direcotory and create test-pdf.blade.php and update the following code into it:

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 9 Generate PDF From View</title>
</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. Ut enim ad minim veniam,
    quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
    consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
    cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
    proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</body>
</html>

Step 6: Run Development Server

Lastly, start the laravel development server using PHP artisan command:

php artisan serve

If you want to run the project diffrent port so use this below command 

php artisan serve --port=8080  

Test the application using the following url:

http://localhost:8000/create-pdf-file

Ultimately, the Laravel generates slug tutorial is completed, In this laravel generate pdf post, we figured out how to create a pdf file in laravel 9 using the laravel dom-pdf package with example.

Leave a Comment