Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sekrom505's avatar

Livewire stream PDF - mccarlosen/laravel-mpdf

Hi,

I'm trying to stream a PDF file directly to the browser in Livewire I'm using mccarlosen/laravel-mpdf

$pdf = PDF::loadView('template', $data);
return $pdf->stream('documentname');

When I click the button nothing happens

Thanks.

0 likes
4 replies
aurawindsurfing's avatar

I imagine this is what your are looking for:

https://laravel-livewire.com/docs/2.x/file-downloads

return response()->streamDownload(function () {
    echo 'CSV Contents...';
}, 'export.csv');

so in your case it might look something like this:

return response()->streamDownload(function () {
    $pdf->stream('documentname');
}, 'documentname.pdf');

But I did not test it ;-)

sekrom505's avatar

Thank you very much for your reply . The issue with this, the download is being triggered . I'm looking for something that opens the PDF in the browser . This way I don't have to force the user to download .

aurawindsurfing's avatar

Ok this is different issue where it is actually browser related and you can not impose that on your users.

Try for instance dooing the same thing in safari, firefox and chrome. Chrome by default likes to open pdf's, firefox likes to download them. You can also change that default behaviour of your browser but you can not control how end user deals with it.

Have a look: https://apple.stackexchange.com/questions/146362/set-chrome-to-open-pdf-rather-than-download-them

Jaikumar's avatar

I have resolve this issue Here is the code : public function DownloadNotes(PDF $p) { $note = AcademyLessonNote::where('user_id',auth()->user()->id)->first(); if($note){ $pdf = $p->loadView('pdf.notesPdf',compact('note'))->output(); return response()->streamDownload(function () use ($pdf) { print($pdf); }, md5(time()).".pdf"); } }

1 like

Please or to participate in this conversation.