KoolReport's Forum

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

Failed to load PDF document - in Yii2 #2830

Open Vijay opened this topic on on Sep 21, 2022 - 13 comments

Vijay commented on Sep 21, 2022

Hello Koolreport,

I recently implemented PDF Export in one of my Yii2 based project.

I checked whole docs, examples as well as forum topics and I see I can't resolve this issue.

Kindly check images attached.

  1. Failed to load PDF document. - when opening PDF in browser
  2. Getting a warning message in error logs (see images attached)

When Opening PDF

Error Logs

Sebastian Morales commented on Sep 26, 2022

Pls view your exported PDF with a text editor to see if there's any error message or blank spaces/lines at the beginning of the file.

Vijay commented on Sep 27, 2022

Here is the error log I see when open the PDF in Notepad++

An Error occurred while handling another error: yii\web\HeadersAlreadySentException: Headers already sent in D:\wamp\www\humhub\sw7_new\protected\modules\sw7reports\vendor\koolreport\export\File.php on line 67. in D:\wamp\www\humhub\sw7_new\protected\vendor\yiisoft\yii2\web\Response.php:373 Stack trace: #0 D:\wamp\www\humhub\sw7_new\protected\vendor\yiisoft\yii2\web\Response.php(346): yii\web\Response->sendHeaders() #1 D:\wamp\www\humhub\sw7_new\protected\vendor\yiisoft\yii2\web\ErrorHandler.php(136): yii\web\Response->send() #2 D:\wamp\www\humhub\sw7_new\protected\vendor\yiisoft\yii2\base\ErrorHandler.php(135): yii\web\ErrorHandler->ren

Sebastian Morales commented on Sep 28, 2022

It meant your app already output some data (blank spaces, lines, error messages, etc) before the pdf data. Pls remove all blank spaces, lines after all PHP ending sequence ?> in your files. Another option is to use buffering and discard it right before pdf output. For example:

//export.php or export() method
ob_start();
$report = new MyReport();
$report->run();
$handler = $report->export(...)->pdf(...);
...
ob_end_clean();
$handler->toBrowser(...);
Vijay commented on Sep 28, 2022

OK I tried this as well. and the same error there.

I check all my controllers, views, lalyouts etc. and there are no exra space character which may generate this error.

Error log when open PDF in notepad++

An Error occurred while handling another error: yii\web\HeadersAlreadySentException: Headers already sent in D:\wamp\www\humhub\sw7_new\protected\modules\sw7reports\vendor\koolreport\export\File.php on line 67. in D:\wamp\www\humhub\sw7_new\protected\vendor\yiisoft\yii2\web\Response.php:373 Stack trace: #0 D:\wamp\www\humhub\sw7_new\protected\vendor\yiisoft\yii2\web\Response.php(346): yii\web\Response->sendHeaders() #1 D:\wamp\www\humhub\sw7_new\protected\vendor\yiisoft\yii2\web\ErrorHandler.php(136): yii\web\Response->send() #2 D:\wamp\www\humhub\sw7_new\protected\vendor\yiisoft\yii2\base\ErrorHandler.php(135): yii\web\ErrorHandler->ren

And here is the error log

yii\base\InvalidArgumentException: Response content must be a string or an object implementing __toString(). in D:\wamp\www\humhub\sw7_new\protected\vendor\yiisoft\yii2\web\Response.php:1117 Stack trace: #0 D:\wamp\www\humhub\sw7_new\protected\vendor\yiisoft\yii2\web\Response.php(344): yii\web\Response->prepare() #1 D:\wamp\www\humhub\sw7_new\protected\vendor\yiisoft\yii2\base\Application.php(398): yii\web\Response->send() #2 D:\wamp\www\humhub\sw7_new\index.php(29): yii\base\Application->run() #3 {main}

KoolReport commented on Sep 29, 2022

May you post me the action of Yii that contains the export pdf.

KoolReport commented on Sep 29, 2022

One way to check the content of exporting is to do this:

Instead of calling:

$report->run()->export(..)->pdf(...)->toBrowser(..);

You do:

$report->run()->render();

It will render content to browser, you view the html content and see if any extra content (even a space) before the content of report. It may help to know where went wrong.

Vijay commented on Oct 3, 2022

ok this is what I am trying to export to PDF.

Trying to Export to PDF

Sebastian Morales commented on Oct 3, 2022

Pls send us your exported PDF file via email to support@koolreport.com / support@koolphp.net. Tks,

Vijay commented on Oct 3, 2022

OK mailed you the pdf file.

Nick Perkins commented on Apr 16

What was the issue here? I am having a An Error occurred while handling another error: yii\web\HeadersAlreadySentException: Headers already sent in, error while working with Yii2 and this product. I am not having success.

Vijay commented on Apr 17

Well the export PDF functionality of Koolreport is not working as expected!

Finally I had used this module "https://github.com/kartik-v/yii2-mpdf" to sort this out.

I have a due date to finish this functionality in time (a lot of client's pressure) so I have to take another way with another Yii2 extension

KoolReport commented on Apr 17

Yii2 framework trying to send header so we should avoid it by adding exit; at the end of Yii2 action.

Nick Perkins commented on Apr 17

Vjay this above did work. Kool Report needs to add this to its Yii2 setup docs.

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
None yet

Yii2