How To Use DomPDF To Generate PDFs in Laravel 10

Reading Time: 4 minutes

Inside this article we will see the concept i.e How To Use DomPDF To Generate PDFs in Laravel 10. Article contains the classified information i.e Step-by-step guide to generate PDF with DomPDF in Laravel 10.

DomPDF is a powerful library for generating PDFs in PHP, and it can be easily integrated into Laravel 10 applications. With DomPDF, you can create professional-looking PDF documents from your Laravel views, and then save or output them to the user.

This tutorial will guide you through the process of setting up and using DomPDF in your Laravel 10 application.

Read More: How To Use Laravel 10 Seeder for Database Seeding Tutorial

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve


Assuming laravel already installed inside your system.

How To Install DomPDF Package to Laravel?

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.

Create Controller

Open project into terminal and run this command into it.

$ php artisan make:controller SiteController

It will create SiteController.php file inside /app/Http/Controllers folder.

Read More: Laravel 10 Send Email with Multiple Attachments Tutorial

Open controller file and write this code into it.


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Barryvdh\DomPDF\Facade\Pdf;

class SiteController extends Controller
    public function generatePDF()
        $data = [
            'title' => 'Welcome to',
            'author' => "Sanjay"
        $pdf = PDF::loadView('my-pdf-file', $data);
        return $pdf->download('onlinewebtutorblog.pdf');


$pdf = PDF::loadView('my-pdf-file', $data);

return $pdf->download('onlinewebtutorblog.pdf');

It first creates a PDF from a blade template and then download it.

Create Template

Create a file named as my-pdf-file.blade.php inside /resources/views folder. This template file will be the layout for pdf file.

Read More: Laravel 10 Send Email with PDF Attachment Tutorial

Open file my-pdf-file.blade.php and write this code into it.

<!DOCTYPE html>
    <title>Title From OnlineWebTutorBlog</title>
    <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>

Add Route

Open web.php from /routes folder. Add this route into it.


use App\Http\Controllers\SiteController;

Route::get('generate-pdf', [SiteController::class, 'generatePDF']);


Application Testing

Run this command into project terminal to start development server,

php artisan serve


When we type this URL, it will download a pdf file into system.

We hope this article helped you to learn about How To Use DomPDF To Generate PDFs in Laravel 10 Tutorial Example in a very detailed way.

Read More: Laravel 10 How To Send Email To Multiple Users Tutorial

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

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.