kartik-v / yii2-export

A library to export server/db data in various formats (e.g. excel, html, pdf, csv etc.)
http://demos.krajee.com/export
Other
165 stars 126 forks source link

Headers already sent on export #261

Closed dampfklon closed 5 years ago

dampfklon commented 6 years ago

Prerequisites

Steps to reproduce the issue

  1. Create Export Menu
  2. Export any Format except CSV & Text
  3. Request failes (but you get the download)

Error (always Headers already sent but depending on format different locations) :

yii\web\HeadersAlreadySentException: Headers already sent in /vendor/kartik-v/yii2-export/src/ExportMenu.php on line 771. in /vendor/yiisoft/yii2/web/Response.php:366

possibly related to https://github.com/yiisoft/yii2/issues/15782

Expected behavior and actual behavior

When I follow those steps, I see Exeption thrown

I was expecting no Error

Environment

Libraries

mgrabow1 commented 6 years ago

My temporary fix for this is to edit ExportMenu.php and change line 774: Yii::$app->end(); to exit();

It seems to do the trick, though I know that $app->end() is the proper way to terminate the application. Probably some event handlers try to send additional headers in $app->end(): https://github.com/yiisoft/yii2/blob/master/framework/base/Application.php#L638

Setting output_buffering = On also works locally, but I'm not able to set it on my hosting account.

gtaranti commented 6 years ago

The same here. version 1.3.0. I tried @mgrabow1 fix ( the exit(); ) and it works again.

kartik-v commented 6 years ago

This was changed from exit to Yii::$app->end in last release to properly terminate the app. Need to review

kartik-v commented 5 years ago

Fixed with latest release.

Alexum33Vld commented 5 years ago

Got this problem in 1.3.1 Yii 2.0.15.1.

ExportMenu used as widget in index.php view inside Pjax
<?= ExportMenu::widget([ ...

`An Error occurred while handling another error: yii\web\HeadersAlreadySentException: Headers already sent in C:...\vendor\kartik-v\yii2-export\src\ExportMenu.php on line 778. in C:\inetpub\phpsites\agencyreward\vendor\yiisoft\yii2\web\Response.php:366 Stack trace:

0 C:...vendor\yiisoft\yii2\web\Response.php(339): yii\web\Response->sendHeaders()

1 C:...vendor\yiisoft\yii2\web\ErrorHandler.php(135): yii\web\Response->send()

2 C:...vendor\yiisoft\yii2\base\ErrorHandler.php(111): yii\web\ErrorHandler->renderException(Object(yii\web\HeadersAlreadySentException))

3 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\web\HeadersAlreadySentException))

4 {main}`

esaesa commented 5 years ago

Still there is an error. The source of the event that the framwork go through the original action after the post then the gridview module handle the export.

yii\web\HeadersAlreadySentException: Headers already sent in C:....\project\vendor\kartik-v\yii2-export\src\ExportMenu.php on line 778. in C:...\project\vendor\yiisoft\yii2\web\Response.php:366 Stack trace:

0 C:...\project\vendor\yiisoft\yii2\web\Response.php(339): yii\web\Response->sendHeaders()

1 C:...\project\vendor\yiisoft\yii2\base\Application.php(656): yii\web\Response->send()

2 C:...\project\vendor\kartik-v\yii2-export\src\ExportMenu.php(781): yii\base\Application->end()

3 C:...\project\vendor\yiisoft\yii2\base\Widget.php(140): kartik\export\ExportMenu->run()

4 C:...\project\views\report\inventory.php(261): yii\base\Widget::widget(Array)

5 C:...\project\vendor\yiisoft\yii2\base\View.php(336): require('C:\vhosts\kasse...')

6 C:...\project\vendor\yiisoft\yii2\base\View.php(254): yii\base\View->renderPhpFile('C:\vhosts\kasse...', Array)

7 C:...\project\vendor\yiisoft\yii2\base\View.php(156): yii\base\View->renderFile('C:\vhosts\kasse...', Array, Object(app\controllers\ReportController))

8 C:...\project\vendor\yiisoft\yii2\base\Controller.php(384): yii\base\View->render('inventory', Array, Object(app\controllers\ReportController))

9 C:...\project\controllers\ReportController.php(60): yii\base\Controller->render('inventory', Array)

10 [internal function]: app\controllers\ReportController->actionInventory()

11 C:...\project\vendor\yiisoft\yii2\base\InlineAction.php(57): call_user_func_array(Array, Array)

12 C:...\project\vendor\yiisoft\yii2\base\Controller.php(157): yii\base\InlineAction->runWithParams(Array)

13 C:...\project\vendor\yiisoft\yii2\base\Module.php(528): yii\base\Controller->runAction('inventory', Array)

14 C:...\project\vendor\yiisoft\yii2\web\Application.php(103): yii\base\Module->runAction('report/inventor...', Array)

15 C:...\project\vendor\yiisoft\yii2\base\Application.php(386): yii\web\Application->handleRequest(Object(yii\web\Request))

16 C:...\public_html\index.php(9): yii\base\Application->run()

17 {main}

kartik-v commented 5 years ago

Update to v1.3.2 and let know

Alexum33Vld commented 5 years ago

Fix confirmed in 1.3.2, thanks kartik 👍, exit() the best.