【问题标题】:Fatal error: Some data has already been output, can't send PDF file致命错误:一些数据已经输出,无法发送PDF文件
【发布时间】:2017-09-28 21:58:03
【问题描述】:

这是我的代码

 <body>
  <section id="container" >
     <?php require 'inc/header.php';?>

    <section id="main-content">
      <section class="wrapper">
            <div class="row">
              <div class="row mt">
                <div class="col-md-12">
                  <div class="content-panel">
        <!-- 1ere ETAPE -->                 
                  <h1 style="text-align:center ;"> Rénitialisation  </h1>
                    <div class="etape1">
<?php 
require('inc/reni/fpdf.php'); 
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',10);
$pdf->Ln();
$pdf->Ln();
$pdf->SetFont('times','B',10);
$pdf->Cell(25,7,"nom");
$pdf->Cell(30,7,"prenom");
$pdf->Ln();
$pdf->Cell(450,7,"----------------------------------------------------------------------------------------------------------------------------------------------------------------------");
$pdf->Ln();

        include ('inc/reni/db.php');
       $sql="SELECT * FROM table1";
        $result = $bdd->query($sql);

        while($rows=$result->fetch())
        {
            $nom = $rows[0];
            $prenom = $rows[1];
            $pdf->Cell(25,7,$nom);
            $pdf->Cell(30,7,$prenom);

        
            $pdf->Ln(); 
        }

$pdf->Output();?>
                    </div>

              
                  </div>
                </div>
              </div><!-- /col-md-12 -->
            </div><!-- /row -->
      </section><! --/wrapper -->
    </section><!-- /MAIN CONTENT -->
  </section>
<?php require 'inc/script.php';?>

  </body>

错误是:致命错误:未捕获异常:FPDF错误:一些数据已经输出,无法发送PDF文件。 我尝试了一切,比如 add ob_end_clean(); ob_start ();ob_end_flush(); 当我这样做时,我的页面是空的,Pdf 不显示

【问题讨论】:

  • 请在此处添加您的代码而不是图片。您需要向您的页面发送任何其他内容之前输出 PDF。
  • 我只是编辑我的帖子

标签: php mysql pdf


【解决方案1】:

您尝试使用的结果是一个八位字节流,它将 PDF 文件发送到您的浏览器以下载文件。在这种情况下,您之前不能有输出。创建单个文件并输出该 PDF 文件,而无需之前编写任何内容。然后就可以下载文件了。

这就是错误消息告诉您的内容。在输出 PDF 文件之前不要输出任何内容。

【讨论】:

  • 对不起,我不明白我应该怎么做
  • 使用您的 PDF 代码制作一个单独的文件,然后将该文件输出到该新文件中,之前没有任何输出。
【解决方案2】:

你已经在输出一些东西了。

通常在某处有回声,或者你有这样的 php 代码:

<HTML>
<?php // do my pdf thing ?>

如果您的 pdf 输出前面有任何内容,无论是空格、一些 html、回显、打印,您都会收到此错误。

即使是包含在 pdf 的 php 结束标记之后的空格也会导致这种情况。

<?php 
    class PDFWriter() {
    // some code
    }
?> <-- space here

<?php include('pdfwriter.php');

这就是为什么你应该省略纯 php 文件的结束标签,以免发生这种情况。

在您的情况下,您要添加所有这些 div

<body>
  <section id="container" >
     <?php require 'inc/header.php';?>

    <section id="main-content">
      <section class="wrapper">
            <div class="row">
              <div class="row mt">
                <div class="col-md-12">
                  <div class="content-panel">
        <!-- 1ere ETAPE -->                 
                  <h1 style="text-align:center ;"> Rénitialisation  </h1>
                    <div class="etape1">

这违反了无输出发送要求。

【讨论】:

    猜你喜欢
    • 2017-01-05
    • 2020-05-15
    • 1970-01-01
    • 1970-01-01
    • 2012-03-17
    • 2021-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多