F
Filamentβ€’9mo ago
astronomic

Pdf stream() doesn't work on submit() custom page

Hello, I have a custom page, where I have a search form, but I want a PDF to open when I click send. But it's not working, I guess it will be done another way, but I don't know which one. I need to open the pdf in another tab and that's it, I don't want to download it directly. Can somebody help me? thanks Submit custom page: public function submit() { $teacher = Employee::find($this->data['professor']); $sessions = $teacher->sessions()->where('course_id', $this->data['course'])->get(); if (!$this->data['course']) { return Notification::make()->duration(12000)->title("Selecciona un/a curs per seguir")->danger()->send(); } if (count($sessions) == 0) { return Notification::make()->duration(12000)->title("No hi ha sessions per generar informe")->danger()->send(); } $pdf = PDF::loadView('pdf.report-hours-kms', ['data' => $sessions,'teacher' => $teacher]); return $pdf->stream(); }
17 Replies
Dennis Koch
Dennis Kochβ€’9mo ago
If you want it in a new tab, you need a normal browser link that opens a new page and that page streams the PDF.
toeknee
toekneeβ€’9mo ago
Yeah I tend to use:
return response()->streamDownload(function () use ($pdf, $path, $filename) {
echo $pdf->output();
File::delete($path.$filename);
}, $filename);
return response()->streamDownload(function () use ($pdf, $path, $filename) {
echo $pdf->output();
File::delete($path.$filename);
}, $filename);
astronomic
astronomicβ€’9mo ago
Thanks but If I do like toeknee, it downloads, what I want is for the pdf to open in the browser
toeknee
toekneeβ€’9mo ago
Then, you can have a custom URL which allows you to stream it on a new page instead
astronomic
astronomicβ€’9mo ago
But I'm not saving the pdf, I just want to show so that they decide to download or print, or have I misunderstood?
awcodes
awcodesβ€’9mo ago
you can't show a pdf without saving it first. the browser has to have a url to show it. just like an image.
astronomic
astronomicβ€’9mo ago
But in Laravel I have always shown it with $pdf->stream() without saving it. Is it a filament thing then? It's about learning why it happens, thanks
awcodes
awcodesβ€’9mo ago
how were you displaying the stream?
astronomic
astronomicβ€’9mo ago
In Laravel I always did like this: $pdf = PDF::loadView('pdf.report-hours-kms', ['data' => $sessions,'teacher' => $teacher]); return $pdf->stream();
toeknee
toekneeβ€’9mo ago
They are building it with domPDF into tmp and steaming it basically
astronomic
astronomicβ€’9mo ago
Yes, I use domPDF. P.D: I'm a woman πŸ˜‰
toeknee
toekneeβ€’9mo ago
Apologise for the assumption
astronomic
astronomicβ€’9mo ago
There's no problem πŸ˜‰
toeknee
toekneeβ€’9mo ago
So you can do:
return response()->stream(function () use ($pdf, $path, $filename) {
echo $pdf->output();
File::delete($path.$filename);
}, $filename);
return response()->stream(function () use ($pdf, $path, $filename) {
echo $pdf->output();
File::delete($path.$filename);
}, $filename);
But I doubt you want that. So instead, move the PDF Building into a new controller and have the url of the action open the new url which contains the id of the file you are bullding? if it's live / on the fly, you will need to save it, view it and setup a command to auto-clear the tmp pdf folder
awcodes
awcodesβ€’9mo ago
I think the issue is that you are in the context of a livewire request. Maybe some insight here. https://livewire.laravel.com/docs/wire-stream
Laravel
wire:stream | Laravel
A full-stack framework for Laravel that takes the pain out of building dynamic UIs.
awcodes
awcodesβ€’9mo ago
nevermind, the tag says your on v2. this won't help since stream is a livewire v3 feature.
Dennis Koch
Dennis Kochβ€’9mo ago
Livewire handles streamed data as a download. That's just the way downloads in Livewire work. That's why you need a separate page where you stream the data, so you can open it in a new tab.