TCPDF & mPDF error: Some data has already been output to browser, can't send PDF file
Asked Answered
N

8

7

The Problem:

TCPDF & mPDF error: Some data has already been output to browser, can't send PDF file I gave up on trying to fix the error with TCPDF and installed mPDF only to get the same error when attempting to render the document to the browser. I can save the document just fine and have it displayed in the browser upon retrieval.

Additionally, this error only presented itself after switching from my dev server to my host server. Works fine on DEV server (DEV server = WAMPSERVER, PROD server = Hostgator Linux).

Troubleshooting:

I've read the many volumes of other discussions around the internet regarding this problem and I can find no white space related issue. I have condensed the request to the following:

<?php
ob_start(); 
$html = "Hello World";
include("../mpdf.php");
$mpdf=new mPDF(); 
$mpdf->WriteHTML($html);
$mpdf->Output();
ob_end_clean();
?>

Tried the same concept with TCPDF using ob_clean() method before writeHtml. Same error in all cases (I can assure everyone this is no white space related issue - I even viewed the file in hex to make sure there were no odd characters being inserted by the editor).

Possible Clue:

I was finally able to get a clue as to what's going on when I moved the entire mPDF library and classes and folders to the public_html folder, rather than from inside my application folder (a symfony project). Under this scenario, when I pointed my browser to the example pages, it rendered just fine with no errors at all (and it was super fast btw). So, I know it works, and I know there is no white-space related issue, or any other related issue, regarding the code or installation (on the mPDF/TCPDF side of things). Which leads me to believe either symfony is inserting headers of some sort (which I tried removing using: clearHttpHeaders() ), or there is a PHP INI or CONFIG setting I am missing somewhere on the PROD server.

Does anyone have ANY idea of what's going on here??

Update: stream dump:

Request URL:http://www.example.com/mpdf
Request Method:GET
Status Code:200 OK

Request Headers
GET /mpdf HTTP/1.1
Host: www.example.com
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Cookie: __utma=44708724.1463191694.1383759419.1383759419.1383765151.2; __utmz=44708724.1383759419.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); PHPSESSID=9c7c802200b9d8eefe718447755add5f; __utma=1.813547483.1383767260.1385127878.1385130071.38; __utmb=1.7.10.1385130071; __utmc=1; __utmz=1.1383767260.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)

Response Headers
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:Keep-Alive
Content-Type:text/html
Date:Fri, 22 Nov 2013 14:59:52 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive:timeout=5, max=75
Pragma:no-cache
Server:Apache
Transfer-Encoding:chunked

Nothing is jumping out at me... any other thoughts?

Nigel answered 22/11, 2013 at 14:13 Comment(5)
Have you dumped the stream (as seen by the client) and looked at it?Passing
No. I plead ignorance. I don't know how to dump a stream. I'd be happy to though if I was pointed in the right direction.Nigel
Best thing is to use Firefox, install the Firebug extension, enable the network module for this site in Firebug - this will show you byte-per-byte, what your client receives ... including whitespace, newlines etc. My Tip: The culprit will be \r - ignored by windows, but not by LinuxPassing
Umm...are the same to my problem. I try to remove my css styling and it work. Any suggest?Saum
Any solution found...?Holism
I
5

Most probably it's BOM marker, use your IDE to remove it, other hot fix can be:

<?php

$html = "Hello World";
include("../mpdf.php");

ob_clean(); // cleaning the buffer before Output()

$mpdf=new mPDF(); 
$mpdf->WriteHTML($html);
$mpdf->Output();

?>
Ioves answered 23/11, 2013 at 10:36 Comment(1)
Thanks, but I thought I mentioned that I tried both of these fixes (looked at code in hex to try and find BOM marker, and that I tried all iterations of ob_clean()). Any other ideas?Nigel
K
4

I have got the same error.

Solve this using ob_start(); and ob_end_clean();

PHP is an interpreted language thus each statement is executed one after another, therefore PHP tends to send HTML to browsers in chunks thus reducing performance. Using output buffering the generated HTML gets stored in a buffer or a string variable and is sent to the buffer to render after the execution of the last statement in the PHP script.

But Output Buffering is not enabled by default. In order to enable the Output Buffering one must use the ob_start() function before any echoing any HTML content in a script.

[reference credit][1]

[PHP | ob_start() Function][2]

public function gen_pdf($html, $user_id, $paper = 'A4') {
        
        //Enables Output Buffering
        ob_start();
        $mpdf = new mPDF('UTF-8', $paper, '', '', 15, 15, 30, 20, 15, 5);
        $mpdf->mirrorMargins = 1; // Use different Odd/Even headers and footers and mirror margins

        $header = '';
        $footer = '';
        
        $mpdf->SetHTMLHeader($header);
        $mpdf->SetHTMLFooter($footer);

        $mpdf->SetWatermarkText('Watermark', 0.1);
        $mpdf->showWatermarkText = true;
        $mpdf->WriteHTML($html);
        $fileName = date('Y_m_d_H_i_s');
        ob_end_clean();//End Output Buffering
        $mpdf->Output('Example_' . $fileName . '.pdf', 'I');
    }

So that it will clear all buffered output before processing mPDF.

Best Luck... [1]: https://www.geeksforgeeks.org/php-ob_start-function/ [2]: https://www.php.net/manual/en/function.ob-start.php

Kulturkampf answered 20/10, 2020 at 5:51 Comment(0)
S
2

It could be some warning issued from PHP before the pdf->output. The warning text is sent to the client's browser and thus the file cannot be sent.
If your warning level is not the same for DEV and PROD, that could explain the difference of behavior.

In my case, with TCPDF, I had many warnings such as "date() it is not safe to rely on the system's timezone settings...", then the error "Some data has already been output to browser, can't send PDF".
Adding the function date_default_timezone_set() in my php source code solved the warnings and the error.

Shroff answered 10/4, 2014 at 23:56 Comment(1)
This was helpful. Had the same issue, turned out that it was b/c I left error reporting on (until I finish configuring the new server). ThanksTauromachy
R
1

I have the same issue, and add this line before $pdf->output():

error_reporting(E_ALL);

An then I found that I have BOM on some files. And I see a Warning message sent to the browser.

Best Luck !!

Regards

Romona answered 2/9, 2014 at 20:36 Comment(0)
C
0

May be it occurs because of in your result of HTML code have some error to output to create the TCPDF ...

OR

If above is not work try set the Charset as UTF-8 in class file of TCPDF may it solve your issue...

Because this type of error was happening in my project before one week ago ...

Cashmere answered 10/9, 2014 at 11:11 Comment(0)
A
0

Remove any file you would have included at the start of the page. In my case it was a file that was connecting with database. It worked for me. (Tip from @Nicolas400 )

Anemograph answered 17/10, 2016 at 8:25 Comment(0)
D
0

Try using ob_clean(); before include("../mpdf.php");.

Dialyse answered 23/1, 2018 at 12:4 Comment(0)
S
0

I have got the same error.

Data has already been sent to output, unable to output PDF file

This means before creating pdf with mPDF some data is stored in the buffer which is sended to the browser. Therefore it is unable to create PDF.

Just do this.. Add this below php built-in function at the first line of your page were you are preparing data for pdf.

op_start();

And add this below php built-in function before mPDF code (before where you are calling mpdf)

ob_end_flush();

require_once __DIR__ . '/vendor/autoload.php';

$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML($html);
$mpdf->Output();

So that it will clear all buffer output before processing mPDF.

Subscribe answered 10/9, 2020 at 6:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.