Laravel generate PDF using dompdf

version : Laravel 10
Post Time : 09-06-2024

Laravel generate PDF using dompdf

install laravel
         - composer create-project --prefer-dist laravel/laravel:^10.0 laravel_demo

Firstly, install the barryvdh/laravel-dompdf package using Composer :
           - composer require barryvdh/laravel-dompdf

Next, publish the package configuration file. This step is optional but allows you to customize the settings
           - php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"

Create DemoPDFController Controller  :
           - php artisan make:controller DemoPDFController

app/Http/Controllers/DemoPDFController.php

                                      
                                        

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Barryvdh\DomPDF\Facade\Pdf;
use App\Models\User;

class DemoPDFController extends Controller
{
   public function CreatePDFFile()
   {
       $data['title'] = 'User Data';
       $data['users'] = User::where('status', 1)->get()->toArray();

       $pdf = Pdf::loadView('demo_pdf_template', $data);

       //for Display PDF stream
       return $pdf->stream('document.pdf');

       // for Download PDF file
       // return $pdf->download('documentname.pdf');
   }
}

Create demo_pdf_template blade file :-
Resources/views/demo_pdf_template.blade.php

                                      
                                        

<!DOCTYPE html>
<html>
<head>
   <title>PDF Document</title>
</head>
<body>
   <h1>{{ $title }}</h1>
   <table>
       <thead>
           <tr>
               <th>No</th>
               <th>Name</th>
               <th>Email</th>
               <th>Phone No</th>
               <th>Address</th>
           </tr>
       </thead>
       <tbody>
       @if($users)
           @php $no = 1; @endphp
           @foreach($users as $user)
           <tr>
               <td>{{ $no++ }}</td>
               <td>{{ $user['name'] }}</td>
               <td>{{ $user['email'] }}</td>
               <td>{{ $user['phone_no'] }}</td>
               <td>{{ $user['address'] }}</td>
           </tr>
           @endforeach
       @else 
           <tr>
               <td colspan="100%">No Data</td>
           </tr>
       @endif
       </tbody>
   </table>
</body>
</html>

routes/web.php

                                      
                                        

<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\DemoPDFController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/
Route::get('/', function () {
   return view('welcome');
});

Route::get('/create-pdf-file', [DemoPDFController::class, 'CreatePDFFile'])->name('CreatePDFFile');

Run below url on localhost
http://127.0.0.1:8000/create-pdf-file