TCPDF 向生成的 pdf 添加 html 元素时出现一系列错误

TCPDF series of errors when adding an html element to generated pdf

我用TCPDF做了个pdf,用了writeHTML()函数。我有一个 table 由 MySQL 代码生成,它显示在我的网站上,它也显示在 pdf 上,并添加了一个名为:fetch_data() 的函数。第一个table的代码如下:

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;  
  } 
}

在名称 fetch_data2() 下添加了一个额外的 html table 元素之前,此生成的 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;  
  } 
}

这是用于生成 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

我发现问题出在下面的代码段上。在这种情况下,我试图系统地创建一个 table,其中标题位于第一列,数据库信息位于第二列。事实证明,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;  
  } 
} 

解决方案是创建一个传统的 table,其中列标题位于 <thead> 标记的顶部,而数据库中的实际 table 行位于 [=22] =]代码。

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;  
  } 
}

最后在生成 PDF 代码中添加了 fetch_data2(),并添加了 table 头部作为 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, '');