How to generate PDF in Laravel 11 using Dompdf

In this tutorial you will learn How to generate PDF in Laravel 11 using Dompdf package and Bootstrap. The dompdf liabrary is best to convert HTML view content to PDF in Laravel.

Using the berryvdh/dompf package, we can generate a PDF from a view by embedding HTML code and dynamically loading data from the database as needed. This tutorial will guide you through the steps to create an HTML view and export it as a PDF file in Laravel 11.

Laravel and DomPDF: Generate Simple Invoice PDF

Let’s get started with the step-by-step example tutorial for creating an HTML view to PDF file in Laravel 11.

  • 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

# 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

# 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,
]

# Create Routes for Generate Pdf

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');

# 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');
    }
}

# 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 11 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>

# 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 11 using the laravel dom-pdf package with example.