How to Generate PDF from HTML, Blade in Laravel 9

How to Generate PDF from HTML, Blade in Laravel 9

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.

Laravel 9 generate pdf from html view example; In this tutorial, we will learn how to generate or create pdf from view, blade, html in laravel 9.

How to Generate PDF from HTML, Blade in Laravel 9

Follow the below steps and generate pdf in laravel 9 using DOMPdf library:

  • Step 1 – Download Laravel 9 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 9 Application

First of all download or install laravel 9 new setups. So, open the terminal and type the following command to install new laravel 9 app into your machine:

composer create-project --prefer-dist laravel/laravel blog

Step 2 – Install domPDF Package

In this step, open again your command prompt. And run the following command on it. To install the 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' => [
'aliases' => [
	'PDF' => Barryvdh\DomPDF\Facade::class,

Step 4 – Create PDF Routes

In this step, open web.php file from the routes directory. 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 the command prompt to create a 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:

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',
            'date' => date('m/d/Y')
        $pdf = PDF::loadView('testPDF', $data);
        return $pdf->download('tutsmake.pdf');

Step 6 – Create Blade File

Now, create a blade view file to generate pdf from view. So, Go to resources/views and create testPDF.blade.php and update the following code into it:

<!DOCTYPE html>
    <title>Laravel 9 Generate PDF From View</title>
    <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>

Step 7 – Run Development Server

The last step, open a command prompt and run the following command to start development server:

php artisan serve

Then open your browser and hit the following url on it:

Recommended Laravel Tutorials


Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *