In this tutorial you will learn about the Laravel 8 Generate PDF File using DomPDF Tutorial and its application with practical example.
In this Laravel 8 Generate PDF File using DomPDF Tutorial I will show you how to generate pdf file using DomPDF in laravel. In this tutorial you will learn to generate or convert a pdf file using DomPDF in laravel. In this example we will be using DomPDF library to generate pdf file. In this article I will show you how to install and use DomPDF package to generate pdf file in laravel. You will also learn to install DomPDF with laravel. Before starting with this tutorial you are required to install DomPDF package in laravel.
Laravel 8 Generate PDF File using DomPDF Tutorial
In this step by step tutorial I will demonstrate you how to install DomPDF package in laravel. As well as I will also show you how to generate pdf file using DomPDF package in laravel. Please follow the instruction given below:
- 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 we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Step 2 – Install domPDF Package
In this step, we will install dompdf Package via the composer dependency manager. Use the following command to install dompdf Package.
1 |
composer require barryvdh/laravel-dompdf |
Step 3 – Register DOMPDF Package
Now we will register this package in laravel application. So, Open the providers/config/app.php file and register the DOMPDF provider and aliases.
1 2 3 4 5 6 7 8 9 |
'providers' => [ .... Barryvdh\DomPDF\ServiceProvider::class, ], 'aliases' => [ .... 'PDF' => Barryvdh\DomPDF\Facade::class, ] |
Step 4 – Create PDF Routes
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 |
use App\Http\Controllers\PDFController; Route::get('create-pdf-file', [PDFController::class, 'index']) |
Step 5 – Create PDF Controller By Artisan Command
Now, lets create a controller named PDFController using command given below –
1 |
php artisan make:controller PDFController |
Now, go to app/http/controllers and open PDFController.php file. And put the following code into it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?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 w3adda.com', 'date' => date('m/d/Y') ]; $pdf = PDF::loadView('testPDF', $data); return $pdf->download('a\w3adda.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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<!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
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://127.0.0.1:8000/create-pdf-file |