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?

4

7 回答 7

26

只需插入 ob_end_clean(); 在输出之前。

<?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();
?>
于 2013-10-16T07:57:45.583 回答
11

我认为session.auto_start设置为 1。这将启动一个会话并向PHPSESSID浏览器发送一个 cookie。

您可以尝试使用以下代码禁用它:

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

如果设置session.auto_start为 0 不起作用,请尝试以下操作:

<?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(); 
?>
于 2013-08-11T13:38:17.333 回答
1

就我而言,我已经设置:

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

当我发出生成报告的请求时,浏览器中会显示一些警告(例如使用不推荐使用的功能)。
打开选项offdisplay_errors报告生成成功。

于 2015-02-02T13:20:10.910 回答
1

如果您在该浏览器页面上呈现其他内容后尝试生成 PDF,则会发生此错误,例如,如果您执行了以下操作:

回声$值;

FPDF 代码需要一个“空白画布”来呈现其输出(或者,有人推测,一个空白 iframe,尽管我还没有测试过)。

于 2016-06-20T09:06:32.830 回答
1

使用这样的行:

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

问题将得到解决;)

于 2015-07-31T17:24:14.663 回答
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 

错误:部分数据已经输出,无法发送PDF文件。

于 2017-04-26T15:38:52.473 回答
0

我把它放在开始(ob_clean)不改变PDF结构时解决这个问题:

require('fpdf/fpdf.php');
ob_clean();
于 2017-12-05T12:09:18.610 回答