Updated April 11, 2023
What is Dompdf Laravel?
Dompdf is a query based on the Laravel framework that will help generate a pdf file. It can be integrated in any environment, based on a Laravel framework. Laravel, as we all know is one of the most popular of frameworks and over the years has become the most sought after for e commerce solutions. The reason for it are many:
- Robust command sets.
- Easy to write codes.
- Scalable, to support expansion of ecommerce services.
- Myriad options, providing the client with many choices to choose from.
All this makes the Laravel framework a dream platform.
Another reason that is quite often overlooked is the capability of Laravel to integrate itself with affiliate frameworks. For example, Laravel cashier can integrate with the Sprint Billing system to create a complete subscription profile.
In this article we are going to look at the Dompdf package, which enables the user to print pdf files through the Laravel framework.
How to Use Dompdf Laravel?
- It is a query, similar to the other Laravel query forms, where the package has to be installed first and then configured. A Layout Blade file needs to be created thereafter. Model and Migration files need to be created after which the Controller and the routes need to be too.
- Now a form blade for the input file need to created too.
- Store the data in the database.
- A view file for displaying the data needs to be created hence.
- A route to download the pdf file has to be defined.
- blade.php created to design the pdf file.
- The pdf file needs to be downloaded. A controller for the same needs to be created.
- PDF is ready. Download it.
Now that we have understood the steps involved, let us look at an example, which will make it even more clearer.
Step 1:
A package has to be downloaded. Here we would be using laravel-dompdf which will help download pdf file
//download a pdf file
return $pdf->download('users-list.pdf');
Step 2:
Creation of the project: A fresh project has to be created in laravel. This will be created through a composer.
composer create-project laravel/laravel kodementor
Step 3:
Set the Database up: A MYSQL database has been setup. We would name it kodementor. This will be set in the .env file.
// .env file
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=kodementor
DB_USERNAME=root
DB_PASSWORD=secret
Step 4:
Creation of Authentication: This has to be done since the user list has to be exported. A laravel authentication which is the default authentication has to be created.
php artisan make:auth
Step 5:
The tables need to be migrated now: Point to remember that we already have default migrations for the users and the reset tables for password. We will use these migrations to create tables for users and password reset.
php artisan migrate
Step 6:
Creation of Routes: Three routes need to be created:
- Users listing
- Downloading of the pdf
- Viewing of the pdf in the browser
// routes/web.php
Route::get('users', ['as' => 'users', 'uses' => 'UserController@index']);
Route::get('view-pdf/', ['as'=>'view-pdf','uses'=>'UserController@viewPDF']);
Route::get('download-pdf/', ['as'=>'download-pdf','uses'=>'UserController@downloadPDF']);
Step 7:
Creation of Controllers: A usercontroller has to be created. Here three methods need to be defined.
- Index
- viewPDF
- downloadPDF
php artisan make:controller UserController
Retrieving the User List
// App/Http/Controllers/UserController.php
public function index()
{
$users = $this->user->get();
return view('users.index', ['users' => $users]);
}
Create downloadPDF method
// // App/Http/Controllers/UserController.php
public function downloadPDF()
{
$users = $this->user->get();
// load view for pdf file
$pdf = PDF::loadView('pdf.users', ['users' => $users]);
return $pdf->download('users.pdf');
}
Create a viewPDF method
public function viewPDF()
{
$users = $this->user->get();
$pdf = PDF::loadView('pdf.users', ['users' => $users]);
return $pdf->setPaper('a4')->stream();
}
Hence, the controller will now look like
// App/Http/Controllers/UserController.php
user = $user;
}
/**
* List all users
* @return view [description]
*/
public function index()
{
//get all users
$users = $this->user->get();
return view('users.index', ['users' => $users]);
}
/**
* Download PDF on Local Storage
* @return pdf [description]
*/
public function downloadPDF()
{
//get all users
$users = $this->user->get();
// load view for pdf
$pdf = PDF::loadView('pdf.users', ['users' => $users]);
//download pdf
return $pdf->download('users.pdf');
}
/**
* View PDF on the browser
* @return pdf [description]
*/
public function viewPDF()
{
//get all users
$users = $this->user->get();
// load view for pdf
$pdf = PDF::loadView('pdf.users', ['users' => $users]);
// stream pdf on browser
return $pdf->stream();
}
}
Step 8:
Creation of View: Now that we have been able to set up the setup, routes and controllers, it is time we setup the view files.
Two blade files will be created hence:
index.blade.php to display the users list in the directory
resources/views/users/
users.blade.php will be used as a template for the pdf file which can be found in the directory resources/views/pdf/
// resources/views/users/index.blade.php
Generate PDF and Download or Stream(view online) in Laravel 5
@forelse ($users as $user)
@empty
No users
@endforelse
Name Email Joined Date
{{ $user->name }} {{ $user->email }} {{ $user->created_at }}
This is how the display of all users look like
Now for the view file:
// resources/views/users/index.blade.php
Users List
@foreach($users as $user)
@endforeach
Name Email Joined Date
{{ $user->name }} {{ $user->email }} {{ $user->created_at }}
Some additional configuration would be needed:
return PDF::loadFile(public_path().'/myfile.html')->save('https://cdn.educba.com/path-to/my_stored_file.pdf')->stream('download.pdf');
This can chain the methods
The orientation can be changed as well as the paper size:
PDF::loadHTML($html)->setPaper('a4', 'landscape')->setWarnings(false)->save('myfile.pdf')
Output:
Conclusion
Dompdf makes things easier for the developer to create queries for generation of pdf. It is a great way to provide a format which is sought after globally. With laravel-dompdf, one can easily go ahead create downloadable pdf file. The best part is and has already been stated previously, that the codes are easy to write codes and does not require much back breaking work. This has also been the hallmark for Laravel framework. It is a framework which is replete with options which directly minimises the effort of the developer and reduces the time to develop a function.
Laravel-dompdf is one such example.
Recommended Article
We hope that this EDUCBA information on “Dompdf Laravel” was beneficial to you. You can view EDUCBA’s recommended articles for more information.