Laravel PDF Generator | HTML to PDF Using Laravel Dompdf

In this article, we will discuss laravel 7 pdf generator. It will also work for laravel 5. I will show you an example, that you can generate a pdf file for your laravel application. 

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. After that, we will export this view as a PDF file.

In this tutorial i will use barryvdh/laravel-dompdf packages to create html to pdf in laravel 7 application. You can use other packages. But i will use barryvdh/laravel-dompdf. Cause it is very easy to use.

So now let's start our tutorial that how we can generate a PDF file from html. You can see more about this package from dompdf

export-to-pdf-using-laravel-dom-pdf

Step 1: Setup laravel-dompdf package

Firstly, we need to install the “barryvdh/laravel-dompdf” package. It’s easy to install using given composer command:

composer require barryvdh/laravel-dompdf

Step 2: Configure package

After executing the above command, time to update Service Provider to the providers and facade to the aliases array in config/app.php.

config/app.php

'providers' => [

  Barryvdh\DomPDF\ServiceProvider::class,

],

'aliases' => [

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

]

Let publish the assets from the vendor using the given artisan command.

php artisan vendor:publish

 

After executing the above command, list of available packages shown. You can select as per your need. Now, we are selecting the “Provider: Barryvdh\DomPDF\ServiceProvider” and the new file is copied to the config/dompdf.php. This file contains the global settings for the dompdf package.

Step 3: Setup blade view template for PDF

We are creating the simple HTML table for this example. You can update this HTML as per your requirement.

 

In this, we are creating just HTML template where just generate a table. This table contains the customer details such as customer name, email, phone and etc. You can change the view as per your PDF required.

Step 4: Setup blade template 

In this view, we create a view which displays our customer list with the export button. Export button is used to trigger the export pdf functionality.

Step 5: Setup Model 

app/Customer.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Customer extends Model
{
    public $table = 'customers';

    protected $fillable = [

        'name',
        'email',
        'mobileno'

    ];
}

Step 6: Register routes 

Register the route for export pdf. When an URL customer/pdf hits the report generate and store on the server then download start. You can control the storage path as per your need.

Route::get('/customers/pdf','[email protected]_pdf')->name('customer.pdf');

 Step 7: Setup Controller

Here is our logic to generate a PDF file in Laravel using DOM PDF package.

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class CustomerController extends Controller
{
  public function export_pdf()
  {
    // Fetch all customers from database

    $data = Customer::get();

    // Send data to the view using loadView function of PDF facade

    $pdf = \PDF::loadView('pdf.customers', $data);

    // If you want to store the generated pdf to the server then you can use the store function

    $pdf->save(storage_path().'_filename.pdf');

    // Finally, you can download the file using download function

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

  }
}

Read also : Import and Export CSV file in Laravel

Now all are done. Now you can check it in your server. Now when we click on the export pdf button then download start. Feel free to comment if any query.  

A web enthusiastic, self-motivated Full-Stack Web Developer from Dhaka, Bangladesh with experience in developing applications using JavaScript, Laravel & Wordpress specifically. Facebook Github Website