简体   繁体   中英

"The Response callback must not be null" when trying to send a PDF file download in Laravel 7

In my Laravel 7 application, I have a route where a PDF is dynamically generated, and I want the browser to treat it as a file download.

In writing this, I found a number of docs and questions about Laravel serving a file download using a file saved on the filesystem, but in my case, I don't want to store the pdf in the filesystem. I just want to dynamically generate it when the user hits the route, and then the browser downloads the pdf.

I'm using the mpdf library and it appears that it is properly forming a pdf file. However, Laravel is throwing a The Response callback must not be null LogicException. In the browser, I see the data of the PDF displayed, followed by the exception stack trace.

Here's the controller method:

public function pdf($id) {
    $manual = new \App\manual($id);
    $contents = view('admin.manual')->with($manual)->render();
    $mpdf = New \Mpdf\Mpdf();
    $mpdf->WriteHtml($contents);
    $pdf = $mpdf->output();
    return response()->streamDownload($pdf, 'manual.pdf');
}

In this screenshot, you can see the data of the PDF file, which tells me its being generated and delivered: 在此处输入图片说明

However the exception follows after the EOF of the PDF object: 在此处输入图片说明

I found this 5-year-old issue from Symfony for the same exception message, but it seems to have been long resolved. I've found nothing else in relation to Laravel 7.

How do I properly specify a pdf file download of data in Laravel 7?

In my Laravel 7 application, I have a route where a PDF is dynamically generated, and I want the browser to treat it as a file download.

In writing this, I found a number of docs and questions about Laravel serving a file download using a file saved on the filesystem, but in my case, I don't want to store the pdf in the filesystem. I just want to dynamically generate it when the user hits the route, and then the browser downloads the pdf.

I'm using the mpdf library and it appears that it is properly forming a pdf file. However, Laravel is throwing a The Response callback must not be null LogicException. In the browser, I see the data of the PDF displayed, followed by the exception stack trace.

Here's the controller method:

public function pdf($id) {
    $manual = new \App\manual($id);
    $contents = view('admin.manual')->with($manual)->render();
    $mpdf = New \Mpdf\Mpdf();
    $mpdf->WriteHtml($contents);
    $pdf = $mpdf->output();
    return response()->streamDownload($pdf, 'manual.pdf');
}

In this screenshot, you can see the data of the PDF file, which tells me its being generated and delivered: 在此处输入图片说明

However the exception follows after the EOF of the PDF object: 在此处输入图片说明

I found this 5-year-old issue from Symfony for the same exception message, but it seems to have been long resolved. I've found nothing else in relation to Laravel 7.

How do I properly specify a pdf file download of data in Laravel 7?

I was struggling with the same issue and solved it using the post above. However in Lumen 8 I needed to change the streamDownload to stream.

Here is my code:

$stream = $mpdf->Output($filename, "I");
return response()->stream(function() use ($stream) { echo $stream; }, 200, ['Content-type'=>'application/pdf']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM