How To Generate PDF Using DomPDF With Laravel 5.8 Tutorial

admin_img Posted By Bajarangi soft , Posted On 12-01-2021

how to download pdf using dompdf in laravel 5.8

How-to-generate-pdf-using-laravel

Step 1 : Install Laravel using the following command.

composer create-project --prefer-dist laravel/laravel laravelPDF

 

Step: 2 Download the laravel-dompdf package

composer require barryvdh/laravel-dompdf


Step 3 : So go to a config >> app.php and add the following configuration.

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


Step 4 : Run the command in terminal to make auth

 php artisan make:auth


Step 5 : After users registration , create a controller as PDFController

php artisan make:controller PDFController

 

Step: 6 Create a view file for display the data.
Before we create a view file, we need to add one route inside the web.php.

Route::get('users','PDFController@index');

Now, create a view file called users.blade.php file. Add the following code.
@extends('layouts.app')
@section('content')
    <div class="col-md-12 table_center">
        <div class="col-md-10">
            <table class="table table-striped">
                <thead>
                <th>ID</th>
                <th>Name</th>
                <th>Email</th>
                <th>Action</th>
                </thead>
                <tbody>
                @foreach($users as $user)
                    <tr>
                        <td>{{$user->id}}</td>
                        <td>{{$user->name}}</td>
                        <td>{{$user->email}}</td>
                        <td><a class="btn btn-success" href="{{url('downloadPDF/'.$user->id)}}">Download</a></td>
                    </tr>
                @endforeach
                </tbody>
            </table>
        </div>
    </div>
@endsection
Now, add the code inside the index() function of PDFController.php file.
public function index()
{
    $users = User::get();
    return view('users', compact('users'));
}

Step: 7 Create a route to download the pdf file
Add the following code inside the route file.

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

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

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
</head>
<body>
<table class="table table-bordered">
    <thead>
    <tr>
        <td><b>ID</b></td>
        <td><b>Name</b></td>
        <td><b>Email</b></td>
    </tr>
    </thead>
    <tbody>
        <tr>
            <td>{{$user->id}}</td>
            <td>{{$user->name}}</td>
            <td>{{$user->email}}</td>
        </tr>
    </tbody>
</table>
</body>
</html>

Step: 9 Write a controller function to download the PDF
Write the following code inside the PDFController.php file.

use App\User;
use Illuminate\Http\Request;
use PDF;
public function downloadPDF($id) {
    $user = User::find($id);
    $pdf = PDF::loadView('pdf', compact('user'));

    return $pdf->download('disney.pdf');
}

 



Note : if you are facing this error while downloading pdf
[PHP 7.4 RC3] Invalid characters passed for attempted conversion, these have been ignored

Inside vendor/dompdf/dompdf/lib/Cpdf.php
Solution : Quick fix. Line 2543, add :
                if (ctype_xdigit($c) && ctype_xdigit($n)) { ...
Then close after following if statement.

Related Post