PHP code example of amjadiqbal / filament-record-pdf

1. Go to this page and download the library: Download amjadiqbal/filament-record-pdf library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

amjadiqbal / filament-record-pdf example snippets


use AmjadIqbal\FilamentRecordPdf\Actions\BulkDownloadPdfAction;
use AmjadIqbal\FilamentRecordPdf\Actions\DownloadPdfAction;
use AmjadIqbal\FilamentRecordPdf\Actions\EmailPdfAction;
use AmjadIqbal\FilamentRecordPdf\Actions\PreviewPdfAction;
use AmjadIqbal\FilamentRecordPdf\Actions\SavePdfAction;
use Filament\Actions\ActionGroup;

public static function table(Table $table): Table
{
    return $table
        ->columns([/* ... */])
        ->recordActions([
            ActionGroup::make([
                PreviewPdfAction::make()->pdfView('pdf.invoice'),
                DownloadPdfAction::make()->pdfView('pdf.invoice'),
                EmailPdfAction::make()->pdfView('pdf.invoice')
                    ->defaultRecipient(fn (Invoice $record) => $record->customer_email),
                SavePdfAction::make()->pdfView('pdf.invoice')->saveToColumn('pdf_path'),
            ]),
        ])
        ->toolbarActions([
            BulkDownloadPdfAction::make()->pdfView('pdf.invoice'),
        ]);
}

DownloadPdfAction::make()
    ->pdfView('pdf.invoice')
    ->pdfData(fn (Invoice $record) => [
        'invoice' => [
            'number' => $record->number,
            'date' => $record->issued_at->toDateString(),
            'currency' => 'USD',
            'from' => [config('app.name'), '1 Main Street'],
            'to' => [$record->customer->name, $record->customer->address],
            'items' => $record->lines->map(fn ($line) => [
                'description' => $line->description,
                'quantity' => $line->quantity,
                'unit_price' => $line->unit_price,
            ])->all(),
            'tax_rate' => 10,
        ],
    ]);

->pdfView('pdf.invoice')                                  // string or fn (Model $record)
->pdfData(fn (Invoice $record) => [...])                  // merged with ['record' => $record]
->pdfFilename(fn (Invoice $record) => "Invoice {$record->number}")  // becomes invoice-inv-1042.pdf
->pdfFormat('letter')                                     // a4 (default), letter, legal, a3, a5 ...
->pdfLandscape()
->pdfDriver('browsershot')                                // per action; the default comes from config
->modifyPdfUsing(fn (PdfBuilder $pdf, Invoice $record) => $pdf->margins(10, 10, 10, 10))
->pdfAbility(['downloadPdf', 'view'])                     // see Authorization
->authorizePdfUsing(fn (Invoice $record, ?User $user) => $user?->is($record->owner))

EmailPdfAction::make()
    ->defaultRecipient(fn (Invoice $record) => $record->customer_email)
    ->defaultSubject(fn (Invoice $record) => "Invoice {$record->number}")
    ->defaultMessage('Please find your invoice attached.')
    ->queueMail()                       // or set filament-record-pdf.mail.queue
    // the closure can also take Model $record and array $data (the form values)
    ->sendUsing(fn (array $recipients, RecordPdfMail $mail) => Mail::mailer('postmark')->to($recipients)->send($mail));

SavePdfAction::make()
    ->disk('s3')                                   // default: filament-record-pdf.disk (local)
    ->directory(fn (Invoice $record) => 'invoices/'.$record->issued_at->year)
    ->visibility('private')
    ->saveToColumn('pdf_path')                     // stores the path on the record
    ->overwrite(false)                             // keep old files and add a timestamp to the new one
    ->afterSaving(fn (Invoice $record, string $path, string $disk) => /* ... */);

SavePdfAction::make()->toMediaCollection('pdfs');

BulkDownloadPdfAction::make()
    ->pdfView('pdf.invoice')
    ->zipFilename(fn () => 'invoices-'.now()->toDateString())
    ->queueAfter(25);   // null never queues, 0 always queues

return [
    'driver' => env('RECORD_PDF_DRIVER', 'dompdf'),
    'format' => 'a4',
    'landscape' => false,
    'default_view' => 'filament-record-pdf::default',
    'disk' => env('RECORD_PDF_DISK', 'local'),
    'directory' => 'record-pdfs',
    'zip_directory' => 'record-pdfs/zips',
    'visibility' => 'private',
    'preview_ttl' => 10,
    'cache_store' => env('RECORD_PDF_CACHE_STORE'),
    'route_prefix' => 'filament-record-pdf',
    'route_middleware' => ['web'],
    'bulk' => ['queue_after' => 25, 'connection' => null, 'queue' => null, 'link_hours' => 24, 'max_records' => 500],
    'mail' => ['queue' => false],
];
bash
php artisan vendor:publish --tag="filament-record-pdf-config"
bash
php artisan make:record-pdf-view invoice --preset=invoice