使用 laravel-dompdf 进行 laravel 打印预览
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44746763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
laravel print preview using laravel-dompdf
提问by scott
I am using Laravel 5.4 and using
我正在使用 Laravel 5.4 并使用
"barryvdh/laravel-dompdf": "^0.8.0",
https://github.com/barryvdh/laravel-dompdf
https://github.com/barryvdh/laravel-dompdf
i have follwoing code in controller
我在控制器中有以下代码
$pdf = PDF::loadView('print.print', $data);
return $pdf->download('invoice.pdf');
As it will download pdf properly but i am looking to preview the content in the view to print. i have googled lot still not able to get it
因为它会正确下载 pdf,但我希望在视图中预览要打印的内容。我用谷歌搜索了很多仍然无法得到它
回答by Sagar Gautam
You can use stream()
function as stated in documentation here.
您可以使用此处stream()
文档中所述的函数。
According to documentation of dompdf
,
When you use stream like this:
根据文档dompdf
,当您像这样使用流时:
$dompdf->stream('filename.pdf');
In default, you will see a download dialouge box
.
默认情况下,您将看到一个download dialouge box
.
But, you can add parameter Attachment
value to false
so that you can view pdf
in browser.
但是,您可以添加参数Attachment
值,false
以便您可以pdf
在浏览器中查看。
Here is what you need to do:
以下是您需要做的:
$dompdf->stream("filename.pdf", array("Attachment" => false));
This might help you.
这可能对你有帮助。
回答by Romeo Onisim
I've struggled with this and manage to make it work with the following snippet:
我一直在努力解决这个问题并设法使它与以下代码段一起工作:
$html = view('desktop.bill', $data)->render();
return @\PDF::loadHTML($html, 'utf-8')->stream(); // to debug + add the follosing headers in controller ( TOP LEVEL )
Besides the upper command, you will need to add on a TOP LEVEL, like first lines in the controller, the following hearders:
除了上面的命令,您还需要添加一个 TOP LEVEL,如控制器中的第一行,以下听者:
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="document.pdf"');
header('Content-Transfer-Encoding: binary');