Posted by Arjun on Monday 27th November 2017

Laravel - Generate PDF file from view

How to view Laravel 5.x generate Laravel 5.5 PDF file

We gonna use barryvdh/laravel-dompdf Laravel package to generate PDF files from view file. This package is just a wrapper around DOMPDF library.

[caption id="attachment_5683" align="aligncenter"]Generate PDF file from view [/caption]

Installtion

Use below composer command to install the package

composer require barryvdh/laravel-dompdf

After installing laravel-dompdf, add the ServiceProvider class to the provider's array in config/app.php

'providers' => [
    Barryvdh\DomPDF\ServiceProvider::class
 ];

You can optionally use the facade for shorter code. So when you need to generate PDF you just need to include it with use PDF, you dont have to metion full namesapce. Add this to your facades array in config/app.php

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

Great! installation and configuration part is completed. Now let use this package.

Generate a controller called UserController.php with below command

$ php artisan make:controller UserController

After generating controller add following index and report methods to it. Index is to show the list of users and report is to generate PDF and to download the PDF.

<?php

namespace App\Http\Controllers;

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

class UserController extends Controller
{

    public function index(Request $request) {
        $users = User::all();
        return view('users.index',['users' => $users]);
    }
    
    public function report(Request $request)
    {
         $users = User::all();
         
        if($request->view_type === 'download') {
            $pdf = PDF::loadView('users.report', ['users' => $users]);
            return $pdf->download('users.pdf');
        } else {
            $view = View('users.report', ['users' => $users]);
            $pdf = \App::make('dompdf.wrapper');
            $pdf->loadHTML($view->render());
            return $pdf->stream();
        }

    }
}

Create a users directory with index.blade.php and report.balde.php files.

index.blade.php

<!doctype html>
<html lang="{{ app()->getLocale() }}">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Laravel</title>
</head>
    <body>
        <div class="row">
            <a href="{{ url('users/report/download') }}">Download PDF</a>
            <a href="{{ url('users/report/view') }}">view PDF</a>
            <table>
                <tr>
                    <th>Name</th>
                    <th>Email</th>
                </tr>
                @foreach ($users as $user)
                <tr>
                    <td>{{ $user->name }}</td>
                    <td>{{ $user->email }}</td>
                </tr>
                @endforeach
            </table>
        </div>
    </body>
</html>

report.blade.php

<!doctype html>
<html lang="{{ app()->getLocale() }}">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>Laravel</title>
    </head>
    <body>
        <div class="row">
            <table>
                <tr>
                    <th>Name</th>
                    <th>Email</th>
                </tr>
                @foreach ($users as $user)
                <tr>
                    <td>{{ $user->name }}</td>
                    <td>{{ $user->email }}</td>
                </tr>
                @endforeach
            </table>
        </div>
    </body>
</html>

Now define route to access controller methods index and report, add follwing lines to routes/web.php

Route::get('/users', '[email protected]');
Route::get('/users/report/{view_type}', '[email protected]');

That's it. Now, with http://localhost:8000/users/report/view, you can render pdf in the browser, with http://localhost:8000/users/report/download you can download the user's list pdf.