PDF stands for “portable document format“. PDFs are typically used to distribute read-only documents that preserve the layout of a page. They’re commonly used for documents like user manuals, eBooks, application forms, and scanned documents, to name just a few.
Laravel 9 generate pdf from html view example; In this tutorial, we will learn how to generate or create pdf from view, blade, html in laravel 9.
How to Generate PDF from HTML, Blade in Laravel 9
Follow the below steps and generate pdf in laravel 9 using DOMPdf library:
- Step 1 – Download Laravel 9 Application
- Step 2 – Install DomPDF Package
- Step 3 – Register DOMPDF Package
- Step 4 – Create PDF Routes
- Step 5 – Create PDF Controller By Artisan Command
- Step 6 – Create Blade View File
- Step 7 – Run Development Server
Step 1 – Download Laravel 9 Application
First of all download or install laravel 9 new setups. So, open the terminal and type the following command to install new laravel 9 app into your machine:
composer create-project --prefer-dist laravel/laravel blog
Step 2 – Install domPDF Package
In this step, open again your command prompt. And run the following command on it. To install the DOMPDF package:
composer require barryvdh/laravel-dompdf
Step 3 – Register DOMPDF Package
In this step, registered this package in laravel application. So, Open the providers/config/app.php file and register the DOMPDF provider and aliases.
'providers' => [
....
Barryvdh\DomPDF\ServiceProvider::class,
],
'aliases' => [
....
'PDF' => Barryvdh\DomPDF\Facade::class,
]
Step 4 – Create PDF Routes
In this step, open web.php file from the routes directory. And update the following routes into web.php file:
use App\Http\Controllers\PDFController;
Route::get('create-pdf-file', [PDFController::class, 'index'])
Step 5 – Create PDF Controller By Artisan Command
In this step, run the following command on the command prompt to create a controller file:
php artisan make:controller PDFController
After that, go to app/HTTP/controllers and open PDFController.php file. And update the following code into it:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use PDF;
class PDFController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$data = [
'title' => 'Welcome to Tutsmake.com',
'date' => date('m/d/Y')
];
$pdf = PDF::loadView('testPDF', $data);
return $pdf->download('neveropen.pdf');
}
}
Step 6 – Create Blade File
Now, create a blade view file to generate pdf from view. So, Go to resources/views and create testPDF.blade.php and update the following code into it:
<!DOCTYPE html>
<html>
<head>
<title>Laravel 9 Generate PDF From View</title>
</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. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</body>
</html>
Step 7 – Run Development Server
The last step, open a command prompt and run the following command to start development server:
php artisan serve
Then open your browser and hit the following url on it:
http://127.0.0.1:8000/create-pdf-file
Recommended Laravel Tutorials