KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

Ob_clean() in Koolreport Excel #2715

Open Suvindran opened this topic on on Jun 9, 2022 - 3 comments

Suvindran commented on Jun 9, 2022

Recently there was a problem with the EXPORT EXCEL function where the file corrupts due to blank space in the first row. For those using Laravel Framework, run the following codes in Controller instead of opening a new PHP/Blade file which will cause a blank space in the first row:

The following method works very well !!

//ReportsController

use App\Reports\MyReport;

 public function ExportExcel()
    {
        ob_start();
        $report = new MyReport;
        $report->run();
        ob_end_clean();
        $report->exportToExcel('MyReportExcel')->toBrowser("MyReport.xlsx");
}

//Web.php

Route::get('ExportExcel', [ReportsController::class, 'ExportExcel']);

//Button

<form>
<button type="submit" class="btn btn-primary" formaction="/ExportExcel">Download Excel</button>
</form>

// Additional steps i have added ob_clean(); in LaravelProject/vendor/koolreport/excel/FileHandler.php line 71 (Not Safe)

    public function toBrowser($filename, $openOnBrowser = false)
    {
      
        // ignore_user_abort(true);
        $disposition = "attachment";
        if (gettype($openOnBrowser) == "string") {
            $disposition = $openOnBrowser;
        } else if ($openOnBrowser) {
            $disposition = "inline";
        }

        $source = realpath($this->path);

      
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header("Content-Type: " . $this->mime_type($filename));
        header("Content-Disposition: $disposition; filename=\"$filename\"");
        header("Content-Transfer-Encoding: binary");
        header("Content-Length: " . filesize($source));

        ob_clean(); // Added this which prevents blank space and first row

        readfile($this->path);

        return $this;
    }

This might be helpful for those stuck with the Excel Export function.

Please suggest to me if there is a better way to prevent this issue because modifying the core files inside the vendor is not safe. :(

Thanks!

Sebastian Morales commented on Jun 9, 2022

Pls try to remove PHP ending ?> at the end of your view/blade files. Any blank/new line character after ?> is automatically output, which could corrupt excel/pdf file export. Rgds,

Suvindran commented on Jun 10, 2022

I have removed ?> at the end of blade file and still the same. The file was corrupted after download.

Sebastian Morales commented on Jun 10, 2022

Pls try this example on your dev machine to see if there's any blank/new line at the beginning of the exported excel file:

https://www.koolreport.com/examples/reports/excel/excel_template/

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
help needed
suggestion

None