rap2hpoutre / fast-excel

🦉 Fast Excel import/export for Laravel
MIT License
2.08k stars 246 forks source link

Ability to pass HTTP headers? #321

Open liepumartins opened 1 year ago

liepumartins commented 1 year ago

Laravel Vapor requires custom headers for binary file downloads. https://docs.vapor.build/1.0/projects/development.html#binary-responses

Attempting to do

return (new FastExcel($data))->download('name.xlsx'); 

will cause an error

Fatal error: Uncaught Exception: Error encoding runtime JSON response: Malformed UTF-8 characters, possibly incorrectly encoded in /var/task/vendor/laravel/vapor-core/src/Runtime/NotifiesLambda.php:49 

I see that streamDownload() can take HTTP headers, but there is no way to pass them down from calling download(). https://github.com/rap2hpoutre/fast-excel/blob/c6d2b8b707b85f7c43028d3b4cc969388d8b6c17/src/Exportable.php#L68

Trying to extend the class and customize download method is not viable as many used methods are private.

This should work

public function download($path, callable $callback = null)
    {
        if (method_exists(response(), 'streamDownload')) {
            return response()->streamDownload(function () use ($path, $callback) {
                self::exportOrDownload($path, 'openToBrowser', $callback);
            }, null, ['X-Vapor-Base64-Encode' => 'True']);
        }
        self::exportOrDownload($path, 'openToBrowser', $callback);

        return '';
    }