FPDF error: Some data has already been output, can't send PDF
Asked Answered
M

16

38

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.

Mcconnell answered 28/2, 2012 at 2:42 Comment(0)
M
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.

Melise answered 28/2, 2012 at 2:59 Comment(7)
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.Mcconnell
btw - i don't use short tags in php. used this <?php ?>Mcconnell
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.Melise
@GordonBailey What would be approach in using fpdf in PHP MVC frameworks, for instance in phalconphp, but conceptual answer for MVC is very welcomeInterfere
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.Deane
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 "<?php " tag and my problem is resolved.Looby
echo was a problem in my case! Thank youLydie
E
42

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(); 
?>
Ervin answered 18/6, 2015 at 6:28 Comment(2)
This is the answer which solved my problem great workLowrey
This is the solution of error FPDF error: Some data has already been output, can't send PDF.Nolde
C
26

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
Connor answered 19/10, 2012 at 10:46 Comment(6)
I up-checked this answer because it works, particularly in cases where you have spaces or newlines outside of your <?php ?> 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.Deane
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 981Trave
This is working perfeclty for me. upvote from my side.Manometer
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.Kinshasa
Worked flawlessly for me. I was having this issue when trying to compare two strings.Venereal
I'm using this library: github.com/myokyawhtun/PDFMerger and this worked for me. Instead, I modified /tcpdf/tcpdf.phpPeper
R
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.

Runnel answered 3/10, 2012 at 17:58 Comment(0)
W
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 :-)

Watchmaker answered 5/6, 2012 at 1:30 Comment(0)
D
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.

Dang answered 2/2, 2015 at 13:20 Comment(0)
G
3

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

Guerrilla answered 18/4, 2016 at 14:2 Comment(1)
This should be posted as a comment.Intinction
C
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)
Consensus answered 10/9, 2012 at 11:49 Comment(0)
P
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! =)

Protozoon answered 9/12, 2019 at 5:42 Comment(2)
this error showing today. its working from last 3 year and today not workSludge
This made the trick, but ensure you don't have divs or other html stuff (even blank spaces), otherwise will silently fail.Diaphanous
V
2

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

Vacuum answered 2/8, 2020 at 10:35 Comment(0)
I
1

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

ob_start(); 

before the line

$pdf->Output();
Inflationary answered 8/3, 2016 at 14:7 Comment(0)
T
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();
Trave answered 4/8, 2017 at 14:25 Comment(0)
T
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

Tejeda answered 11/3, 2015 at 17:59 Comment(0)
S
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

Sludge answered 20/5, 2020 at 4:56 Comment(1)
its working from last 3 year but today its stop on all sites please help meSludge
M
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.

Manufacturer answered 5/11, 2020 at 18:41 Comment(0)
C
0

The error you encountered, FPDF error: Some data has already been output, can't send PDF file, typically occurs when there is some output (even a blank space or newline) sent to the browser before the PDF generation. In PHP, when generating files for download, it's crucial to ensure no output is sent before the file headers are set.

Here’s how you can resolve this issue: Step 1: Ensure No Prior Output

Make sure no whitespace or any other output is sent before generating the PDF. This includes any spaces or newlines before the opening tag in any included files.

    // Create a new PDF
    $pdf = new FPDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial', 'B', 16);

    // Title
    $pdf->Cell(0, 10, 'My Data', 0, 1, 'C');
    $pdf->Ln(10);
    $pdf->Cell(40, 10, 'Name ');
    $pdf->Ln(10);

    // Clear any previous output
    ob_end_clean();
    
    // Output PDF
    $pdf->Output('D', 'file.pdf');
Chamberlain answered 27/5 at 2:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.