How to generate pdf file in laravel 12 using dompdf composer package, in this tutorial I will show you.
In Laravel 12 we will use the DomPDF Composer package to generate a PDF file. We will create 10 dummy users and some dummy text to add to the PDF file. So, let’s follow the steps below to create the PDF file:
Step for Laravel 12 Create PDF File using DomPDF Example
- Step 1: Install Laravel 12
- 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 12
To create the Laravel app, execute the below command:
laravel new 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 the first 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()
Here, you can update the code in the controller file.
Read More Blog Posts
Laravel 12 Form Validation Tutorial Example | TutorialDev
Laravel 12 File Upload Tutorial Example Full Guide
Laravel CRUD Application Example Tutorial
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 ItSolutionStuff.com', 'date' => date('m/d/Y'), 'users' => $users ]; $pdf = PDF::loadView('myPDF', $data); return $pdf->download('itsolutionstuff.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 12 Generate PDF Example - ItSolutionStuff.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 steps are 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