Ряд ошибок TCPDF при добавлении html-элемента в сгенерированный pdf - PullRequest
0 голосов
/ 01 декабря 2018

Я сделал PDF с использованием TCPDF и использовал функцию 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 работал очень хорошо, пока не был добавлен дополнительный элемент таблицы HTML под именем fetch_data2(), который выглядит следующим образом:

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

1 Ответ

0 голосов
/ 02 декабря 2018

Я обнаружил, что проблема заключается в сегменте кода ниже.В этом сценарии я пытался систематически создать таблицу, где заголовки были в первом столбце, а информация базы данных - во втором столбце.Оказывается, что генератор 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;  
  } 
} 

Решением было создание традиционной таблицы, где заголовки столбцов находятся вверху в <thead> тег и фактические строки таблицы из базы данных в коде 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, '');
...