36

I am using the fpdf library for my project, and I'm using this to extend one of the drupal module. These lines

$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();

give me an error: FPDF error: Some data has already been output, can't send PDF

I tried creating this in a separate file outside the drupal area name test.php and when viewed it worked. Anyone here know why this don't work? Or anyone here can point me a right pdf library which I can use in drupal to view HTML to PDF format.

Christian Neverdal
  • 5,655
  • 6
  • 38
  • 93
Wondering Coder
  • 1,652
  • 9
  • 31
  • 51

15 Answers15

55

For fpdf to work properly, there cannot be any output at all beside what fpdf generates. For example, this will work:

<?php
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
?>

While this will not (note the leading space before the opening <? tag)

 <?php
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
?>

Also, this will not work either (the echo will break it):

<?php
echo "About to create pdf";
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
?>

I'm not sure about the drupal side of things, but I know that absolutely zero non-fpdf output is a requirement for fpdf to work.

Gordon Bailey
  • 3,881
  • 20
  • 28
  • 1
    oww ok, because i just inserted the code inside .tpl files. It has blocks of codes that uses echo/print, or html tags and javascript syntax. – Wondering Coder Feb 28 '12 at 03:52
  • btw - i don't use short tags in php. used this – Wondering Coder Feb 28 '12 at 03:53
  • 2
    Yep. that's your problem. Using `echo` will also break fpdf (I'll edit my answer to show that). The short tags vs. long tags shouldn't make a difference, but you'll have to make sure that there is not even a single character outside of your php tags. – Gordon Bailey Feb 28 '12 at 03:59
  • @GordonBailey What would be approach in using fpdf in PHP MVC frameworks, for instance in phalconphp, but conceptual answer for MVC is very welcome – eomeroff Nov 25 '13 at 23:45
  • REMOVE the closing "?>" at the end of each PHP file, particularly any PHP file that might be "included" in the code. The very existence of the "?>" means there might be a "\r\n" EOL afterwards, depending on your editor, and that "\r\n" will be sent in the output Response and contaminate your PDF. – UncaAlby Nov 10 '16 at 23:18
  • Thank you @gordeon i have same problem is happened in mpdf but localhost give pdf output but server give not output then i remove the white space before the " – Bhavin Thummar Jun 23 '17 at 12:10
  • `echo` was a problem in my case! Thank you – Zaheer May 31 '19 at 23:45
41

add ob_start (); at the top and at the end add ob_end_flush();

<?php
    ob_start();
    require('fpdf.php');
    $pdf = new FPDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',16);
    $pdf->Cell(40,10,'Hello World!');
    $pdf->Output();
    ob_end_flush(); 
?>
bummi
  • 27,123
  • 14
  • 62
  • 101
Behlum Noman
  • 421
  • 4
  • 5
25

give me an error as below:
FPDF error: Some data has already been output, can't send PDF

to over come this error: go to fpdf.php in that,goto line number 996

function Output($name='', $dest='')

after that make changes like this:

function Output($name='', $dest='') {   
    ob_clean();     //Output PDF to so
Prasad Jadhav
  • 5,090
  • 16
  • 62
  • 80
raghavendra
  • 259
  • 3
  • 2
  • I up-checked this answer because it works, particularly in cases where you have spaces or newlines outside of your tags, and can not find them all. It might be safer to call "ob_clean" from the calling function, instead of inside the "Output" function. Be warned, that it will also hide any legitimate error messages that might actually be causing the problem. If you fix the errors, which you ought to do anyway, that might solve your problem without resorting to "hacks" like this. – UncaAlby Oct 25 '16 at 21:46
  • I used that but it shows me - Notice: ob_clean(): failed to delete buffer. No buffer to delete in /var/www/html/-------/fpdf.php on line 981 – Wasim Khan Aug 04 '17 at 13:52
  • This is working perfeclty for me. upvote from my side. – Naren Verma Nov 06 '17 at 08:48
  • I picked up the error noted by the OP after moving files to a new server. No changes, just updating from Windows Server 2012 to 2016. Adding ob_clean(); solved the problem. – Burndog Jan 02 '18 at 18:24
  • Worked flawlessly for me. I was having this issue when trying to compare two strings. – Keno May 07 '18 at 04:06
  • I'm using this library: https://github.com/myokyawhtun/PDFMerger and this worked for me. Instead, I modified `/tcpdf/tcpdf.php` – mike Mar 26 '19 at 17:27
5

Try to save the file without the option: "BOM comment", i.e. in Adobe Dreamweaver, you Save File As..., uncheck the box below the filename that says, "Include Unicode signature(BOM)".

On Notepad++ you should select the menu: Encoding, "Encode in UTF-8 without BOM".

And make it default for other files you create, it will spare you a lot of headaches in future.

Sebass van Boxel
  • 2,514
  • 1
  • 22
  • 37
Andrey Aires
  • 51
  • 1
  • 1
3

Hi do you have a session header on the top of your page. or any includes If you have then try to add this codes on top pf your page it should works fine.

<?

while (ob_get_level())
ob_end_clean();
header("Content-Encoding: None", true);

?>

cheers :-)

