Laravel 9 Generate PDF File Example; In this consecutive tutorial you will learn how to generate or create pdf file from html blade view in laravel application. In laravel the DomPdf package is best to convert the html css content to pdf file.
The DomPdf package have all features to implement lines, tables, links and many more which you want. Here below the Laravel 9 Export to PDF topic have explained the step by step guide to generate PDF from HTML using the DomPDF library.
Laravel 9 PDF | How to Generate PDF File in Laravel
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.
Follow the below steps and generate pdf in laravel 9 using DOMPdf library:
- Step 1: Download Laravel 9 App
- Step 2: Install DomPDF Package
- Step 3: Make Routes
- Step 4: Create PDF Controller
- Step 5: Create Blade View File
- Step 6: Run Development Server
Download Laravel 9 App
In the beginning, open a terminal window and use the suggested command to create a fresh laravel application:
composer create-project --prefer-dist laravel/laravel laravel-pdf
Go into the app:
cd laravel-pdf
Install domPDF Package
Now execute the following command on terminal install the laravel-dompdf for generate or create pdf file in laravel:
composer require barryvdh/laravel-dompdf
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,
]
Make Routes
Next, get into the routes/web.php file, and add the following routes to web.php file:
use App\Http\Controllers\PDFController;
Route::get('create-pdf-file', [PDFController::class, 'index'])
Create PDF Controller
Now, generate a controller, this new controller will hold the generate pdf file in laravel programming logic.
php artisan make:controller PDFController
Plus, open app/Http/Controllers/PDFController.php file and place the following code.
<?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 codingdriver.com',
'date' => date('m/d/Y')
];
$pdf = PDF::loadView('test-pdf', $data);
return $pdf->download('codingdriver.pdf');
}
}
Create Blade File
Now go to resources/views direcotory and create test-pdf.blade.php and then open resources/views/test-pdf.blade.php file 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>
Run Development Server
Lastly, start the laravel development server using PHP artisan command:
php artisan serve
If you want to run the project diffrent port so use this below command
php artisan serve --port=8080
Test the application using the following url:
http://localhost:8000/create-pdf-file
Ultimately, the Laravel 9 generates slug tutorial is completed. In this laravel generate pdf post, we figured out how to create a pdf file in laravel 9 using the laravel dom-pdf package with example.