LARAVEL : EXPORT DATA TO PDF (DOMPDF)
Tags: laravel, tutorial, php
Published: 2022-07-08 13:44:19

Video link:
https://youtu.be/8eMBg6pBrVs

## HELLO FELLOW DEVELOPER!

**How are you?**


In laravel you can easily export your data in pdf format with the help of this useful package that is called dompdf. In this tutorial I have given you an example by exporting users data from database to pdf file.

Package link:
https://github.com/barryvdh/laravel-dompdf

Well we will cover this up step by step so let's follow this article or video that I have given above.

**STEP # 01:**
Install and configure the dompdf package first..

```
composer require barryvdh/laravel-dompdf
```

    

```
php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"
```

**STEP #02:**
Add this function in to your controller to view pdf.
```
public function viewPDF()
    {
        $users = User::all();

        $pdf = PDF::loadView('pdf.usersdetails', array('users' =>  $users))
        ->setPaper('a4', 'portrait');

        return $pdf->stream();

    }
```
Add this funtion into your controller to download pdf...
```
public function downloadPDF()
    {
        $users = User::all();

        $pdf = PDF::loadView('pdf.usersdetails', array('users' =>  $users))
        ->setPaper('a4', 'portrait');

        return $pdf->download('users-details.pdf');   
    }
```

Make route of this function:

```
Route::post('users/view-pdf', [HomeController::class, 'viewPDF'])->name('view-pdf');
Route::post('users/download-pdf', [HomeController::class, 'downloadPDF'])->name('download-pdf');
```

Well you can use any html template to export your data but let me share with you that I have used in my video.

```
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>User Details</title>

    <style>
        table {
            width: 95%;
            border-collapse: collapse;
            margin: 50px auto;
        }

        /* Zebra striping */
        tr:nth-of-type(odd) {
            background: #eee;
        }

        th {
            background: #3498db;
            color: white;
            font-weight: bold;
        }

        td,
        th {
            padding: 10px;
            border: 1px solid #ccc;
            text-align: left;
            font-size: 18px;
        }

     
    </style>

</head>

<body>

    <div style="width: 95%; margin: 0 auto;">
        <div style="width: 10%; float:left; margin-right: 20px;">
            <img src="{{ public_path('assets/images/logo.png') }}" width="100%"  alt="">
        </div>
        <div style="width: 50%; float: left;">
            <h1>All User Details</h1>
        </div>
    </div>

    <table style="position: relative; top: 50px;">
        <thead>
            <tr>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Email</th>
                <th>Date Of Joining</th>
            </tr>
        </thead>
        <tbody>
            @foreach ($users as $user)
                <tr>
                    <td data-column="First Name">{{ $user->first_name }}</td>
                    <td data-column="Last Name">{{ $user->last_name }}</td>
                    <td data-column="Email" style="color: dodgerblue;">
                        {{ $user->email }}
                    </td>
                    <td data-column="Date">
                        {{ date('F j, Y', strtotime($user->create_at)) }}
                    </td>
                </tr>
            @endforeach
        </tbody>
    </table>

</body>

</html>
```
 
If you have any queries or issue pin your comments below or watch the video!

https://youtu.be/8eMBg6pBrVs

THANKS!!!