Table of Contents
Inside this article we will see the concept of Generate PDF in Laravel 8 Using DomPDF package. This will be very interesting topic to see. We will convert a HTML view template into PDF document.
DomPDF is a package we install via composer which helps to work with PDF. So here, the article will be step by step guide to Generate PDF file using DomPDF in laravel 8.
This tutorial is not laravel 8 specific, we can use the same concept into other versions of laravel as well.
Let’s get started.
Installation of Laravel 8 Application
Laravel Installation can be done in two ways.
- Laravel Installer
- By using composer
Laravel Installer
To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.
$ composer global require laravel/installer
This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –
$ laravel
This command will open a command palette of Laravel Installer.
To create ad install laravel project in system,
$ laravel new blog
With the name of blog a laravel project will be created at your specified path.
By using composer
Alternatively, we can also install Laravel by Composer command create-project. If your system doesn’t has Composer Installed, Click here to Install Composer ? Here is the complete command to create a laravel project-
$ composer create-project --prefer-dist laravel/laravel blog
After following these steps we can install a Laravel 8 application into system. To start the development server of Laravel –
$ php artisan serve
This command outputs –
Starting Laravel development server: http://127.0.0.1:8000
Assuming laravel 8 already installed at system.
Install dompdf Package
Here, we will install a pdf package to laravel application. Open project in terminal and type this composer command to install.
$ composer require barryvdh/laravel-dompdf
When we install we should see the given image in terminal

Package Configuration in Application
Successfully, we have installed above. Next, we need to set some application configuration.
Open app.php from /config/app.php
Search for the array of Providers and aliases. Add the given code into providers and aliases array.
//.. Other Codes 'providers' => [ .... Barryvdh\DomPDF\ServiceProvider::class, ], 'aliases' => [ .... 'PDF' => Barryvdh\DomPDF\Facade::class, ]
Add Route
To add application route, open up the file web.php from /routes.
Open web.php and add this following code.
# Import Controller Class use App\Http\Controllers\PDFController; # Add Route Route::get('generate-pdf', [PDFController::class, 'index']);
Create Controller
Open laravel application into terminal and run this following command. This command will create a controller class file at location /app/Http/Controllers
$ php artisan make:controlle
r PDFController
Open controller file and write the given code into that.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use PDF; class PDFController extends Controller { public function generatePDF() { $data = [ 'title' => 'Welcome to OnlineWebTutorBlog.com', 'author' => "Sanjay" ]; $pdf = PDF::loadView('my-pdf-file', $data); return $pdf->download('onlinewebtutorblog.pdf'); } }
Create PDF Template File
Next, we need to create an email folder inside /resources/views directory. Inside email folder, then create a file named as my-pdf-file.blade.php
This template file will be the pdf file.
Open file my-pdf-file.blade.php
<!DOCTYPE html> <html> <head> <title>Title From OnlineWebTutorBlog</title> </head> <body> <h1>Title: {{ $title }}</h1> <h3>Author: {{ $author }}</h3> <p>ut aliquip ex ea commodoconsequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidata.</p> </body> </html>
Application Testing
Open project to terminal and type the command to start development server
$ php artisan serve
Open up the URL – <Project-URL>/generate-pdf
When we type this URL, it will download a pdf file into system.

We hope this article helped you to learn about i.e How Can Generate PDF in Laravel 8 Using DomPDF Tutorial in a very detailed way.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.
Hi, I am Sanjay the founder of ONLINE WEB TUTOR. I welcome you all guys here to join us. Here you can find the web development blog articles. You can add more skills in web development courses here.
I am a Web Developer, Motivator, Author & Blogger. Total experience of 7+ years in web development. I also used to take online classes including tech seminars over web development courses. We also handle our premium clients and delivered up to 50+ projects.