How to generate pdf file in Laravel 5 using laravel-dompdf package

By | June 5, 2018
how to generate pdf file in laravel

Hello friends, Do you need to generate pdf file in you laravel application or you want to learn how to generate pdf file in laravel 5 using laravel-dompdf package. Then you are at right place. In this article i will let you know about pdf file generation in laravel application.

Here we will use laravel-dompdf package.  This package provides simple methods to create pdf file and let’s see how simple to integrate in our laravel application. Follow the steps below-

Step: 1 Install laravel-dompdf package

In the first step we will download the package through composer command. So open your terminal/command prompt aand execute the below command.

composer require barryvdh/laravel-dompdf

After successfully running the above command. We will need to add some configurations.

Open config/app.php file and add below lines in providers and alias section.

config/app.php

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

Step: 2 Create a route to download the pdf file

Add a route in routes/web.php be using the following code.

/ /web.php

Route::get('/downloadPDF/{id}','UserDetailController@downloadPDF');

Step: 3 Create a controller function to download the PDF.

In the third step we will need to create a controller file to perform the action regarding to generate pdf. I am assuming you have already a model UserDetail and there are some data into the table respective this model. And want you display user’s data in the PDF.

So let’s create UserDetailController.php file in laravel’s controller directory and copy the below code inside the controller file.

// UserDetailController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use PDF;
use App\UserDetail;

class UserDetailController extends Controller
{

    public function store(Request $request){

      $user = new UserDetail([
        'full_name' => $request->get('full_name'),
        'street_address' => $request->get('street_address'),
        'city' => $request->get('city'),
        'zip_code' => $request->get('zip_code')
      ]);

      $user->save();
      return redirect('/index');
    }
    public function index(){

      $users = UserDetail::all();

      return view('index', compact('users'));
    }

    public function downloadPDF($id){
      $user = UserDetail::find($id);

      $pdf = PDF::loadView('pdf', compact('user'));
      return $pdf->download('invoice.pdf');

    }
}

Here one point to be noted in the above code i am using PDF::loadView method and passing pdf as a first parameter which is actually a view blade file So let’s create that file in the next step.

Step: 4 Create pdf.blade.php file to view our pdf

In the fourth step we will create a view names pdf.blade.php, here we will design how pdf will look like

<!-- pdf.blade.php -->

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <table class="table table-bordered">
      <tr>
        <td>
          {{$user->full_name}}
        </td>
        <td>
          {{$user->street_address}}
        </td>
      </tr>
      <tr>
        <td>
          {{$user->city}}
        </td>
        <td>
          {{$user->zip_code}}
        </td>
      </tr>
    </table>
  </body>
</html>

After completing these steps you have implement laravel-dompdf package. Now open the created route in the browser and you will see pdf file has generated and downloaded in your computer.

You can get more information about this package from here: barryvdh/laravel-dompdf

If you have any query or getting any error. You can comment here. I will definitely help you.