Cino Jose
  • 47
  • 1
3

In my case i had set:

ini_set('display_errors', 'on');
error_reporting(E_ALL | E_STRICT);

When i made the request to generate the report, some warnings were displayed in the browser (like the usage of deprecated functions).
Turning off the display_errors option, the report was generated successfully.

Victor
  • 5,043
  • 3
  • 41
  • 55
2

The FPDF Error Message will point you to the PHP Line that is sending some content.

If you get no hint what File & Line send some content you probably have an encoding mismatch in your include / require Files.

For me

  • fpdf.php was ANSI-encoded,
  • my pdf-generator.php was UTF-8-encoded and
  • my database-connect-inlude was UTF-8-encoded (this UTF-8-encoding did raise the FPDF error. I had to switch it back to ANSI)
Santosh Kumar
  • 26,475
  • 20
  • 67
  • 118
HansWurst
  • 21
  • 1
2

if you're code outputs notices/warnings before the PDF generation, try turning them off. error_reporting(0). Then work on the warnings there-after

2

Add to the beginning of the script

ob_start();
require ('fpdf.php');

and at the end, after output()

ob_end_flush();

It worked for me! =)

JavierMD
  • 61
  • 5
  • this error showing today. its working from last 3 year and today not work – Rajesh May 20 '20 at 04:57
  • This made the trick, but ensure you don't have divs or other html stuff (even blank spaces), otherwise will silently fail. – m3nda Aug 16 '21 at 20:09
2

Even a single space in the included php files causes that warning. There shouldn't be any output in any way.

mguven guven
  • 91
  • 1
  • 5
1

First step check the permissions on the folders second step put this

ob_start(); 

before the line

$pdf->Output();
Santos L. Victor
  • 628
  • 8
  • 13
1

I used the following and it worked for me

require_once ('pdf/fpdf.php');

$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output(F,'/var/www/html/PATH/filename.pdf');
ob_end_flush();
Wasim Khan
  • 1,177
  • 12
  • 13
0

You need to call the library

require ('fpdf.php');

<?php
require('fpdf.php');

$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'¡Hola, Mundo!');
$pdf->Output();
?>

http://www.fpdf.org/

http://www.fpdf.org/es/tutorial/tuto1.htm

dario
  • 5,149
  • 12
  • 28
  • 32
0

Fatal error: Uncaught exception 'Exception' with message 'FPDF error: Some data has already been output, can't send PDF file (output started at /home/asri123/public_html/bill/invoice/invoice.php:743)' in /home/asri123/public_html/bill/invoice/fpdf.php:271 Stack trace: #0 /home/asri123/public_html/bill/invoice/fpdf.php(1052): FPDF->Error('Some data has a...') #1 /home/asri123/public_html/bill/invoice/fpdf.php(1012): FPDF->_checkoutput() #2 /home/asri123/public_html/bill/invoice/mirasbill.php(262): FPDF->Output('MSFS/2018-19/76...', 'D') #3 {main} thrown in /home/asri123/public_html/bill/invoice/fpdf.php on line 271

Rajesh
  • 77
  • 1
  • 1
  • 7
0

Another answer that nobody else has posted here... Double-check the encoding of your PHP file and make sure that it's not something other than UTF-8. The wrong code editor (or FTP upload?) can mess with the file's encoding in which case none of the other fixes mentioned in this thread will help.

justanotherguy
  • 506
  • 2
  • 4
  • 18