Laravel 8 generate pdf from html view example; In this tutorial, you will learn how to generate or create pdf from view, blade, html in laravel 8.
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 8 pdf generate a file from view, blade, and html with dompdf package step by step.
The laravel 8 domPDF package make it simple to create/generate and download pdf file from views, blade and html.
How to Generate PDF File in Laravel 8 Using DOM PDF
Follow the below steps and generate pdf in laravel 8 using DOMPdf library:
- Step 1 – Download Laravel 8 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 8 Application
First of all download or install laravel 8 new setup. So, open terminal and type the following command to install new laravel 8 app into your machine:
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, open web.php file from routes direcotry. 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 command prompt to create 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('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 8 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