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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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");
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0