How to Download PDF file using Laravel 8 DomPDF

AuthorHariom Prajapati

Pubish Date26 Jun 2022

categoryLaravel

In this tutorial we will learn how to download PDF file using Laravel 8 DomPDF.

 

How to download pdf file using Laravel 8 dompdf

 

How to download PDF file using Laravel 8 DomPDF

Normally, when we work on large project on laravel then we require to generate PDF file of database table. So, In this tutorial we will learn about Laravel pdf package ( laravel dompdf ) using which we can download pdf file .

Here, we use laravel dompdf for create pdf and this package also provide the function of download. 

Now follow all the below steps to download pdf using laravel 8 .

 

Step 1 – In first step , Install dompdf package 

Run this command in your cmd terminal –

composer require barryvdh/laravel-dompdf

how to download pdf using laravel 8 dompdf

 

Step 2 - In second step open config/app.php file and update package service provider and alias.

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

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

    ] 

 

 

Step 3 – Create Controller

php artisan make:controller PdfController

 

app\Http\Controllers\PdfController.php

<?php
namespace App\Http\Controllers;
Use PDF;
use Illuminate\Http\Request;

class PdfController extends Controller
{
    public function downloadPDF(){
        $data = [
           "full_name"=> "john do",
           "email"=> "john@example.com",
           "age"=>"23",
           "phone"=>"998877XXXX",
        ];
        $pdf = PDF::loadView('Sample_pdf',$data);
        return $pdf->download('teknowize.pdf');
    }
}  

 

Step 4- Create Route

use App\Http\Controllers\PdfController;
use Illuminate\Support\Facades\Route; 

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
*/

Route::get('download-pdf', [PdfController::class, 'downloadPDF']); 

 

Step 5 –  Create pdf view template 

In this step , Create Sample_pdf.blade.php in resources\views .

resources\views\Sample_pdf.blade.php

<!DOCTYPE html>
<html>

<head>
    <title>laravel-pdf</title>
    <style>
        table tr th {
            background-color: #04AA6D;
            color: white;
            border: 1px solid #ddd;
            padding: 8px;
        }

        table tr td {
            border: 1px solid #ddd;
            padding: 8px;
        }
    </style>
</head>

<body>
    <h3>Laravel 8 PDF Example Using dompdf </h3>
    <table>
        <tr>
            <th>Full Name</th>
            <th>Email Id</th>
            <th>Age</th>
            <th>Phone Number</th>
        </tr>
        <tr>
            <td>{{ $full_name }}</td>
            <td>{{ $email }}</td>
            <td>{{ $age }}</td>
            <td>{{ $phone }}</td>
        </tr>
    </table>
</body>

</html>

 

Step 6 – Run php artisan serve

php artisan serve

laravel

Finally , run this url in your browser

http://127.0.0.1:8000/download-pdf

Comments 0

Leave a comment