Skip to content
Advertisement

Laravel streamDownload a pdf file from a guzzle request

I have setup the following method using Guzzle and Laravel’s streamDownload():

public function export(Request $request, string $uuid) 
{
    $api = $this->do_api;
    $client = new Client(['headers' => ['Authorization' => "Bearer {$this->do_token}"]]);
    return response()->streamDownload(function () use ($uuid, $api, $client) {
        $client->get("{$api}/customers/my/invoices/{$uuid}/pdf")->getBody();
    },"{$uuid}.pdf");
}

And while I have confirmed that this API does return a PDF file, the PDF being returned by laravel has 0 bytes, what am I doing wrong?

Advertisement

Answer

I was missing a echo inside the callable function:

$api = $this->do_api;
$client = new Client(['headers' => ['Authorization' => "Bearer {$this->do_token}"]]);
return response()->streamDownload(function () use ($uuid, $api, $client) {
    echo $client->get("{$api}/customers/my/invoices/{$uuid}/pdf")->getBody();
},"{$uuid}.pdf");
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement