【问题标题】:TCPDF series of errors when adding an html element to generated pdf将 html 元素添加到生成的 pdf 时的 TCPDF 系列错误
【发布时间】:2018-12-01 12:15:14
【问题描述】:

我使用 TCPDF 制作了一个 pdf,并使用了 writeHTML() 函数。我有一个从 MySQL 代码生成的表,该表显示在我的网站上,该表也显示在 pdf 上,并添加了一个名为:fetch_data() 的函数。第一个表的代码如下:

if (isset($_GET['report'])) {
  $order_id = ($_GET['report']); 

  function fetch_data() {  
    $output = '';  
    require 'dbh.php'; 
    $order_id = ($_GET['report']);
    $sql = "SELECT * FROM samples_database JOIN results_database ON samples_database.sample_id = results_database.sample_id JOIN microbiology_analysis_database ON results_database.m_analysis_id = microbiology_analysis_database.id WHERE samples_database.order_id = $order_id;";
    $result = mysqli_query($conn, $sql);  
    $input = mysqli_fetch_array($result);
    $order_number = $input['order_number'][0];
    while($row = mysqli_fetch_array($result)) {       
      $output .= ' 
      <table>
      <thead>
      <tr>
      <th>Sample ID</th>
      <th>Client ID</th>
      <th>Analysis</th>
      <th>Detected</th>  
      <th>Result</th>              
      </tr>                           
      </thead>
      <tbody> 
      <tr> 
      <td><?php echo $row["env_sam_id"]; ?></td>
      <td><?php echo $row["c_sam_id"]; ?></td>
      <td><?php echo $row["m_analysis"]; ?></td>
      <td><?php echo $row["detected"]; ?></td>
      <td><?php echo $row["result"]; ?></td>                       
      </tr> 
      </tbody>
      </table>   
      ';  
    }  
    return $output;  
  } 
}

这个生成的 pdf 工作得非常好,直到在名称 fetch_data2() 下添加了一个额外的 html 表格元素,如下所示:

if (isset($_GET['report'])) {
  $order_id = ($_GET['report']); 

  function fetch_data2() {  
    $output = '';  
    require 'dbh.php'; 
    $order_id = ($_GET['report']);
    $sql = "SELECT * FROM order_database JOIN client_database ON order_database.client_id = client_database.id  WHERE order_database.id = $order_id;";
    $result = mysqli_query($conn, $sql);  
    $input = mysqli_fetch_array($result);
    $order_number = $input['order_number'];
    $time1 = $input['time1'];
    $date1 = $input['date1'];
    $client_first_name = $input['client_first_name'];
    $client_last_name = $input['client_last_name'];
    $company_name = $input['company_name'];
    $phone = $input['phone'];
    $email = $input['email'];  
    {       
      $output .= '

      <table>
      <thead>
      <tr>  
      </tr>                           
      </thead>
      <tbody> 
      <tr> 
      <td><strong>Order Number:</strong></td>
      <td><input type="text" name="date1" value="'.$order_number.'" readonly></td>                     
      </tr>  
      <tr> 
      <td><strong>Order Date:</strong></td>
      <td><input type="text" name="date1" value="'.$date1.'" readonly></td>                     
      </tr>   
      <tr> 
      <td><strong>Time Placed:</strong></td>
      <td><input type="text" name="time1" value="'.$time1.'" readonly></td>                    
      </tr>   
      <tr>
      <td><strong>Client:</strong></td> 
      <td><input type="text" name="client_name" value="'.$client_first_name.' '.$client_last_name.'" readonly></td>                    
      </tr>   
      <tr> 
      <td><strong>Company:</strong></td>
      <td><input type="text" name="company_name" value="'.$company_name.'" readonly></td>                      
      </tr>   
      <tr> 
      <td><strong>Email*:</strong></td>
      <td><input type="text" name="email" value="'.$email.'"></td>                     
      </tr>   
      <tr>
      <td><strong>Contact Number*:</strong></td> 
      <td><input type="text" name="phone" value="'.$phone.'"></td>                      
      </tr>  
      </tbody> 
      </table> 
      ';  
    }  
    return $output;  
  } 
}

这是用于生成 PDF 文件的代码:

