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:Jump to solution
I managed to do it
`return response()->streamDownload(function () use ($record) {
echo base64_decode(Pdf::view('pdf.invoice', ['invoice' => $record])
->format('a4')...
16 Replies
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');
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
That is my backup idea, I am trying to avoid it if it's possible
I couldn't figure out a way quickly last time, It's how the PDF rendering engine returns the stream IIRC
I'll try it today, I have an idea but I doubt it'll work
Solution
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');
@toeknee
Ahhh base64x handy
yeah, but it works π
Let me know if it works for you
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:
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:
But this still just downloads the pdf file.No idea tbh, perhaps you can look on how this is achieved in a generic Livewire component
I ended up just creating a test route for my view in web.php:
As I just wanted to see the output without needing to download the pdf to check changes during development.
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 wellAhh yeah that sounds really helpful! Thanks for the tip!