Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

fpdf

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?

like image 688
user1857756 Avatar asked Aug 11 '13 13:08

user1857756


4 Answers

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();
?>
like image 85
user1825831 Avatar answered Nov 08 '22 14:11

user1825831


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(); 
?>
like image 45
Raymond Nijland Avatar answered Nov 08 '22 14:11

Raymond Nijland


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.

like image 1
Victor Avatar answered Nov 08 '22 15:11

Victor


Use line like this:

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

Issue will resolved ;)

like image 1
abid saleem Avatar answered Nov 08 '22 14:11

abid saleem