Table BulkActions: Download PDF From Selected Rows

2024-05-07

You can add a custom BulkAction to the table, building a file from the selected table rows and download it immediately.

This example is to build/download the PDF from the selected rows, but you can use it to build and download any file using the same Collection and response()->streamDownload().

Install the package for PDF:

composer require barryvdh/laravel-dompdf

Use this package to form the PDF from Blade.

app/Filament/Resources/UserResource.php:

use Barryvdh\DomPDF\Facade\Pdf;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Blade;
 
public static function table(Table $table): Table
{
return $table
// ...
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\BulkAction::make('Export')
->icon('heroicon-m-arrow-down-tray')
->openUrlInNewTab()
->deselectRecordsAfterCompletion()
->action(function (Collection $records) {
return response()->streamDownload(function () use ($records) {
echo Pdf::loadHTML(
Blade::render('pdf', ['records' => $records])
)->stream();
}, 'users.pdf');
}),
]),
]);
}

The Blade file itself.

resources/views/pdf.blade.php:

<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Created At</th>
</tr>
</thead>
<tbody>
@foreach($records as $record)
<tr>
<td>{{ $record->name }}</td>
<td>{{ $record->email }}</td>
<td>{{ $record->created_at }}</td>
</tr>
@endforeach
</tbody>
</table>

A few of our Premium Examples: