简体   繁体   English

尝试在 Laravel 7 中发送 PDF 文件下载时出现“响应回调不能为空”

[英]"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.在我的 Laravel 7 应用程序中,我有一个动态生成 PDF 的路径,我希望浏览器将其视为文件下载。

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.在写这篇文章时,我发现了一些关于 Laravel 使用保存在文件系统上的文件来提供文件下载的文档和问题,但就我而言,我不想将 pdf 存储在文件系统中。 I just want to dynamically generate it when the user hits the route, and then the browser downloads the pdf.我只想在用户点击路由时动态生成,然后浏览器下载pdf。

I'm using the mpdf library and it appears that it is properly forming a pdf file.我正在使用 mpdf 库,它似乎正在正确地形成一个 pdf 文件。 However, Laravel is throwing a The Response callback must not be null LogicException.然而,Laravel 抛出了一个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.在浏览器中,我看到显示的 PDF 数据,然后是异常堆栈跟踪。

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:在这个截图中,你可以看到 PDF 文件的数据,它告诉我它正在生成和交付: 在此处输入图片说明

However the exception follows after the EOF of the PDF object:但是,在 PDF 对象的 EOF 之后出现异常: 在此处输入图片说明

I found this 5-year-old issue from Symfony for the same exception message, but it seems to have been long resolved.我从 Symfony 发现了这个 5 年前的问题,用于相同的异常消息,但它似乎已经解决了很长时间。 I've found nothing else in relation to Laravel 7.我没有发现与 Laravel 7 相关的任何其他内容。

How do I properly specify a pdf file download of data in Laravel 7?如何在 Laravel 7 中正确指定数据的 pdf 文件下载?

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.在我的 Laravel 7 应用程序中,我有一个动态生成 PDF 的路径,我希望浏览器将其视为文件下载。

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.在写这篇文章时,我发现了许多关于 Laravel 使用保存在文件系统上的文件来提供文件下载的文档和问题,但就我而言,我不想将 pdf 存储在文件系统中。 I just want to dynamically generate it when the user hits the route, and then the browser downloads the pdf.我只想在用户点击路由时动态生成,然后浏览器下载pdf。

I'm using the mpdf library and it appears that it is properly forming a pdf file.我正在使用 mpdf 库,它似乎正确地形成了一个 pdf 文件。 However, Laravel is throwing a The Response callback must not be null LogicException.然而,Laravel 抛出了一个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.在浏览器中,我看到显示的 PDF 数据,然后是异常堆栈跟踪。

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:在这个截图中,你可以看到 PDF 文件的数据,它告诉我它正在生成和交付: 在此处输入图片说明

However the exception follows after the EOF of the PDF object:但是,在 PDF 对象的 EOF 之后出现异常: 在此处输入图片说明

I found this 5-year-old issue from Symfony for the same exception message, but it seems to have been long resolved.我从 Symfony 中发现了这个 5 年前的问题,用于相同的异常消息,但它似乎已经解决了很长时间。 I've found nothing else in relation to Laravel 7.我没有发现与 Laravel 7 相关的任何其他内容。

How do I properly specify a pdf file download of data in Laravel 7?如何在 Laravel 7 中正确指定数据的 pdf 文件下载?

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.但是在 Lumen 8 中,我需要将 streamDownload 更改为流。

Here is my code:这是我的代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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