繁体   English   中英

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

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

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

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

给我一个错误: FPDF错误:一些数据已经输出,不能发送PDF

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

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

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

虽然这不会(注意开始<?标记之前的前导空格)

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

此外,这也不起作用( 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();
?>

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

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

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

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

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

之后进行如下更改:

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

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

在 Notepad++ 上,您应该选择菜单: Encoding ,“ Encode in UTF-8 without BOM ”。

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

嗨,您的页面顶部是否有会话标题。 或任何包含如果您尝试在页面顶部添加此代码,它应该可以正常工作。

<?

while (ob_get_level())
ob_end_clean();
header("Content-Encoding: None", true);

?>

干杯:-)

就我而言,我已经设置:

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

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

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

如果您没有得到提示什么 File & Line 发送一些内容,则您的 include / require 文件中可能存在编码不匹配。

给我

  • fpdf.php是 ANSI 编码的,
  • 我的pdf-generator.php是 UTF-8 编码的,并且
  • 我的 database-connect-inlude 是 UTF-8 编码的(这个 UTF-8 编码确实引发了 FPDF 错误。我不得不将它切换回 ANSI)

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

ob_start(); 

行前

$pdf->Output();

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

添加到脚本的开头

ob_start();
require ('fpdf.php');

最后,在output()之后

ob_end_flush();

它对我有用! =)

我使用了以下内容,它对我有用

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

你需要打电话给图书馆

要求('fpdf.php');

<?php
require('fpdf.php');

$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'¡Hola, Mundo!');
$pdf->Output();
?>

http://www.fpdf.org/

http://www.fpdf.org/es/tutorial/tuto1.htm

在/home/asri123/public_html/bill/invoice/fpdf.php:271 堆栈跟踪:#0 /home/asri123/public_html/bill/invoice/fpdf.php(1052): FPDF->Error('一些数据有一个。 ..') #1 /home/asri123/public_html/bill/invoice/fpdf.php(1012): FPDF->_checkoutput() #2 /home/asri123/public_html/bill/invoice/mirasbill.php(262): FPDF->Output('MSFS/2018-19/76...', 'D') #3 {main} 在第 271 行的 /home/asri123/public_html/bill/invoice/fpdf.php 中抛出

即使包含的 php 文件中的单个空格也会导致该警告。 无论如何都不应该有任何输出。

没有其他人在此处发布的另一个答案...仔细检查您的 PHP 文件的编码,并确保它不是 UTF-8 以外的东西。 错误的代码编辑器(或 FTP 上传?)可能会弄乱文件的编码,在这种情况下,此线程中提到的其他修复都无济于事。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM