A Guide to Work with PDF Generation in FilamentPHP

A Guide to Work with PDF Generation in FilamentPHP

Introduction

FilamentPHP is a fantastic tool that helps rapidly build Admin Panels. It consists of everything you need to build amazing and performant admin panels.

But while building Admin panels, there are cases where you have to build additional features like Generating Invoices/QR Codes. And it can be a bit tricky to build these features when you are working with tools like FilamentPHP.(unless they provide these features out of the box)

So in this blog, we'll be looking at how we can generate PDFs in a Filament-based Admin Panel.


Prerequisites

I assume you already have a working admin panel built using Filament.

And if you don't have one, you can take a reference from this repository (this also contains the code demonstrated in this blog)

And if you are relatively new to FilamentPHP, you can take this beginners course which will get you up and running with Filament.


Installation

For working with PDFs a bit easier, I'm gonna be using an Invoice generator package called laraveldaily/laravel-invoices

So let's start by installing this package:

composer require laraveldaily/laravel-invoices

Implementation

Let's start by adding a new action in the actions functions, we'll give it the name of "Download Pdf", pass an heroicon-o-document-download Icon and in the URL function, we'll get the record by passing in a closure and then redirect it to student.pdf.download route and pass the record as a parameter. One more thing to note here is that we need to pass the model in the closure as $record, this will throw an error if you name it something else.

We're also passing in the openUrlInNewTab() function, it's just a personal preference and also depends on the use case.

->actions([
            // ...
            Tables\Actions\Action::make('Download Pdf')
                ->icon('heroicon-o-document-download')
                ->url(fn (Student $record) => route('student.pdf.download', $record))
                ->openUrlInNewTab(),
            ])

Let's go ahead and define the route now, this code is self-explanatory:

Route::get('/{record}/pdf/download', [DownloadPdfController::class, 'download'])->name('student.pdf.download');

Let's look into the DownloadPdfController

<?php

namespace App\Http\Controllers;

use App\Models\Student;
use Illuminate\Http\Request;
use LaravelDaily\Invoices\Invoice;
use LaravelDaily\Invoices\Classes\Buyer;
use LaravelDaily\Invoices\Classes\InvoiceItem;

class DownloadPdfController extends Controller
{
    public function download(Student $record)
    {
        $customer = new Buyer([
            'name'          => 'John Doe',
            'custom_fields' => [
                'email' => '[email protected]',
            ],
        ]);

        $item = (new InvoiceItem())->title('Service 1')->pricePerUnit(2);

        $invoice = Invoice::make()
            ->buyer($customer)
            ->discountByPercent(10)
            ->taxRate(15)
            ->shipping(1.99)
            ->addItem($item);

        return $invoice->stream();
    }
}

The code inside the download function contains the logic for the PDF generation, and depending on your use case, you can define the logic accordingly.

For demonstration, I've copy-pasted this demo code from the laravel-invoices package.

We're done with the implementation part, it's finally time to test it out.


Testing

If we go ahead and refresh our table, we should see a Download Pdf beside Edit and Delete buttons. And if Clicked, will open a new tab in which our PDF gets rendered.

So yeah, this is how to implement PDF generation in a filament-based app.


Conclusion

If you prefer a video-based tutorial, you can check out this video:

And finally, If you liked this article and wanna see more content like this, you can follow me on Twitter, I'm most active there.


🔥 New Course Alert
★★★★½4.6/5

Filament Admin Panel Course for Laravel (Updated for V3)

Learn to build Admin Panels using FilamentPHP practically by building multiple projects along the way.

30-Day Money Back
1000+ Students
Limited Time
80% OFF
Enroll Now
A
Athanasios A.
★★★★★

Been on Laravel development with blade only for a few years now and decided to give Filament a go. What a brilliant surprise to find this tutorial ! Tapan is an excellent tutor he goes into all the corners of the development labyrinth that we all find ourselves into when getting onboard with a new tech and he manages to give us all everything we would need to deliver a fully-pledged app. I dont think once could ask for anything more. The tutorial spoon feeds us with concise information and his calm voice really helps. Five stars for me, i have a very productive weekend and now I will be all heads in development with Filament. So excited! Thank you Tapan !