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
How are you doing the export?
@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.
But how are you doing the actual export? Are you using a plugin? Rolling your own code to do it?
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.
@krekas thanks for your reply, this pdf generation is for single entry I want to display all the filtered items.
So adjust to you needs
like this.
$this->getRecords()
might get you records that are in the tableit gives error, If I use in PDfcontroller then the error is "Method App\Http\Controllers\PDFReportController::getRecord does not exist."
Of course it won't exist in your controller
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.
Sounds a bit entitled after those 2 already tried to help you.
I'm glad that they tried but no success...
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
Isn't it very common what I'm trying to achieve?
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.
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,
];
}
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 Resourcethis is the actual filament version -> filament/filament v2.17.53
Try the other method then
tried with this code ->action(fn ($livewire) => dd($livewire->getRecords())),
Is it
->getTableRecords()
als shown in the suggestion (green box)?ohh so sorry, my bad. Now, I got the results...
Ah, it's a paginator in this case π
π
No, the data I need is in "items -> items -> attributes, have a look in this image
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
no no, right now the filtered data is only on 1st page. later, going forward I may go on more pages.
Whatever works for you.
Great, this works, now can see the id's
@Dennis Koch thank you so very much for your kind help.
You're welcome
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(),
];
}
You cannot use
->url()
when you already use ->action()
. Please see my answers above and use a redirect.or just generate pdf without controller in the action
Yeah, should also be an option. Create a PDF and return it from the action
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.
@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.
at least tell how you tried
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');
}
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
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(),
];
}
}
i just answered to you and you post the same againπ€¦ββοΈ
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.
Laravel
File Downloads | Laravel
A full-stack framework for Laravel that takes the pain out of building dynamic UIs.
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.π
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. π