How to get selected records in PDF using filament 3

if there are 100 records from that few selected records pdf i need how to make that possible in filament 3
Solution:
try this: ```php...
Jump to solution
40 Replies
LeandroFerreira
LeandroFerreira6mo ago
would you like to get the records from the table? Could you explain what are you trying to do?
Vishal Thakkar
Vishal ThakkarOP6mo ago
Yes i want to get records from the table i will select the records checkbox and need one button to download PDF of that selected records
Vishal Thakkar
Vishal ThakkarOP6mo ago
@Leandro Ferreira you will get more clarity with this image
No description
Vishal Thakkar
Vishal ThakkarOP6mo ago
@Leandro Ferreira Sorry to bother you but if you can suggest how to resolve this its urgent for me
LeandroFerreira
LeandroFerreira6mo ago
ahh ok, inject $livewire and try getSelectedTableRecords
->action(function (YourListPage $livewire) {
$selected = $livewire->getSelectedTableRecords();
})
->action(function (YourListPage $livewire) {
$selected = $livewire->getSelectedTableRecords();
})
Vishal Thakkar
Vishal ThakkarOP6mo ago
I have tried this with livewire but its not working. as i have to work with loop as can get single record in my application i am able to download all the records PDF but not been able to download selected records PDF. if i want to download single record PDF that is also working having issue with selected records
Matthew
Matthew6mo ago
Multiple PDF's ? Or one containing all the records ?
Vishal Thakkar
Vishal ThakkarOP6mo ago
one containing all the records i want to created a product catalog. so when administrator select few records he can generate single PDF contains all selected records
LeandroFerreira
LeandroFerreira6mo ago
hum, I think you can use bulk action in the table header actions to get the selected records:
Tables\Actions\BulkAction::make('generatePdf')
->action(function (Collection $records) {
//...
})
Tables\Actions\BulkAction::make('generatePdf')
->action(function (Collection $records) {
//...
})
Vishal Thakkar
Vishal ThakkarOP6mo ago
below its the code from which i am generating all the records PDF $product = Products::orderBy('position','asc')->get(); $pdf = pdf::loadView('catalogpdf',array('product' => $product))->setPaper('letter'); return $pdf->download('catalog.pdf'); and i am using this action Tables\Actions\Action::make('Download Pdf') ->icon('heroicon-o-code-bracket-square')->url('/pdf') @Leandro Ferreira please help me if you can as i am stuck in this from last 10 days each functionlity is done just this thing is incomplete
LeandroFerreira
LeandroFerreira6mo ago
Did you try bulk action as I said?
Vishal Thakkar
Vishal ThakkarOP6mo ago
yes i have tried that let me show you that what is the issue in that code @Leandro Ferreira i need to access like below code which i have used to delete image of selected records function (Collection $records) { $records->each(function (Products $record) { if ($record->image) { Storage::disk('public')->delete($record->image); } }); }) and for all selected records in one PDF i need to use route to transfer that selected array we can get selected records array with the help as you suggested $selected = $livewire->getSelectedTableRecords(); but again its using livewire so problem is not getting sorted out
LeandroFerreira
LeandroFerreira6mo ago
Would you like to generate a single PDF per record, right? Why not create a zip containing all the PDFs??
Vishal Thakkar
Vishal ThakkarOP6mo ago
i want all the selected records in one single PDF not single pdf per record
LeandroFerreira
LeandroFerreira6mo ago
You have the selected records, you can build a pdf with them
Vishal Thakkar
Vishal ThakkarOP6mo ago
i am not been able to build that what i am saying.... if you have any solution to build a single PDF for selected records do send me @Leandro Ferreira check this image and red box which i have mark you will come to know that i mean to say
Vishal Thakkar
Vishal ThakkarOP6mo ago
No description
LeandroFerreira
LeandroFerreira6mo ago
Ok, just to clarify, this is not a Filament issue. You can get the selected records as your first question
Lucky0
Lucky06mo ago
you having trouble converting data to PDF?
Vishal Thakkar
Vishal ThakkarOP6mo ago
this is the filament issue only how can i get selected records array to pass in route
Lucky0
Lucky06mo ago
what output did you get when you do dd($outputSelected)? if you get the selected output then you can just convert them into one PDF
Vishal Thakkar
Vishal ThakkarOP6mo ago
@Lucky0 $selected = $livewire->getSelectedTableRecords(); through this i can get selected records but i can use the records one by one. i cannot pass the array to route. and livewire is not allwowing to generate PDF as i am using domPDF package
Lucky0
Lucky06mo ago
use this as reference: https://laraveldaily.com/post/laravel-dompdf-generate-simple-invoice-pdf-with-images-css all i can help is with the logic. hopefully.. what you want is to get list of that selected items. right? try to output it in a page see if its displaying all the items. then you use that page and convert it to pdf.. not sure if this will work.. havent tried it myself, but you never know, unless you try..
Vishal Thakkar
Vishal ThakkarOP6mo ago
@Lucky0 i agree with you this working but this thing work for a single record not working for selected records i have already tried this but this does not help in my senario
Lucky0
Lucky06mo ago
can you share you code here, please?
Vishal Thakkar
Vishal ThakkarOP6mo ago
@Lucky0 This code i am using for generating all records PDF and its working fine /* Code in controller function start / $product = Products::orderBy('position','asc')->get(); $pdf = pdf::loadView('catalogpdf',array('product' => $product))->setPaper('letter'); return $pdf->download('catalog.pdf'); / Code in controller ends */ and i am using this action Tables\Actions\Action::make('Download Pdf') ->icon('heroicon-o-code-bracket-square')->url('/pdf')
Lucky0
Lucky06mo ago
can i see your catalogpdf code?
Vishal Thakkar
Vishal ThakkarOP6mo ago
i am getting selected records with the help of this in bulk action Tables\Actions\DeleteBulkAction::make()->before( function (Collection $records) { $records->each(function (Products $record) { // i can get single record through this }); }), ]) with this i am able to access single record i cannot pass an array to routes Acion in filament resource Tables\Actions\Action::make('Download Pdf') ->icon('heroicon-o-code-bracket-square')->url('/pdf') Routes Route::any('/pdf', [ProductsController::class, 'index'])->name('products.index'); // Function in contorller public function index() { $product = Products::orderBy('position','asc')->get(); $pdf = pdf::loadView('catalogpdf',array('product' => $product))->setPaper('letter'); return $pdf->download('catalog.pdf'); }
LeandroFerreira
LeandroFerreira6mo ago
pass the record ids to your route $records->pluck('id')->implode(',')
Vishal Thakkar
Vishal ThakkarOP6mo ago
how to pass that array to route its not possible
LeandroFerreira
LeandroFerreira6mo ago
this is a string, not array
Vishal Thakkar
Vishal ThakkarOP6mo ago
okay let me try if that can work will get back to you with this
LeandroFerreira
LeandroFerreira6mo ago
pass the ids as I said and get the ids in your class $ids = str(request()->ids)->explode(',');
Solution
Lucky0
Lucky06mo ago
try this:
Tables\Actions\BulkAction::make('generatePdf')
->label('Generate PDF')
->action(function ($records) {
$pdf = Pdf::loadView('pdf.selected-items', ['records' => $records]);
return response()->streamDownload(function () use ($pdf) {
echo $pdf->output();
}, 'selected-items.pdf');
})
->deselectRecordsAfterCompletion(),
Tables\Actions\BulkAction::make('generatePdf')
->label('Generate PDF')
->action(function ($records) {
$pdf = Pdf::loadView('pdf.selected-items', ['records' => $records]);
return response()->streamDownload(function () use ($pdf) {
echo $pdf->output();
}, 'selected-items.pdf');
})
->deselectRecordsAfterCompletion(),
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Selected Items</title>
<style>
/* Add any necessary styles here */
</style>
</head>

<body>
<h1>Selected Items</h1>
@foreach ($records as $record)
<h2>Item {{ $loop->iteration }}</h2>

<p>ID: {{ $record->id }}</p>
<p>Name: {{ $record->name }}</p>

@endforeach
</body>

</html>
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Selected Items</title>
<style>
/* Add any necessary styles here */
</style>
</head>

<body>
<h1>Selected Items</h1>
@foreach ($records as $record)
<h2>Item {{ $loop->iteration }}</h2>

<p>ID: {{ $record->id }}</p>
<p>Name: {{ $record->name }}</p>

@endforeach
</body>

</html>
with this you dont have to use controller
Vishal Thakkar
Vishal ThakkarOP6mo ago
@Lucky0 will try this and get back to you once it works hope both the technique suggested works will get back to you within an hour
Lucky0
Lucky06mo ago
this one work. i tried it
Vishal Thakkar
Vishal ThakkarOP6mo ago
have you use domPDF ?
Lucky0
Lucky06mo ago
i used this
composer require barryvdh/laravel-dompdf
composer require barryvdh/laravel-dompdf
krekas
krekas6mo ago
this questions was asked more then once. should have tried searching...
Vishal Thakkar
Vishal ThakkarOP6mo ago
@Lucky0 Problem is resolved as per your suggestion Thank you very much for a great help @Leandro Ferreira Thank you for your support
Want results from more Discord servers?
Add your server