How to Generate PDF File using DomPDF in Laravel 11?
Teams applying “How to Generate PDF File using DomPDF in Laravel 11?” in a production project can use the official walkthrough to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.
Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.
Hi, Dev
In this guide, I'll walk you through the process of generating a PDF file in Laravel 11 using DomPDF.
To accomplish this, we'll leverage the DomPDF Composer package. First, we'll install the package and then proceed to generate a PDF file containing dummy user data and text. Follow the steps outlined below to create your PDF file:
Step for Laravel 11 Create PDF File using DomPDF Example
Step 1: Install Laravel 11
Step 2: Install DomPDF Package
Step 3: Create Controller
Step 4: Add Route
Step 5: Create View File
Run Laravel App
Step 1: Install Laravel 11
This step is not required; however, if you have not created the Laravel app, then you may go ahead and execute the below command:
composer create-project laravel/laravel example-app
Step 2: Install DomPDF Package
Next, we will install the DomPDF package using the following Composer command. Let's run the command below:
composer require barryvdh/laravel-dompdf
Step 3: Create Controller
In this step, we will create a PDFController with a method called generatePDF() where we will write the code to generate a PDF. So, let's create the controller using the command below.
php artisan make:controller PDFController
In the `PDFController`, we also get users table data and display it into a PDF file. So, you can add some dummy data to the users table by using the following Tinker command:
php artisan tinker
User::factory()->count(10)->create()
Now, update the code in the controller file.
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 NiceSnippets.com',
'date' => date('m/d/Y'),
'users' => $users
];
$pdf = PDF::loadView('myPDF', $data);
return $pdf->download('NiceSnippets.pdf');
}
}
Step 4: Add Route
Furthermore, open `routes/web.php` file and add a GET route.
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PDFController;
Route::get('generate-pdf', [PDFController::class, 'generatePDF']);
Step 5: Create View File
In the last step, let's create `myPDF.blade.php` (`resources/views/myPDF.blade.php`) for the layout of the PDF file and put the following code:
resources/views/myPDF.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Laravel 11 Generate PDF Example - NiceSnippets.com</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" >
</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:
All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:
php artisan serve
Now, Go to your web browser, type the given URL and view the app output:
http://localhost:8000/generate-pdf
you will download the file as like below:
Now we are ready to run this example and check it...
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial