php FPDF错误:部分数据已经输出,无法发送PDF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9475686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:54:37  来源:igfitidea点击:

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

phpdrupalfpdf

提问by Wondering Coder

I am using the fpdflibrary for my project, and I'm using this to extend one of the drupal module. These lines

我正在为我的项目使用fpdf库,并且我正在使用它来扩展 drupal 模块之一。这些线路

$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

给我一个错误:FPDF错误:一些数据已经输出,无法发送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.

我尝试在 drupal 区域名称 test.php 之外的单独文件中创建它,并且在查看时它起作用了。这里有人知道为什么这不起作用吗?或者这里的任何人都可以向我指出一个正确的 pdf 库,我可以在 drupal 中使用它来查看 HTML 到 PDF 格式。

回答by Gordon Bailey

For fpdf to work properly, there cannot be anyoutput at all beside what fpdf generates. For example, this will work:

为了让 fpdf 正常工作,除了 fpdf 生成的内容之外,根本没有任何输出。例如,这将起作用:

<?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 echowill break it):

此外,这也不起作用(echo会破坏它):

<?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.

我不确定事物的 drupal 方面,但我知道绝对零非 fpdf 输出是 fpdf 工作的要求。

回答by Behlum Noman

add ob_start ();at the top and at the end add ob_end_flush();

添加ob_start ();在顶部和最后添加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(); 
?>

回答by raghavendra

give me an error as below:
FPDF error: Some data has already been output, can't send PDF

给我一个错误如下:
FPDF error: Some data has already been output, can't send PDF

to over come this error: go to fpdf.phpin that,goto line number 996

克服这个错误:转到fpdf.php那个,转到第 996 行

function Output($name='', $dest='')

after that make changes like this:

之后进行如下更改:

function Output($name='', $dest='') {   
    ob_clean();     //Output PDF to so

回答by Andrey Aires

Try to save the file without the option: "BOM comment", i.e. in Adobe Dreamweaver, you Save File As..., uncheckthe box below the filename that says, "Include Unicode signature(BOM)".

尝试在没有选项的情况下保存文件:“BOM 注释”,即在 Adob​​e Dreamweaver 中,您将文件另存为...,取消选中文件名下方的“包含 Unicode 签名(BOM)”框。

On Notepad++ you should select the menu: Encoding, "Encode in UTF-8 without BOM".

在 Notepad++ 上,您应该选择菜单: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.

并将其设置为您创建的其他文件的默认值,它会在将来为您省去很多麻烦。

回答by Cino Jose

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

欢呼:-)

回答by HansWurst

The FPDF Error Message will point you to the PHP Line that is sending some content.

FPDF 错误消息会将您指向发送某些内容的 PHP 行。

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.phpwas ANSI-encoded,
  • my pdf-generator.phpwas 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)
  • fpdf.php是ANSI编码的,
  • mypdf-generator.php是 UTF-8 编码的,并且
  • 我的数据库连接插入是 UTF-8 编码的(这种 UTF-8 编码确实引发了 FPDF 错误。我不得不将其切换回 ANSI)

回答by Santos L. Victor

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

第一步检查文件夹的权限第二步把这个

ob_start(); 

before the line

行前

$pdf->Output();

回答by Cliff Ombachi

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

如果您在生成 PDF 之前代码输出通知/警告,请尝试将它们关闭。error_reporting(0). 然后在之后处理警告

回答by Victor

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 offthe display_errorsoption, the report was generated successfully.

当我提出生成报告的请求时,浏览器中显示了一些警告(如使用已弃用的功能)。
谈到offdisplay_errors选项后,已成功生成报告。

回答by Wasim Khan

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