Laravel 8 PDF: How To Generate PDF With DomPDF

Laravel 8 PDF: How To Generate PDF With DomPDF

Laravel Dynamic PDF Generation with DomPDF

In a custom Laravel-based CRM project, one common requirement is to generate PDF files dynamically—for example, exporting lead data. After exploring some options, I found a great Laravel wrapper for DomPDF: barryvdh/laravel-dompdf.

📌 What is DomPDF?

Dompdf is an HTML to PDF converter. It's a CSS 2.1-compliant HTML layout and rendering engine written in PHP. Dompdf supports inline styles, external style sheets, and many presentational HTML attributes.

Laravel 8 PDF Example: Step-by-Step Guide

Step 1: Create a Laravel Project

composer create-project --prefer-dist laravel/laravel laravelPDF

Then, open the project folder in your code editor (e.g., VS Code).

Step 2: Install DomPDF Package

composer require barryvdh/laravel-dompdf

This will also install all necessary dependencies like:

  • dompdf/dompdf

  • php-font-lib

  • php-svg-lib

  • php-css-parser

Step 3: Configure DomPDF

In config/app.php, add the provider and alias:

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

Step 4: Create a Layout Blade Template

Create resources/views/layout.blade.php:

<!DOCTYPE html> <html> <head> <title>Laravel PDF Example</title> <link href="{{ asset('css/app.css') }}" rel="stylesheet"> </head> <body> <div class="container"> @yield('content') </div> <script src="{{ asset('js/app.js') }}"></script> </body> </html>

Step 5: Create Model and Migration

php artisan make:model Disneyplus -m

Update the migration file:

public function up() { Schema::create('disneypluses', function (Blueprint $table) { $table->id(); $table->string('show_name'); $table->string('series'); $table->string('lead_actor'); $table->timestamps(); }); }

Then run:

php artisan migrate

Step 6: Create Controller and Routes

php artisan make:controller DisneyplusController

Define the routes in routes/web.php:

Route::get('disneyplus', 'DisneyplusController@create')->name('disneyplus.create'); Route::post('disneyplus', 'DisneyplusController@store')->name('disneyplus.store'); Route::get('disneyplus/list', 'DisneyplusController@index')->name('disneyplus.index'); Route::get('/downloadPDF/{id}', 'DisneyplusController@downloadPDF');

Step 7: Form Blade File

Create resources/views/form.blade.php:

@extends('layout') @section('content') <div class="card mt-4"> <div class="card-header">Add Disneyplus Show</div> <div class="card-body"> <form method="post" action="{{ route('disneyplus.store') }}"> @csrf <div class="form-group"> <label>Show Name:</label> <input type="text" name="show_name" class="form-control"> </div> <div class="form-group"> <label>Series:</label> <input type="text" name="series" class="form-control"> </div> <div class="form-group"> <label>Lead Actor:</label> <input type="text" name="lead_actor" class="form-control"> </div> <button type="submit" class="btn btn-primary">Save Show</button> </form> </div> </div> @endsection

Step 8: Store and Display Data

DisneyplusController.php

use App\Disneyplus; use PDF; public function create() { return view('form'); } public function store(Request $request) { $request->validate([ 'show_name' => 'required', 'series' => 'required', 'lead_actor' => 'required', ]); Disneyplus::create($request->all()); return redirect()->route('disneyplus.index')->with('success', 'Show saved!'); } public function index() { $shows = Disneyplus::all(); return view('list', compact('shows')); }

Disneyplus Model:

protected $fillable = ['show_name', 'series', 'lead_actor'];

Step 9: List Blade File

Create resources/views/list.blade.php:

@extends('layout') @section('content') <table class="table mt-4"> <thead> <tr> <th>ID</th> <th>Show Name</th> <th>Series</th> <th>Lead Actor</th> <th>PDF</th> </tr> </thead> <tbody> @foreach($shows as $show) <tr> <td>{{ $show->id }}</td> <td>{{ $show->show_name }}</td> <td>{{ $show->series }}</td> <td>{{ $show->lead_actor }}</td> <td><a href="{{ url('/downloadPDF/'.$show->id) }}">Download PDF</a></td> </tr> @endforeach </tbody> </table> @endsection

Step 10: Create PDF Blade View

Create resources/views/pdf.blade.php:

<!DOCTYPE html> <html> <head> <title>Show PDF</title> </head> <body> <h2>Disneyplus Show Information</h2> <table width="100%" border="1" cellpadding="10"> <tr> <th>Show Name</th> <td>{{ $show->show_name }}</td> </tr> <tr> <th>Series</th> <td>{{ $show->series }}</td> </tr> <tr> <th>Lead Actor</th> <td>{{ $show->lead_actor }}</td> </tr> </table> </body> </html>

Step 11: Generate PDF in Controller

public function downloadPDF($id) { $show = Disneyplus::find($id); $pdf = PDF::loadView('pdf', compact('show')); return $pdf->download('show-details.pdf'); }

Conclusion

With this setup, you've created a full Laravel CRUD system and enabled PDF export using laravel-dompdf. This is perfect for CRM systems or apps that need printed records, reports, or invoices.

Let me know if you’d like this cleaned up into a downloadable Laravel project or integrated with advanced features like charts, tables, headers/footers, or multi-page PDFs!

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