F
Filamentβ€’15mo ago
leoPascal

How to generate PDF using DomPDF?

Hello Filament community, I'm new to Filament and I have a question. In my Filament project, I need to export filtered data to a PDF using DomPDF. Currently, I'm able to export the entire set of 10 records from the database. However, when I apply a filter, all 10 entries continue to appear in the exported PDF. I want to export only the filtered data, which are about 3 entries, and generate a PDF. Could someone please assist me with this?
48 Replies
cheesegrits
cheesegritsβ€’15mo ago
How are you doing the export?
leoPascal
leoPascalOPβ€’15mo ago
@Hugh Messenger thanks for your reply. Honestly, I don't know how to export anything from view that's why I'm calling from the database. I'm using the default filter method of Filament.
cheesegrits
cheesegritsβ€’15mo ago
But how are you doing the actual export? Are you using a plugin? Rolling your own code to do it?
leoPascal
leoPascalOPβ€’15mo ago
I'm using DomPDF, on button click a controller named "PDFController" will run the code and call all transactions from the database, then display them through the blade view in a new tab.
leoPascal
leoPascalOPβ€’15mo ago
@krekas thanks for your reply, this pdf generation is for single entry I want to display all the filtered items.
krekas
krekasβ€’15mo ago
So adjust to you needs
leoPascal
leoPascalOPβ€’15mo ago
like this.
krekas
krekasβ€’15mo ago
$this->getRecords() might get you records that are in the table
leoPascal
leoPascalOPβ€’15mo ago
it gives error, If I use in PDfcontroller then the error is "Method App\Http\Controllers\PDFReportController::getRecord does not exist."
krekas
krekasβ€’15mo ago
Of course it won't exist in your controller
leoPascal
leoPascalOPβ€’15mo ago
Sorry, I know it's silly but I'm new to Filament and still learning. OMG, there is no one who can help me with this? very disappointing.
Dennis Koch
Dennis Kochβ€’15mo ago
Sounds a bit entitled after those 2 already tried to help you.
leoPascal
leoPascalOPβ€’15mo ago
I'm glad that they tried but no success...
Dennis Koch
Dennis Kochβ€’15mo ago
This should give you all keys of the records on the table. Best to store this in the session, since it might be too much data to pass via request. Then in your Controller load the data from the session
Action::make('pdf')
->action(fn ($livewire) => dd($livewire->getAllTableRecordKeys()))
Action::make('pdf')
->action(fn ($livewire) => dd($livewire->getAllTableRecordKeys()))
leoPascal
leoPascalOPβ€’15mo ago
Isn't it very common what I'm trying to achieve?
Dennis Koch
Dennis Kochβ€’15mo ago
Usually people would use a BulkAction I guess. Since it sounds like you are using a table header action, we need to do a bit of source code diving.
leoPascal
leoPascalOPβ€’15mo ago
tried this, error "Method App\Filament\Resources\TransactionResource\Pages\ListTransactions::getAllTableRecordKeys does not exist." I gave this function in my main resource.php file public static function getAllTableRecordKeys(Table $table): array { $filteredData = $table->getFilters(); return [ $filteredData, ]; }
Dennis Koch
Dennis Kochβ€’15mo ago
Are you on an up-to-date v2 version? I checked that the method exists, might be that I accidentally jumped to v3 code though. Otherwise use $livewire->getRecords() as Krekas suggested. No need to add somethin to your Resource
leoPascal
leoPascalOPβ€’15mo ago
this is the actual filament version -> filament/filament v2.17.53
Dennis Koch
Dennis Kochβ€’15mo ago
Try the other method then
leoPascal
leoPascalOPβ€’15mo ago
tried with this code ->action(fn ($livewire) => dd($livewire->getRecords())),
No description
Dennis Koch
Dennis Kochβ€’15mo ago
Is it ->getTableRecords() als shown in the suggestion (green box)?
leoPascal
leoPascalOPβ€’15mo ago
ohh so sorry, my bad. Now, I got the results...
leoPascal
leoPascalOPβ€’15mo ago
No description
Dennis Koch
Dennis Kochβ€’15mo ago
Ah, it's a paginator in this case πŸ˜… πŸ™ˆ
leoPascal
leoPascalOPβ€’15mo ago
No, the data I need is in "items -> items -> attributes, have a look in this image
No description
Dennis Koch
Dennis Kochβ€’15mo ago
Yes, but only for the current page. Not all pages Is that what you want? Otherwise, this could help: There's ->getFilteredTableQuery(). It's a protected method therefore you need to use Livewire's invade(). This should give you all id's of the current table query
invade($livewire)->getFilteredTableQuery()->pluck('id')
invade($livewire)->getFilteredTableQuery()->pluck('id')
leoPascal
leoPascalOPβ€’15mo ago
no no, right now the filtered data is only on 1st page. later, going forward I may go on more pages.
Dennis Koch
Dennis Kochβ€’15mo ago
Whatever works for you.
leoPascal
leoPascalOPβ€’15mo ago
Great, this works, now can see the id's
No description
leoPascal
leoPascalOPβ€’15mo ago
@Dennis Koch thank you so very much for your kind help.
Dennis Koch
Dennis Kochβ€’15mo ago
You're welcome
leoPascal
leoPascalOPβ€’15mo ago
However, I'm still trying to find a way to send this data to the PDFController like this. protected function getActions(): array { $filteredDataIds = []; // Declare the variable here return [ Actions\ButtonAction::make('Generate PDF') ->action(function ($livewire) use (&$filteredDataIds) { $filteredDataIds = invade($livewire)->getFilteredTableQuery()->pluck('id'); }) ->url(TransactionResource::generatePDFUrl(['filteredDataIds' => $filteredDataIds])) ->openUrlInNewTab(), Actions\CreateAction::make(), ]; }
Dennis Koch
Dennis Kochβ€’15mo ago
You cannot use ->url() when you already use ->action(). Please see my answers above and use a redirect.
krekas
krekasβ€’15mo ago
or just generate pdf without controller in the action
Dennis Koch
Dennis Kochβ€’15mo ago
Yeah, should also be an option. Create a PDF and return it from the action
cheesegrits
cheesegritsβ€’15mo ago
Apologies for not following up on this, I got swamped with my own issues. However, the way to remind people is to "bump" your thread by responding to it and politely asking if anyone has any ideas, not to bitch and moan. Everyone responding to you is a volunteer, doing it for free. If you want guaranteed responses, the Filament team offers paid consultancy, or sponsorship levels on Github which include private support.
leoPascal
leoPascalOPβ€’15mo ago
@Hugh Messenger I want to extend my apologies for my previous message, which was unnecessarily negative. I didn't mean to hurt anyone, and I truly value the contributions of everyone here, including you, @Dennis Koch, @krekas. It was wrong of me to express my frustration in that manner, and I regret my words have hurt you. I greatly appreciate the help from volunteers like you, and I'm committed to ensuring better communication going forward. @krekas thanks for your reply. I tried that but not working.
krekas
krekasβ€’15mo ago
at least tell how you tried
leoPascal
leoPascalOPβ€’15mo ago
now, I'm able to pass the filteredIds and receive in this function. public function downloadReport($filteredDataIds) { dd($filteredDataIds); $transactions = Transaction::whereIn('id', $filteredDataIds)->get(); dd($transactions); $pdf = PDF::loadview('reportPDF', $filteredDataIds); $pdf->set_paper('letter', 'landscape'); return $pdf->stream('report.pdf'); }
krekas
krekasβ€’15mo ago
you can't return pdf stream like that it's in livewire docs how to force download you need response check the link i sent you how it is done there
leoPascal
leoPascalOPβ€’15mo ago
I tried like this.. <?php namespace App\Filament\Resources\TransactionResource\Pages; use App\Filament\Resources\TransactionResource; use App\Http\Controllers\PDFReportController; use Filament\Pages\Actions; use Filament\Resources\Pages\ListRecords; use PDF; class ListTransactions extends ListRecords { protected static string $resource = TransactionResource::class; protected function getActions(): array { $filteredDataIds = []; // Declare the variable here return [ Actions\ButtonAction::make('Generate PDF') ->action(function ($livewire) use ($filteredDataIds) { $filteredDataIds = invade($livewire)->getFilteredTableQuery()->pluck('id'); $pdf = PDF::loadview('reportPDF', $filteredDataIds); $pdf->set_paper('letter', 'landscape'); return $pdf->stream('report.pdf'); }), Actions\CreateAction::make(), ]; } }
krekas
krekasβ€’15mo ago
i just answered to you and you post the same againπŸ€¦β€β™‚οΈ
Dennis Koch
Dennis Kochβ€’15mo ago
Check the Livewire docs that krekas mentioned: https://laravel-livewire.com/docs/2.x/file-downloads#introduction
Livewire
File Downloads | Livewire
A full-stack framework for Laravel that takes the pain out of building dynamic UIs.
krekas
krekasβ€’15mo ago
Laravel
File Downloads | Laravel
A full-stack framework for Laravel that takes the pain out of building dynamic UIs.
leoPascal
leoPascalOPβ€’15mo ago
what I mean in this code is with the first dd($filteredDataIds); I can see result like this ""[1,2,3,5]" // app\Http\Controllers\PDFReportController.php:14". But, with the second dd($transactions); it give this result "count(): Argument #1 ($value) must be of type Countable|array, string given". @krekas & @Dennis Koch thanks again, I got it completely. I understand that is the wrong way, but firstly the issue is something else here.πŸ‘†
cheesegrits
cheesegritsβ€’15mo ago
It doesn't bother me, I have thick skin. The reason I mentioned it is just so you know that you are much more likely to get help in the future if you don't act like that. πŸ™‚
Want results from more Discord servers?
Add your server