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

2021-12-29 00:00:00 fpdf php drupal

我正在为我的项目使用 fpdf 库,并且我正在使用它来扩展其中一个Drupal 模块.这些行

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

给我一​​个错误:FPDF错误:部分数据已经输出,无法发送PDF

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

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

推荐答案

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

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

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

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

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

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.

相关文章