Download a generated file with Action button

Is there a simple way to download a file by pressing an action button? My code already prepare a proper response with the correct mime type and body but the request is not handled correctly by the browser. If I cannot use a direct download I need to use a URL but I need to send POST inputs to the request as I'm generating a PDF preview of the form.
6 Replies
LeandroFerreira
LeandroFerreira6mo ago
->action(fn() => Storage::download('file.jpg'))
->action(fn() => Storage::download('file.jpg'))
?
hitech95
hitech95OP6mo ago
Its a stream tho. Its not a stored file I'm taking the form data via $get and passing it to a service that generate a PDF the pdf is not stored. i've tried with the livewire:
response()->streamDownload(fn() => mycalltoservice(), $get('name').'.pdf')
response()->streamDownload(fn() => mycalltoservice(), $get('name').'.pdf')
Oxalate
Oxalate6mo ago
When using this for a file for a model, it says:
unable to retrieve the file_size for file at location sd02380jsf.jpg for example.
awcodes
awcodes6mo ago
Are you trying to download it before persisting it to storage? Can you share the code of your actual action?
hitech95
hitech95OP6mo ago
Yes, it is a http response with proper headers set. As it is a preview/draft it is not stored on the storage. I can provide an example! The code is basically:
Action::make('download-draft')
->label('Download draft')
->action(function ($get) {
$mpdf = new \Mpdf\Mpdf();
// This uses the $get to generate a proper page
$mpdf->WriteHTML('Hello World');

// Sends output inline to browser
$mpdf->Output();

// I also tried with:
response()->streamDownload(fn() => $mpdf->Output($get('name').'.pdf', Destination::INLINE)

});
Action::make('download-draft')
->label('Download draft')
->action(function ($get) {
$mpdf = new \Mpdf\Mpdf();
// This uses the $get to generate a proper page
$mpdf->WriteHTML('Hello World');

// Sends output inline to browser
$mpdf->Output();

// I also tried with:
response()->streamDownload(fn() => $mpdf->Output($get('name').'.pdf', Destination::INLINE)

});
@Leandro Ferreira If necessary I can update my previous repo demo with such example.
Shavik
Shavik4w ago
I know this is an old topic but I just fought with this a bit on downloading a generated PDF from the spatie package. @awcodes I know you're probably watching the super bowl today but saw you asked him to paste the whole action. In my case, I had binary PDF data from the internal browsershot instance within Laravel PDF. This code properly returns the PDF and autodownloads it.
$pdfContent = $receipt->getBrowsershot()->pdf();
$pdfContent = $receipt->getBrowsershot()->pdf();
Where $receipt is a PdfBuilder instance.
return response()->streamDownload(function () use ($pdfContent) {
echo $pdfContent;
}, $filename, [
'Content-Type' => 'application/pdf',
]);
return response()->streamDownload(function () use ($pdfContent) {
echo $pdfContent;
}, $filename, [
'Content-Type' => 'application/pdf',
]);
I put this in a method on my Order model so I can do something like this:
Actions\Action::make('receipt')
->label('Download Receipt')
->icon('far-file-pdf')
->action(fn (Order $order) => $order->downloadReceipt()),
Actions\Action::make('receipt')
->label('Download Receipt')
->icon('far-file-pdf')
->action(fn (Order $order) => $order->downloadReceipt()),

Did you find this page helpful?