MichaelCrowcroft's avatar

Issue with Streamed Response in Live Site

I have a site deployed with Laravel Forge. My code works on my local machine, but not on my server.

This is my code:

    public function __invoke(Application $application)
    {
        $content = view('coverLetter.show', [
            'application' => $application,
            'cover_letter' => $application->coverLetter,
        ])->render();

        return response()->streamDownload(function () use ($content) {
            echo Browsershot::html($content)
                ->margins(18, 22, 18, 22)
                ->waitUntilNetworkIdle()
                ->pdf();
            },
            auth()->user()->name . ' - ' . $application->company . ' ' . $application->job_title . ' - CoverLetter.pdf',
            ['Content-Type' => 'application/pdf']
        );

And this is my error

[2023-04-24 22:23:32] production.ERROR: Call to undefined method Symfony\Component\HttpFoundation\StreamedResponse::header() {"userId":1,"exception":"[object] (Error(code: 0): Call to undefined method Symfony\Component\HttpFoundation\StreamedResponse::header() at /home/forge/www.futurecareerpaths.com/vendor/laravel/framework/src/Illuminate/Http/Middleware/AddLinkHeadersForPreloadedAssets.php:21)
[stacktrace]
0 likes
2 replies
MichaelCrowcroft's avatar

Unsure why exactly but moving the render method into the closure fixed the issue.

sebastiansulinski's avatar
Level 3

I just came across the same problem and the cause of this was the middleware Illuminate\Http\Middleware\AddLinkHeadersForPreloadedAssets that was running on this route and wasn't really needed for this particular end point (streaming download).

What I did was add call to ->withoutMiddleware(AddLinkHeadersForPreloadedAssets::class) to this particular route i.e.:

Route::get('/export', ExportController::class)
    ->withoutMiddleware(AddLinkHeadersForPreloadedAssets::class)
    ->name('export');

This has resolved the issue for me.

1 like

Please or to participate in this conversation.