Laravel 10 generate pdf from html view example; In this tutorial, you will learn how to generate or create pdf from view, blade, html in Laravel 10.
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.
Sometimes, you need to generate a pdf file for various purposes. So, this tutorial completely guides you on Laravel 10 pdf generate a file from view, blade, and html with dompdf package step by step.
The Laravel 10 domPDF package make it simple to create/generate and download pdf file from views, blade and html.
How to Generate PDF File in Laravel 10 Using DOM PDF
Follow the below steps and generate pdf in Laravel 10 using DOMPdf library:
- Step 1 – Download Laravel 10 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 10 Application
First of all, start your terminal to download or install Laravel 10 new setup. Execute the following command into it to install new Laravel 10 app into your system:
composer create-project --prefer-dist laravel/laravel FormValidation
Step 2 – Install domPDF Package
In this step, open again your command prompt. And run the following command on it. To install 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, Visit your routes directory and open web.php file in any text editor. And add the following routes into web.php route 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, execute the following command on terminal/command prompt/command line to create controller file for your laravel applications; is as follow:
php artisan make:controller PDFController
Now, visit your laravel directory 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('tutsmake.pdf');
}
}
Step 6 – Create Blade File
Now, create blade view file for 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 10 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
Last step, open command prompt and run the following command to start developement server:
php artisan serve
Then open your browser and hit the following url on it:
http://127.0.0.1:8000/create-pdf-file