Friday, 15 June 2018

Laravel - How to Generate Dynamic PDF from HTML using DomPDF

Making of Dynamic PDF is required feature of any commercial web application, so here we have publish this post in which we have describe how to create dynamic pdf file in Laravel by using DomPDF library. Suppose we have working on any enterprise level web application by using Laravel then at that time we want to required to make output of our application in PDF report format from dynamic Mysql database. From this post we have make simple tutorial in which we have describe how to can we generate dynamic PDF file in step by step process which better understading to beginner laravel programmer.

In this tutorial we have use laravel-dompdf package for generate dynamic pdf file and by using this package we can also download pdf file in our local computer also. Here we have use mysql customer table, in which we have already inserted some data for create dynamic pdf file from that data in tabular format. For display data in proper in table in pdf file we have also add some inline css in html table code, because we have make pdf file from html by using dompdf package in Laravel.

Step 1 - Download Laravel Dompdf Package

For create PDF file in Laravel first we want to download laravel-dompdf package. For this first we want to write following command in command prompt terminal.

composer require barryvdh/laravel-dompdf

Download this packege we have to register this package into Laravel working environment, for this we have to open config/app.php file and add service provider and aliase details.

'providers' => [

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

Step 2 - Create Controller

After this we have to create controller in our Laravel application by using composer command. This controller will handle http requrest to convert html code to PDF file.

php artisan make:controller DynamicPDFController

This command will make controller in app/Http/Controllers folder with name like DynamicPDFController.php. In this controller first we have to make index() method for fetch data from database and here for fetch data we have to get_customer_data() method and after this in index() method we have send data to view file. For convert HTML data to PDF we have make another method pdf() by using this method it will convert html code to PDF file. For html data here we have use convert_customer_data_to_html() method. Below you can find complete controller source code below.


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use PDF;

class DynamicPDFController extends Controller
    function index()
     $customer_data = $this->get_customer_data();
     return view('dynamic_pdf')->with('customer_data', $customer_data);

    function get_customer_data()
     $customer_data = DB::table('tbl_customer')
     return $customer_data;

    function pdf()
     $pdf = \App::make('dompdf.wrapper');
     return $pdf->stream();

    function convert_customer_data_to_html()
     $customer_data = $this->get_customer_data();
     $output = '
     <h3 align="center">Customer Data</h3>
     <table width="100%" style="border-collapse: collapse; border: 0px;">
    <th style="border: 1px solid; padding:12px;" width="20%">Name</th>
    <th style="border: 1px solid; padding:12px;" width="30%">Address</th>
    <th style="border: 1px solid; padding:12px;" width="15%">City</th>
    <th style="border: 1px solid; padding:12px;" width="15%">Postal Code</th>
    <th style="border: 1px solid; padding:12px;" width="20%">Country</th>
     foreach($customer_data as $customer)
      $output .= '
       <td style="border: 1px solid; padding:12px;">'.$customer->CustomerName.'</td>
       <td style="border: 1px solid; padding:12px;">'.$customer->Address.'</td>
       <td style="border: 1px solid; padding:12px;">'.$customer->City.'</td>
       <td style="border: 1px solid; padding:12px;">'.$customer->PostalCode.'</td>
       <td style="border: 1px solid; padding:12px;">'.$customer->Country.'</td>
     $output .= '</table>';
     return $output;

Step 3 - Create view file

After this we have make view file in resources/view/dynamic_pdf.blade.php. This file has received data from controller index() method and display dynamic data in html table format. After this we have make on button for convert this html data to pdf file. When we have click on this button then it has send request to pdf() method of controller which will convert html to PDF file by using dompdf package in laravel which we can see on web page.

<!DOCTYPE html>
  <title>Laravel - How to Generate Dynamic PDF from HTML using DomPDF</title>
  <script src=""></script>
  <link rel="stylesheet" href="" />
  <script src=""></script>
  <style type="text/css">
    margin:0 auto;
  <br />
  <div class="container">
   <h3 align="center">Laravel - How to Generate Dynamic PDF from HTML using DomPDF</h3><br />
   <div class="row">
    <div class="col-md-7" align="right">
     <h4>Customer Data</h4>
    <div class="col-md-5" align="right">
     <a href="{{ url('dynamic_pdf/pdf') }}" class="btn btn-danger">Convert into PDF</a>
   <br />
   <div class="table-responsive">
    <table class="table table-striped table-bordered">
       <th>Postal Code</th>
     @foreach($customer_data as $customer)
       <td>{{ $customer->CustomerName }}</td>
       <td>{{ $customer->Address }}</td>
       <td>{{ $customer->City }}</td>
       <td>{{ $customer->PostalCode }}</td>
       <td>{{ $customer->Country }}</td>

Step 4 - Add Route

This is last step for generate dynamic pdf file in laravel by using dompdf package and here we want to set route for index() and pdf() method of DynamicPDFController.php file. For this we have to go to app/Http/routes.php and write following code for set route in our laravel application.

Route::get('/dynamic_pdf', 'DynamicPDFController@index');

Route::get('/dynamic_pdf/pdf', 'DynamicPDFController@pdf');

So, this is complete step by step process for create PDF file in Laravel by using Dompdf package.