How to generate a PDF File From Laravel view — Phpflow.com
In this example, I’ll show you how to generate a PDF file in Laravel from an HTML page. I’ll use the laravel-dompdf package to create PDF files, and it also has download file functionality.
I’ll show you how to make PDF files and download them to your system in a very simple method. In Laravel, we utilize the dompdf package to generate pdf files from HTML views.
What Is Dompdf Liberary ?
Dompdf is a laravel package that converts HTML to PDF using Laravel. The dompdf is a PHP-based HTML layout and rendering engine that is (largely) compliant with CSS 2.1.
It’ll download and read external stylesheets, inline style tags, and all the style attributes of individual HTML elements. It also supports the majority of HTML presentational attributes.
How To Generate a PDF in Laravel
Let’s integrate the Dompdf package with laravel and generate pdf file.
I am assuming, You have laravel application, if not please create an app using the below command:
composer create-project --prefer-dist laravel/laravel laravel_8_view_pdf_example
Step 1: Install The Laravel-Dompdf Package
With the help of a composer, you can easily install the dependencies of laravel-dompdf package. Please run the below command inside your laravel project:
laravel-app-path> composer require barryvdh/laravel-dompdf
Step 2: Configure Laravel-Dompdf Package
Just go to
Config/App.php and create these entries in providers and aliases.
Step 3: Create A Blade File
resources >> views folder, We'll create a new file called
Step 4 : Create Controller
Now create a controller on this path
app\Http\Controllers\EmployeeController.php and using the below command.
php artisan make:controller EmployeeController
Added the blow code into this file:
Step 5: Add Route path
Add Route information into the routes/web.php file.
Step 7 : Run Laravel App
Now, run the laravel project using the below command.
php artisan serve
open the below URL on your browser.
Originally published at https://www.phpflow.com on March 31, 2022.