在Laravel 5.2中流式传输PDF文件 [英] Stream a PDF file in Laravel 5.2

查看:94
本文介绍了在Laravel 5.2中流式传输PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!我正在使用Laravel 5.2应用程序,用户可以在其中下载各种文件.其中之一是用户指南",它解释了网站的设置方式和功能等.我希望将PDF传输到另一页中,以便用户仍在应用程序中.我正在使用的控制器是:

Hi everyone! I'm working on a Laravel 5.2 application in which the user can download a variety of files. One of these is a 'User guide', explaining how the website is set up and functionalities etc. I would like the PDF to be streamed in another page, so that the user is still within the application. The controller which I am using is:

public function userguidePDF(){

    return response()->stream('../public/download/userguide.pdf');

}

但这返回:

参数1传递给Symfony \ Component \ HttpFoundation \ StreamedResponse :: __ construct()必须可调用,给定字符串,被调用/path/to/laravel/vendor/laravel/framework/src/Illuminate/Routing/ResponseFactory.php在第117行并已定义

Argument 1 passed to Symfony\Component\HttpFoundation\StreamedResponse::__construct() must be callable, string given, called in /path/to/laravel/vendor/laravel/framework/src/Illuminate/Routing/ResponseFactory.php on line 117 and defined

我已经在互联网上搜索了一种方法,这使我想到了以下语法:

I have searched on the internet for a method, which leaded me to the following syntax:

 return response()->stream($callback, 200, $headers);

不幸的是,由于我不了解这些参数,因此无法找到有关该参数的更多文档.有人可以向我解释一下 $ callback,200,$ header 是什么参数,如何使用呢?

Unfortunately, I am unable to find more documentation on the parameters because I don't understand them. Could someone please explain to me what the $callback, 200, $header are as parameters and how I can use this?

推荐答案

https://laravel.com/docs/5.2/responses#view-responses

public function userguidePDF() {

    return response()->file(
        public_path('download/userguide.pdf')
    );

}

这篇关于在Laravel 5.2中流式传输PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