How to Generate PDF File using DomPDF in Laravel 11

In this article, we'll see how to generate a PDF file using DomPDF in laravel 11. Here We will utilize the DomPDF Composer package to generate a PDF file within Laravel 11. In this task, we'll generate ten dummy user profiles and include some placeholder text within the PDF document

Also, You can create a new DOMPDF instance and load an HTML string, file, or view name. You can save it to a file, stream it (show it in the browser), or download it.

So, let's see that laravel 11 generates PDF files using DomPDF, how to create a PDF file in laravel 11, laravel generates PDF from view.

Step 1: Install Laravel 11

In the first step, we'll install laravel 11 using the following command. if you already installed laravel 11 then skip this step.

composer create-project laravel/laravel laravel-11-example


Step 2: Install DomPDF Package

Next, I will install the DomPDF package using the following Composer command.

composer require barryvdh/laravel-dompdf


Step 3: Create Controller

In this step, I will create a PDFController with a function named generatePDF(). So, run the following command to create a controller.

php artisan make:controller PDFController

After that, I'll create dummy records using the tinker.

php artisan tinker


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' => 'Techsolutionstuff',
            'date' => date('m/d/Y'),
            'users' => $users
        $pdf = PDF::loadView('pdf-example', $data);
        return $pdf->download('techsolutionstuff.pdf');
Step 4: Add Route

Now, I'll define the routes into the web.php file.


use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PDFController;
Route::get('generate-pdf', [PDFController::class, 'generatePDF']);
Step 5: Create View File

Then, I'll create a pdf-example.blade.php file for the PDF.


<!DOCTYPE html>
    <title>How to Generate PDF File using DomPDF in Laravel 11 - Techsolutionstuff</title>
    <link rel="stylesheet" href="" >
    <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">
        @foreach($users as $user)
            <td>{{ $user->id }}</td>
            <td>{{ $user->name }}</td>
            <td>{{ $user->email }}</td>


Step 6: Run Laravel 11 Application

Now, I'll run the laravel 11 application using the following command.

php artisan serve


You might also like:


Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.