AppDividend
Latest Code Tutorials

How to Generate PDF in Laravel 5

31,522

Get real time updates directly on you device, subscribe now.

Greetings, Web Developers,  In today’s AppDividend Tutorial, I have shown the code of How to generate pdf in laravel 5

If you want to know the basic laravel functionality then check out my other article Laravel 5.4 Crud Example From Scratch

Generate pdf in laravel 5

First, we will install the laravel-dompdf and then we will start our example.

Step: 1 Download the laravel-dompdf package through composer.

composer require barryvdh/laravel-dompdf

Step: 2 We need to configure this package in our laravel application.

So go to the config >> app.php and add the following configuration.

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

Here we are registering the PDF register provider for our application and also set an alias for it. So when we need to generate PDF, we just need to include it in our namespace like,

use PDF;

Then, we instantiate the PDF class and use its API for further enhancing the generated PDF file.

Step: 3 Create a master blade file.

<!-- master.blade.php -->

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="css/app.css">
  </head>
  <body>
      <div class="container">
          @yield('content')
      </div>
  </body>
</html>

Step: 4 Create a form blade file for input the data.

<!-- form.blade.php -->

@extends('master')
@section('content')
<form method="post" action="{{url('submitForm')}}">
    {{csrf_field()}}
    <div class="form-group"> <!-- Full Name -->
      <label for="full_name_id" class="control-label">Full Name</label>
      <input type="text" class="form-control" id="full_name_id" name="full_name" placeholder="John Deer">
    </div>

    <div class="form-group"> <!-- Street 1 -->
      <label for="street1_id" class="control-label">Street Address 1</label>
      <input type="text" class="form-control" id="street1_id" name="street_address" placeholder="Street address, P.O. box, company name, c/o">
    </div>

    <div class="form-group"> <!-- City-->
      <label for="city_id" class="control-label">City</label>
      <input type="text" class="form-control" id="city_id" name="city" placeholder="Smallville">
    </div>

    <div class="form-group"> <!-- Zip Code-->
      <label for="zip_id" class="control-label">Zip Code</label>
      <input type="text" class="form-control" id="zip_id" name="zip_code" placeholder="#####">
    </div>

    <div class="form-group"> <!-- Submit Button -->
      <button type="submit" class="btn btn-primary">Buy!</button>
    </div>

    </form>
@endsection

Step: 5 Add the routes for our application.

// web.php

<?php

Route::get('/', function () {
    return view('form');
});

Route::post('submitForm','UserDetailController@store');

Step: 6 Create the model file for form data.

// UserDetail.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class UserDetail extends Model
{
    protected $fillable = ['full_name','street_address','zip_code','city'];
}

Step: 7 Create controller file for form data.

// UserDetailController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
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'));
    }
}

Step: 8 Update web.php file for display the listing page.

// web.php

Route::get('/index','UserDetailController@index');

Step: 9 Create a view file for display the data.

<!-- index.blade.php -->

@extends('master')
@section('content')
<table class="table table-striped">
  <thead>
    <th>ID</th>
    <th>Full Name</th>
    <th>Address</th>
    <th>City</th>
    <th>Zip Code</th>
    <th>Action</th>
  </thead>
  <tbody>
    @foreach($users as $user)
    <tr>
      <td>{{$user->id}}</td>
      <td>{{$user->full_name}}</td>
      <td>{{$user->street_address}}</td>
      <td>{{$user->city}}</td>
      <td>{{$user->zip_code}}</td>
    </tr>
    @endforeach
  </tbody>
</table>
@endsection

Step: 10 Create a route for download the pdf file.

/ /web.php

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

Step: 11 Update index.blade.php file.

<!-- index.blade.php -->

@extends('master')
@section('content')
<table class="table table-striped">
  <thead>
    <th>ID</th>
    <th>Full Name</th>
    <th>Address</th>
    <th>City</th>
    <th>Zip Code</th>
    <th>Action</th>
  </thead>
  <tbody>
    @foreach($users as $user)
    <tr>
      <td>{{$user->id}}</td>
      <td>{{$user->full_name}}</td>
      <td>{{$user->street_address}}</td>
      <td>{{$user->city}}</td>
      <td>{{$user->zip_code}}</td>
      <td><a href="{{action('UserDetailController@downloadPDF', $user->id)}}">PDF</a></td>
    </tr>
    @endforeach
  </tbody>
</table>
@endsection

Step: 12 Create pdf.blade.php file to design our pdf blade.

<!-- 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>

Step: 13 Write a controller function to download the PDF.

// 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');

    }
}

This was the simple example of laravel pdf generation. Finally, our tutorial on How to Generate PDF in Laravel 5 is over.

15 Comments
  1. stacy says

    Yes this is easy and helpful for the beginner.

    1. Krunal says

      Thanks

  2. Willy says

    Ohh man, this is an extremely nice post.

  3. Robin says

    Very deep knowledge about how you can generate pdf. Keep posting.

  4. harish patel says

    Grate works 🙂 it’s really helpfull

  5. Gaby says

    THANK YOU SO MUCH
    I’ve been trying to figure it out for a while showing only 1 register in the file and it finally worked!

  6. Muhammad says

    Thank you so much.

    I have been searching for this for a while

  7. mithilesh says

    good post , assume if i want to use multi language pdf like thai, chienese, english,japanies etc. in pdf
    What i need to do for it ?

  8. mrkhd says

    how i can run a non-static function (loadview) , statically ?

  9. Niyaz says

    Thanks lot.

  10. Vepi says

    I have been trying for one week (I am a laravel newbie). I got the answer from just this article!!!!!! Brilliant and thanks a lot.

  11. Vivi says

    Thanks.. It’s really help me 😊

  12. nurul says

    hi, can u teah how to put backgroung image in this pdf?

  13. loveleen says

    this is working well with english language but how do i work with tamil and sinhala language ?

  14. diadji says

    thanks man

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.