How to generate PDF in laravel using DomPDF

How to generate PDF in laravel using DomPDF

In this article let’s understand how to generate pdf in laravel using DomPDF.

If you don’t have n existing project create one using below command.

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

Install DomPDF library

Let’s install the domPDF library via composer using below command.

composer require barryvdh/laravel-dompdf

This will install the library and it’s dependencies.

In laravel 10 we don’t need to add alias and providers but for older versions add them in config/app.php as per below.

'providers' => [
'aliases' => [
  'PDF' => Barryvdh\DomPDF\Facade::class,

Create pdf :

Let’s create our controller to generate PDF.

php artisan make:controller PDFController

Now we will add the method in this controller which will serve our main functionality to create PDF. Checkout the code below.


namespace App\Http\Controllers;
use Illuminate\Http\Request;
use PDF;

class PDFController extends Controller
    Method to generate PDF

    public function generatePDF()
        $pdfData = [
            'title' => 'Coding Solutions Point',
            'subTitle' => "Let's solve problems together" ,
            'text' => "Welcome to our programming blog! We are passionate about all things related to programming, and we created this platform to share our knowledge, insights, and experiences with fellow developers, beginners, and enthusiasts.",
            'date' => date('m/d/Y')
        $pdf = PDF::loadView('generatePDF', $pdfData);
        return $pdf->download('csp.pdf');
  • We have added “use PDF” service here.
  • We created generatePDF method to create PDF with static data. You can load your dynamic data from eloquents here and create data as we have static here.
  • Use PDF class and it’s method to create and download.

Next, we will create one route to access above method. Add route as below, make sure you import your controller.


use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PDFController;

Route::get('createPdf', [PDFController::class, 'generatePDF']);

Let’s create generatePDF view file as we defined in our controller to create PDF at /resources/views/generatePDF.blade.php and add below HTML code to load html as PDF.

<!DOCTYPE html>
    <title>{{ $title }}</title>
    <h1>{{ $title }}</h1>
    <h4>{{ $subTitle }}</h4>
    <h6>Published on : {{ $date }}</h6>
    <p> {{ $text }} </p>

Run the laravel app and visit our created route as below :

php artisan serve

Visit the route :


This will download the file as below PDF.

This is the basic example of how to load html from view and create PDF. There are other functions as well provided by DomPDF which can be used as per need. Let’s see few common of them.

Load direct HTML as well

return $pdf->stream();

Load from plain html file as well

return Pdf::loadFile(public_path().'/csp.html')->save('/path-to/csp.pdf')->stream('csp.pdf');

Change orientation and paper size

Pdf::loadHTML($html)->setPaper('a4', 'landscape')->setWarnings(false)->save('csp.pdf')

That’s all about how to generate PDF in laravel using DomPDF. Hope this find you helpful.

Read more articles on Laravel here.