if(isset($_POST["generate_pdf"])) {

  $file_name = 'Microbiology_Report_'.$order_number.'.pdf';  

  require_once('tcpdf/tcpdf.php');
  require_once('tcpdf/config/tcpdf_config.php'); 

// create new PDF document
  $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  $pdf->SetCreator(PDF_CREATOR);
  $pdf->SetAuthor($_SESSION['logged_in_id']);
  $pdf->SetTitle($file_name);
  $pdf->SetSubject('TCPDF Tutorial');
  $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
  $pdf->setFooterData();
  $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
  $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
  $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
  $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
  $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
  $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
  $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
    require_once(dirname(__FILE__).'/lang/eng.php');
    $pdf->setLanguageArray($l);
  }
  $pdf->setFontSubsetting(true);
  $pdf->SetFont('helvetica', '', 10, '', true);
  $pdf->AddPage();
  $html = ''; 
  $html .= fetch_data2();    
  $pdf->writeHTML($html, true, false, true, false, '');
  $html = '<h1 style="color:green;">Testing</h1>'; 
  $html .= '  
  <h4 align="center">Envirocare Microbiology Report</h4><br /> 
  <table border="1" cellspacing="0" cellpadding="3">
  <thead>
  <tr>
  <th>Sample ID</th>
  <th>Client ID</th>
  <th>Analysis</th>
  <th>Detected</th>  
  <th>Result</th>              
  </tr>                           
  </thead>
  <tbody> 
  <tr> ' ;
  $html .= fetch_data();  
  $html .= ' 
  </tr>
  </tbody>
  </table>
  ';
  $pdf->writeHTML($html, true, false, true, false, '');
  $html = '

  <h3>Abbreviations and remarks:</h3>
  <p>CFU: Colony Forming Units</p>
  ';
  $pdf->writeHTML($html, true, false, true, false, '');
  ob_end_clean();
  $pdf->Output($file_name, 'I');  
}  

我知道错误与fetch_data2() 有关,但我不知道是什么导致了错误或如何格式化代码以使其正常工作。该错误包含各种未定义的索引错误以及:TCPDF ERROR: Some data has already been output to browser, can't send PDF file。谁能帮助缓解这个问题?

编辑

这是完整的错误报告:

Notice: Undefined index: startcolumn in 

C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 19480

Notice: Undefined index: startx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 19481

Notice: Undefined index: startpage in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 19484

Notice: Undefined index: startpage in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 19487

Notice: Undefined index: in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 19487

Notice: Undefined index: startpage in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 19488

Notice: Undefined index: in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 19488

Notice: Undefined offset: -1 in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 19543

Notice: Undefined index: startx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 19740

Notice: Undefined variable: startpage in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 19741

Notice: Undefined variable: endpage in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 19741

Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18121

Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18188

Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18214

Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18269

Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18121

Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18188

Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18214

Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18269

Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18121

Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18188

Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18214

Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18269

Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18123

Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18188

Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18214

Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18269

Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18121

Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18188

Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18214

Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18269

Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18123

Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18188

Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18214

Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18269

Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18121

Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18188

Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18214

Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18269

Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18123

Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18188

Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18214

Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18269

Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18121

Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18188

Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18214

Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18269

Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18123

Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18188

Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18214

Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18269

Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18121

Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18188

Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18214

Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18269

Notice: Undefined variable: cellspacingx in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18123

Notice: Undefined variable: cellspacing in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 18188

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php:18188) in C:\xampp\htdocs\envirocare_lims\server\tcpdf\tcpdf.php on line 7625
TCPDF ERROR: Some data has already been output to browser, can't send PDF file

【问题讨论】:

  • 请分享完整的错误报告。
  • @RoAchterberg 添加了报告。
  • 如果您已将 PHP 设置为显示错误,那么这可能会阻止 TCPDF 发送所需的标头。但是无论如何您可能都想清除这些警告,但我没有看到提到的变量。如果您需要帮助修复它们,请发布所有相关代码。
  • @RoAchterberg 这是我用来生成 PDF 文件的全部代码。仅缺少数据库信息。

标签: php html pdf tcpdf


【解决方案1】:

我发现问题出在下面的代码段上。在这种情况下,我试图系统地创建一个表,其中标题位于第一列,数据库信息位于第二列。事实证明,PDF 生成器对哪个是哪个以及如何系统地将其附加到 PDF 感到困惑。

