jeudi 11 mars 2021

Force the browser to download a file that has been retrieved via an external URL in PHP/Laravel

I have been stuck on this for hours. Maybe my mind is tired. I hope someone can assist me. I am developing a Laravel application which connects to an external application that creates and edits invoices. One of my routes is supposed to allow a user to download a PDF invoice from this external application. My controller resembles the code below:

public function download(Invoice $invoice)
{
    // Creates an instance of the remote invoice
    $remoteInvoiceFile = RemoteInvoiceSoftware::find($invoice->id);

    return response()->streamDownload(function () use ($remoteInvoiceFile ) {
        // This calls the remote server and the server responds with the PDF file contents
        $file $remoteInvoiceFile ->download();

        echo $file
    }, 'file.pdf');
}

I read about the streamDownoad function from the Laravel documentation and implemented it the same way it was displayed. But I am getting an error where I am able to download a PDF file, however, not only is the file less than 5KB (the original invoice file is about 60KB), I also get an error when I try to open it. Something about the file being corrupt or not parsed well.

When I echo $remoteInvoiceFile ->download() without using the streamDownload I get something like this:

When I echo contents of the response from the invoice URL

Please help me figure what's going on and how I can fix this. Thank you!!



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3etSMC8
via IFTTT

Aucun commentaire:

Enregistrer un commentaire