Blog · Laravel

Laravel 6 export to PDF using laravel-dompdf

Install barryvdh/laravel-dompdf, configure the provider, and generate downloadable PDFs from Blade views.

Laravel 6DomPDFBlade

In this tutorial, we will learn how to install and export PDFs in your PHP Laravel project. We will use the barryvdh/laravel-dompdf package. If you want to install a fresh Laravel project you can use the guide below.

Install a new Laravel project

The official documentation will help you install a supported version of Laravel for your environment.

1. Download and install barryvdh/laravel-dompdf package

Enter the following command to install the Laravel DomPDF package.

composer require barryvdh/laravel-dompdf

The above command will install the DomPDF package. Next, we will configure the Laravel project.

2. Configure barryvdh/laravel-dompdf package in Laravel project

Update the barryvdh/laravel-dompdf service provider and facades in config/app.php. Copy the following into your app.php file:

# config/app.php

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

3. Insert data to the table

For this tutorial, we use the table below to load data and export it to PDF. If you want to build CRUD first to test DomPDF, you can follow a step-by-step guide:

Laravel CRUD tutorial (Eloquent basics in the Laravel documentation).

We will use the posts table. Sample structure:

mysql> desc posts;
+-------------+---------------------+------+-----+---------+----------------+
| Field       | Type                | Null | Key | Default | Extra          |
+-------------+---------------------+------+-----+---------+----------------+
| id          | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
| name        | varchar(255)        | NO   |     | NULL    |                |
| description | text                | NO   |     | NULL    |                |
| created_at  | timestamp           | YES  |     | NULL    |                |
| updated_at  | timestamp           | YES  |     | NULL    |                |
+-------------+---------------------+------+-----+---------+----------------+

4. Create a model

Create a model for the posts table:

php artisan make:model Post

5. Update web.php

Copy and paste the following into routes/web.php:

# routes/web.php

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

Route::get('download-all-post', 'PdfController@downloadPost');

The first route returns the welcome view (home). The second route triggers PDF generation for all posts.

6. Create a controller

Create the controller:

php artisan make:controller PdfController

Add index and downloadPost methods. Then create the welcome view with a download link.

# app/Http/Controllers/PdfController.php

<?php

namespace App\Http\Controllers;

use App\Post;

class PdfController extends Controller
{
    public function index()
    {
        return view('welcome');
    }

    public function downloadPost()
    {
        $posts = Post::all();
        $pdf = \PDF::loadView('pdf', compact('posts'));

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

Create resources/views/welcome.blade.php with a link to the download route:

# resources/views/welcome.blade.php

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>Laravel</title>

        <!-- Fonts -->
        <link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet">
    </head>
    <body>
        <a href="/download-all-post" class="btn btn-primary">Download all posts</a>
    </body>
</html>

7. Create a Blade file to design PDF layout

Create a Blade view (e.g. resources/views/pdf.blade.php) that lists posts. The downloadPost method loads this view and DomPDF converts the HTML table to PDF.

# resources/views/pdf.blade.php

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
</head>

<body>
    <table>
        <thead>
            <tr>
                <td><b>Name</b></td>
                <td><b>Description</b></td>
                <td><b>Created At</b></td>
            </tr>
        </thead>
        <tbody>
            @foreach($posts as $post)
            <tr>
                <td>
                    {{$post->name}}
                </td>
                <td>
                    {{$post->description}}
                </td>
                <td>
                    {{$post->created_at->diffForHumans()}}
                </td>
            </tr>
            @endforeach
        </tbody>
    </table>
</body>

</html>