Laravel 6 export / generate to pdf using laravel-dom

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

Install a new Laravel project

The above tutorial will guide/help you to install the latest version of Laravel.

1. Download and install barryvdh/laravel-dompdf package

Enter the following command to install Laravel dompdf package.

composer require barryvdh/laravel-dompdf

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

2. Configure barryvdh/laravel-dompdf Package in Laravel Project

Before we get started, we need to update barryvdh/laravel-dompdf service provider and facades in config/app.php file. Copy the following code and paste it in your app.php file.

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

3. Insert data to the table

For this tutorial, I will use the below table to get all data and export to pdf. If you want to create crud operation to test export dompdf you can follow step by step tutorial to generate the CRUD operation.

Laravel CRUD tutorial

I will use the post table to export pdf data. Below is the sample table structure of the post table

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

Next, we will create the model for post table. To do so, enter the following command to create model file.

php artisan make:model Post

5. Update web.php

Copy and paste the below code in your route/web.php file

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

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

The first route will navigate to the welcome blade file where will place the download button. The second button will download/generate post's  PDF.

6. Create a controller

Enter the following command to create controller.

php artisan make:controller PdfController

The above command will generate PdfController. Now let's create a function and view file to place the pdf download button.

#app/Http/Controllers/PdfController.php

public function index()
{
    return view('welcome');
}

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

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

Now create a welcome blade file and add the below code.

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

We will now create a blade file to get all the posts. downloadPost Function that we declared before will convert html table to PDF using dompdf

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