Generate HTML to PDF in Laravel 7 Using domPdf

Generate HTML to PDF in Laravel 7 is very easy using the domPdf library. Mostly in laravel admin projects needs to generate pdf file from html view. Here in this article we have already explain easy way How to Generate HTML to PDF file in Laravel. Using dompdf package you can generate your PDF with dynamic data and in very nice formatting.

Using the berryvdh/dompf we will create pdf from a view, in this view, we will write the HTML code and load data dynamically from the database as per the requirement and we can export the view as a pdf file easily. So lets start for Laravel 7 Create HTML view to PDF file step by step tutorial example for you.

Step 1. Installing Package

install barryvdh/laravel-dompdf composer package by following command in your laravel 7 project.

sudo composer require barryvdh/laravel-dompdf

Step 2. Update config/app.php

Now open config/app.php file.

Add the following line to ‘providers’ array:

'providers' => [
 ....
 Barryvdh\DomPDF\ServiceProvider::class,
],

Now add this line to ‘aliases’:

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

Step 3. Add routes

Now open your web.php file and add following route.

Route::get('generate-pdf','PDFController@generatePDF');

Step 4. Add Link In Blade File

<a href="/generate-pdf">Generate PDF</a>

Step 5. Create Controller

Add your controller below code.

<?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 generatePDF()
    {
        $data = [
          'title' => 'First PDF for Coding Driver',
          'heading' => 'Hello from Coding Driver',
          'content' => 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.'        
            ];
        
        $pdf = PDF::loadView('generate_pdf', $data);
  
        return $pdf->download('codingdriver.pdf');
    }
}

Step 6. Create Blade File

Let’s create generate_pdf.blade.php for layout of pdf file and put following code:

<!DOCTYPE html>
<html>
<head>
    <title>Generate Pdf</title>
</head>
<body>
  <h1>{{ $heading}}</h1>
  <div>
     <p>{{$content}}</p>
  </div>
</body>
</html>

See Also: Download pdf file using Jspdf and html2canvas

So friends, Generate HTML to PDF in Laravel 7 Using domPdf tutorial is completed now, If you have any question please comment below.

3 thoughts on “Generate HTML to PDF in Laravel 7 Using domPdf”

  1. Works a treat. Thanks a million. Had to remember to import the class into the controller. Other than that, worked first time

    Reply

Leave a Comment