if (isset($_GET['report'])) {
  $order_id = ($_GET['report']); 

  function fetch_data2() {  
    $output = '';  
    require 'dbh.php'; 
    $order_id = ($_GET['report']);
    $sql = "SELECT * FROM order_database JOIN client_database ON order_database.client_id = client_database.id  WHERE order_database.id = $order_id;";
    $result = mysqli_query($conn, $sql);  
    $input = mysqli_fetch_array($result);
    $order_number = $input['order_number'];
    $time1 = $input['time1'];
    $date1 = $input['date1'];
    $client_first_name = $input['client_first_name'];
    $client_last_name = $input['client_last_name'];
    $company_name = $input['company_name'];
    $phone = $input['phone'];
    $email = $input['email'];  
    {       
      $output .= '

      <table>
      <thead>
      <tr>  
      </tr>                           
      </thead>
      <tbody> 
      <tr> 
      <td><strong>Order Number:</strong></td>
      <td><input type="text" name="date1" value="'.$order_number.'" readonly></td>                     
      </tr>  
      <tr> 
      <td><strong>Order Date:</strong></td>
      <td><input type="text" name="date1" value="'.$date1.'" readonly></td>                     
      </tr>   
      <tr> 
      <td><strong>Time Placed:</strong></td>
      <td><input type="text" name="time1" value="'.$time1.'" readonly></td>                    
      </tr>   
      <tr>
      <td><strong>Client:</strong></td> 
      <td><input type="text" name="client_name" value="'.$client_first_name.' '.$client_last_name.'" readonly></td>                    
      </tr>   
      <tr> 
      <td><strong>Company:</strong></td>
      <td><input type="text" name="company_name" value="'.$company_name.'" readonly></td>                      
      </tr>   
      <tr> 
      <td><strong>Email*:</strong></td>
      <td><input type="text" name="email" value="'.$email.'"></td>                     
      </tr>   
      <tr>
      <td><strong>Contact Number*:</strong></td> 
      <td><input type="text" name="phone" value="'.$phone.'"></td>                      
      </tr>  
      </tbody> 
      </table> 
      ';  
    }  
    return $output;  
  } 
} 

解决方案是创建一个传统表格,其中列标题位于 &lt;thead&gt; 标记的顶部,而实际表格行来自数据库中的 php 代码。

if (isset($_GET['report'])) {
  $order_id = ($_GET['report']); 

  function fetch_data2() {  
    $output = '';  
    require 'dbh.php'; 
    $order_id = ($_GET['report']);
    $sql = "SELECT * FROM order_database JOIN client_database ON order_database.client_id = client_database.id  WHERE order_database.id = $order_id;";
    $result = mysqli_query($conn, $sql);  

    while ($row = mysqli_fetch_array($result)) {

      $output .= '
      <td>'.$row["order_number"].'</td>  
      <td>'.$row["date1"].'</td>  
      <td>'.$row["time1"].'</td> 
      <td>'.$row["client_first_name"].' '.$row["client_last_name"].'</td>  
      <td>'.$row["company_name"].'</td>                          
      <td>'.$row["email"].'</td>  
      <td>'.$row["phone"].'</td>                         
      ';       
    }    
    return $output;  
  } 
}

最后fetch_data2()被添加到生成PDF代码中,表头添加为html代码:

$pdf->AddPage();
  $html = ''; 
  $html .= '<h1 style="color:green;">Microbiology Report</h1>'; 
  $html .= '
  <table border="1" cellspacing="0" cellpadding="3">
  <thead>
  <tr>
  <th><strong>Order Number:</strong></th>
  <th><strong>Order Date:</strong></th>
  <th><strong>Time Placed:</strong></th>
  <th><strong>Client:</strong></th>       
  <th><strong>Company:</strong></th> 
  <th><strong>Email*:</strong></th>
  <th><strong>Contact Number*:</strong></th>   
  </tr>                           
  </thead>
  <tbody> 
  <tr>
  ';
  $html .= fetch_data2();
  $html .= '
  </tr>  
  </tbody> 
  </table>
  ';
  $pdf->writeHTML($html, true, false, true, false, '');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多