F
Filamentβ€’6mo ago
Prosp30

Download PDF using Spatie Laravel-PDF on table action

I am trying to download a PDF generated from a blade view. I tried DOMPDF and it works fine, but I would like to use Spatie's PDF which has better funcionality. I added the PDF to the table action, but the button just spins and nothing gets returned. Is there a way to convert the PDF to a stream to make it downloadable or? Action::make('download') ->action(function (Invoice $record) { return pdf()->view('pdf.invoice', ['invoice' => $record]) ->format('a4') ->name('your-invoice.pdf') ->download(); }) ->iconButton('heroicon-o-document-arrow-down') ->icon('heroicon-o-document-arrow-down') ->label('Preuzmi PDF'),
Solution:
I managed to do it `return response()->streamDownload(function () use ($record) { echo base64_decode(Pdf::view('pdf.invoice', ['invoice' => $record]) ->format('a4')...
Jump to solution
16 Replies
Prosp30
Prosp30β€’6mo ago
I am trying to do it this way, but no luck. Any ideas? return response()->streamDownload(function () use ($record) { Pdf::html('<p>test</p>') ->format('a4') ->name('your-invoice.pdf'); }, 'test' . '.pdf');
toeknee
toekneeβ€’6mo ago
That's because you are not returning a file blob I believe. I end up saving to a tmp folder adn then providing the filepath for download and deleting on download
Prosp30
Prosp30β€’6mo ago
That is my backup idea, I am trying to avoid it if it's possible
toeknee
toekneeβ€’6mo ago
I couldn't figure out a way quickly last time, It's how the PDF rendering engine returns the stream IIRC
Prosp30
Prosp30β€’6mo ago
I'll try it today, I have an idea but I doubt it'll work
Solution
Prosp30
Prosp30β€’6mo ago
I managed to do it return response()->streamDownload(function () use ($record) { echo base64_decode(Pdf::view('pdf.invoice', ['invoice' => $record]) ->format('a4') ->footerView('pdf.invoicefooter') ->name('your-invoice.pdf') ->base64()); }, 'test' . '.pdf');
Prosp30
Prosp30β€’6mo ago
@toeknee
toeknee
toekneeβ€’6mo ago
Ahhh base64x handy
Prosp30
Prosp30β€’6mo ago
yeah, but it works πŸ˜„ Let me know if it works for you
ralphjsmit
ralphjsmitβ€’5mo ago
Thanks for this! The echo and base64 part were very helpful tips. For reference, I just implemented PDF download using Spatie/laravel-pdf from an action like this:
Tables\Actions\Action::make('download_order_invoice')
->icon('heroicon-o-arrow-down-tray')
->label('Download invoice')
->action(function (Order $order) {
$pdfBuilder = Pdf::view('pdfs.order-invoice', ['order' => $order])
->format(Format::A4)
->name("order-{$order->uuid}.pdf");

return response()->streamDownload(function () use ($pdfBuilder) {
echo base64_decode($pdfBuilder->download()->base64());
}, $pdfBuilder->downloadName);
}),
Tables\Actions\Action::make('download_order_invoice')
->icon('heroicon-o-arrow-down-tray')
->label('Download invoice')
->action(function (Order $order) {
$pdfBuilder = Pdf::view('pdfs.order-invoice', ['order' => $order])
->format(Format::A4)
->name("order-{$order->uuid}.pdf");

return response()->streamDownload(function () use ($pdfBuilder) {
echo base64_decode($pdfBuilder->download()->base64());
}, $pdfBuilder->downloadName);
}),
nowak
nowakβ€’5mo ago
This was very useful for me too! I just wanted to check if you know how to make the action show the pdf in the browser without downloading? I tried using ->stream() instead of ->streamDownload() like this:
Action::make('downloadPdf')
->label('Download Pdf')
->icon('heroicon-o-arrow-down-tray')
->action(function (Component $livewire) {
$records = $livewire->getTableRecords();

$pdfBuilder = Pdf::view('filament.pdf.group-orders', [
'records' => $records,
])
->landscape()
->margins(0, 0, 0, 0)
->format('a4')
->name("test.pdf");

return response()->stream(function () use ($pdfBuilder) {
echo base64_decode($pdfBuilder->base64());
}, 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="test.pdf"'
]);
}),
Action::make('downloadPdf')
->label('Download Pdf')
->icon('heroicon-o-arrow-down-tray')
->action(function (Component $livewire) {
$records = $livewire->getTableRecords();

$pdfBuilder = Pdf::view('filament.pdf.group-orders', [
'records' => $records,
])
->landscape()
->margins(0, 0, 0, 0)
->format('a4')
->name("test.pdf");

return response()->stream(function () use ($pdfBuilder) {
echo base64_decode($pdfBuilder->base64());
}, 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="test.pdf"'
]);
}),
But this still just downloads the pdf file.
ralphjsmit
ralphjsmitβ€’5mo ago
No idea tbh, perhaps you can look on how this is achieved in a generic Livewire component
nowak
nowakβ€’5mo ago
I ended up just creating a test route for my view in web.php:
Route::get('/test-pdf', function () {
$records = GroupOrder::get();

return view('filament.pdf.group-orders', [
'records' => $records
]);
});
Route::get('/test-pdf', function () {
$records = GroupOrder::get();

return view('filament.pdf.group-orders', [
'records' => $records
]);
});
As I just wanted to see the output without needing to download the pdf to check changes during development.
ralphjsmit
ralphjsmitβ€’5mo ago
Makes sense, I generally have a /debug route in my applications that's only available locally (and on production perhaps for admins), that's helpful sometimes a well
nowak
nowakβ€’5mo ago
Ahh yeah that sounds really helpful! Thanks for the tip!
Want results from more Discord servers?
Add your server