Anna Jeanine Anna Jeanine - 11 days ago 7
PHP Question

Stream a PDF file in Laravel 5.2

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');

}


But this returns:


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);


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?

Answer

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

public function userguidePDF() {

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

}