5

I am using FPDF class to generate a pdf on my website. Everything worked well until last few weeks when I started getting error:

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

During last few weeks haven't change anything in my code and I have also checked for any output execpt the fpdf (including unecessary space before php, disabled BOM signature etc.)

I have my website on 000webhost.com so I have also disabled the analytic code at the end of the page, but the pdf still doesn't work. The only trace I have left is misterious "" in a source code (I can see it when checking source code in Chrome browser).

I cant get to work even this simple example:

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

Is there a way to disable any other output on web page by php? or does someone use fpdf on 000webhost?

2
  • fpdf.php is standard class file from www.fpdf.org, didn't mess with it Aug 11, 2013 at 13:40
  • In that case, "" implies that your host is doing something goofy. I'd switch to a better host.
    – Dave
    Aug 11, 2013 at 13:42

7 Answers 7

26

just insert ob_end_clean(); before outputing.

<?php
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage()
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
ob_end_clean();
$pdf->Output();
?>
4
  • I was testing XAMPP's CD Collection sample app. This worked, thanks.
    – k_rollo
    Jan 21, 2015 at 18:08
  • Can I replace the string "Hello World" with a variable. I have a lots of string to be added there. Jan 24, 2015 at 13:10
  • ob_end_flush(); ob_end_clean(); worked for me, yes, both of them! Oct 7, 2016 at 21:59
  • Answer of the year for me
    – Humphrey
    Aug 1, 2019 at 16:40
12

I think that session.auto_start is set to 1. This will start a session and send a PHPSESSID cookie to the browser.

You can try to disable it using the following code:

<?php
ini_set("session.auto_start", 0);
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage()
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
?>

In case setting session.auto_start to 0 does not work, then try this:

<?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(); 
?>
4
  • surely setting it within the script will be too late? If this is the problem, this needs to be done in php.ini
    – Dave
    Aug 11, 2013 at 13:40
  • also (while this could be the issue) it doesn't explain an output of "".
    – Dave
    Aug 11, 2013 at 13:43
  • true but the manual claims it's a PHP_INI_ALL (Entry can be set anywhere) so it should work, if it does not work the manual is incorrect about this setting Aug 11, 2013 at 13:46
  • 1
    ob_end_flush(); ob_end_clean(); worked for me, yes, both of them! Oct 7, 2016 at 21:58
1

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.

1

Use line like this:

require('fpdf.php');    ob_end_clean();    header("Content-Encoding: None", true);

Issue will resolved ;)

1

This error will occur if you try to generate the PDF after you have already rendered something else on that browser page, for example, if you have done something like this:

echo $value;

The FPDF code wants a "blank canvas" to render its output on (or, one surmises, a blank iframe, although I haven't tested that yet).

0
SELECT motivo, 
       unidad_trans, 
       km_inicial, 
       km_final, 
       rut_chofer, 
       To_char(hora_inicial, 'DD/MM/YYYY HH:mm'), 
       To_char(hora_final, 'DD/MM/YYYY HH:mm'), 
       total_recorrido, 
       destino, 
       cod_combustible, 
       cantidad_litros, 
       cod_vehiculo, 
       d.cod_estableci 
FROM   mov_bitacora b, 
       mov_chofer c, 
       nuc_dependencias d, 
       mov_combustible co, 
       mov_vehiculo v 
WHERE  b.unidad_tran = d.cod_estableci 
       AND b.rut_chofer = c.rut_chofer 
       AND b.cod_combustible = co.cod_combustible 
       AND b.cod_vehiculo = v.cod_vehiculo 
       AND id_bitacora = 6fpdf 

Error: Some data has already been output, can't send PDF file.

0

I putted this to fix this problem in the beggining (ob_clean) doesn't change PDF structure:

require('fpdf/fpdf.php');
ob_clean();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.