Wondering Coder
Wondering Coder

Reputation: 1702

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

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.

Upvotes: 38

Views: 196424

Answers (15)

Arshad Syed
Arshad Syed

Reputation: 403

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');

Upvotes: 0

justanotherguy
justanotherguy

Reputation: 526

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.

Upvotes: 0

mguven guven
mguven guven

Reputation: 91

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

Upvotes: 2

JavierMD
JavierMD

Reputation: 61

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! =)

Upvotes: 2

Wasim Khan
Wasim Khan

Reputation: 1247

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();

Upvotes: 1

Cliff Ombachi
Cliff Ombachi

Reputation: 49

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

Upvotes: 3

Santos L. Victor
Santos L. Victor

Reputation: 658

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

ob_start(); 

before the line

$pdf->Output();

Upvotes: 1

Behlum Noman
Behlum Noman

Reputation: 431

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(); 
?>

Upvotes: 42

Proyecto Pi
Proyecto Pi

Reputation: 1

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

Upvotes: 0

Victor
Victor

Reputation: 5131

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.

Upvotes: 3

raghavendra
raghavendra

Reputation: 279

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

Upvotes: 27

Andrey Aires
Andrey Aires

Reputation: 51

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.

Upvotes: 5

HansWurst
HansWurst

Reputation: 21

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)

Upvotes: 2

Cino Jose
Cino Jose

Reputation: 47

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 :-)

Upvotes: 3

Gordon Bailey
Gordon Bailey

Reputation: 3921

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.

Upvotes: 55

Related Questions