Laravel - generate PDF from html view file and download using dompdf

Laravel - generate PDF from html view file and download using dompdf

Laravel - Generate PDF from HTML View File and Download Using DomPDF

In Laravel, you can generate and download PDFs using the Dompdf package. Below is a step-by-step guide to implement it.

1. Install DomPDF Package

Run the following command to install barryvdh/laravel-dompdf:

composer require barryvdh/laravel-dompdf

Then, add the service provider in config/app.php (for Laravel 6):

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

Publish the package configuration (optional):

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

2. Create a Controller

Run the following command to generate a new controller:

php artisan make:controller PDFController

Modify app/Http/Controllers/PDFController.php:

namespace App\Http\Controllers; use Illuminate\Http\Request; use Barryvdh\DomPDF\Facade as PDF; class PDFController extends Controller { public function generatePDF() { $data = [ 'title' => 'Laravel PDF Example', 'date' => date('m/d/Y'), 'users' => [ ['name' => 'John Doe', 'email' => 'john@example.com'], ['name' => 'Jane Doe', 'email' => 'jane@example.com'], ] ]; $pdf = PDF::loadView('pdf.document', $data); return $pdf->download('document.pdf'); } }

3. Create a Blade View for PDF Content

Create a new Blade file:
resources/views/pdf/document.blade.php

<!DOCTYPE html> <html> <head> <title>Laravel PDF</title> <style> body { font-family: Arial, sans-serif; } table { width: 100%; border-collapse: collapse; margin-top: 20px; } th, td { border: 1px solid black; padding: 8px; text-align: left; } th { background-color: #f2f2f2; } </style> </head> <body> <h2>{{ $title }}</h2> <p>Date: {{ $date }}</p> <table> <tr> <th>Name</th> <th>Email</th> </tr> @foreach($users as $user) <tr> <td>{{ $user['name'] }}</td> <td>{{ $user['email'] }}</td> </tr> @endforeach </table> </body> </html>

4. Define Route for PDF Generation

Modify routes/web.php:

use App\Http\Controllers\PDFController; Route::get('/generate-pdf', [PDFController::class, 'generatePDF']);

5. Run the Application and Generate a PDF

Start the Laravel development server:

php artisan serve

Now, visit the URL in your browser:

http://127.0.0.1:8000/generate-pdf

The PDF file document.pdf will be generated and downloaded automatically.

6. Additional Features

  • Show PDF in Browser Instead of Downloading

    return $pdf->stream('document.pdf');
  • Customize Paper Size and Orientation

    $pdf = PDF::loadView('pdf.document', $data)->setPaper('a4', 'landscape');
  • Use External CSS (Inline CSS is recommended, but you can include a stylesheet like this):

    $pdf->loadHTML('<link rel="stylesheet" href="style.css">'.view('pdf.document', $data)->render());

Conclusion

Dompdf allows you to generate PDFs easily.
Blade templates provide dynamic content rendering.
Custom styling & layouts enhance document presentation.

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close