0

Hi i am developing an application using laravel 4 and when i try to download my report in csv format it inserts 4 new empty line before header and i want to get rid of those empty lines.

Here is my code

public function getDownloadCsv()
    {

        // Grab all the assets
        $assettypes = Assettype::orderBy('id', 'ASC')->select('id', 'asset_type')->get();


        $rows = array();

        // Asset Type Id
        $header = array(
            Lang::get('admin/assetdetails/table.assetnum'),
            Lang::get('admin/assetdetails/table.asset_name'),

        );
        $header = array_map('trim', $header);
        $rows[] = implode($header,',');

        // Create a row per asset
        foreach ($assettypes as $assettype) {
            $row = array();
            $row[] = $assettype->id;
            $row[] = $assettype->asset_type;
            $rows[] = implode($row,',');
        }

          // spit out a csv
         $csv = implode($rows,("\n"));
         $response = Response::make($csv, 200);
         $response->header('Content-Type', 'text/csv');
         $response->header('Content-disposition', 'attachment;filename=Help_document_for_asset_import.csv');
         return $response;
        } 

If you need any further details please let me know. Please help me to achieve this....

Arul
  • 185
  • 1
  • 12
  • 1
    Empty lines are always a pain in the neck. Check this one first: http://stackoverflow.com/questions/11728791/laravel-displays-an-empty-line-before-the-doctype – Adrenaxus Dec 30 '14 at 11:01
  • @Adrenaxus yes i saw that but i dont have any php tag in my code. – Arul Dec 30 '14 at 11:13
  • @Adrenaxus thanks.I checked all the files and in one of the file i had empty lines and i removed that space,now its working fine. Thanks for your valuable answer. – Arul Jan 05 '15 at 13:41
  • You're welcome. Please answer your own question and mark is at accepted for future reference. Cheers – Adrenaxus Jan 05 '15 at 15:36

2 Answers2

1

Finally i solved the issue, thanks to Adrenaxus valuable comments all i did is deleted all the empty lines after closing php tag.Please note that you must do this in all php files in your application.

In my case i checked the current file which i was working and failed to replace the empty lines in other php files but the issue was still happening,removing empty spaces in all php files did the trick.

Arul
  • 185
  • 1
  • 12
0

I did solve this problem by using function ob_clean(), You may also need to use ob_start() OR ob_end_clean() depending on your code.