TCPDF ERROR: Some data has already been output, can not send PDF file

1

I am trying to generate a PDF report using TCPDF , but when executing my code the following error occurs:

  

TCPDF ERROR: Some data has already been output, can not send PDF file

Please, can you give me a hand to determine the error, generate the PDF and manage to print it ?. Of course, I was very grateful.

The code I am using is the following:

index1.php

<?php  
 function busca_datos()  
 {  
      $output = '';  
      $connect = mysqli_connect("localhost", "usuario", "password", "personal");  
      $sql = "SELECT id_cliente, cedula, nombres, tel, dir FROM clientes ORDER BY id_cliente ASC";  
      $result = mysqli_query($connect, $sql);  
      while($row = mysqli_fetch_array($result))  
      {       
      $output .= '<tr>  
                          <td>'.$row["id_cliente"].'</td>  
                          <td>'.$row["cedula"].'</td>  
                          <td>'.$row["nombres"].'</td>  
                          <td>'.$row["tel"].'</td>  
                          <td>'.$row["dir"].'</td>  
                     </tr>  
                          ';  
      }  
      return $output;  
 }  
 if(isset($_POST["create_pdf"]))  
 {  
      require_once('tcpdf/tcpdf.php');  
      $obj_pdf = new TCPDF('P', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);  
      $obj_pdf->SetCreator(PDF_CREATOR);  
      $obj_pdf->SetTitle("Export HTML Table data to PDF using TCPDF in PHP");  
      $obj_pdf->SetHeaderData('', '', PDF_HEADER_TITLE, PDF_HEADER_STRING);  
      $obj_pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));  
      $obj_pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));  
      $obj_pdf->SetDefaultMonospacedFont('helvetica');  
      $obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER);  
      $obj_pdf->SetMargins(PDF_MARGIN_LEFT, '5', PDF_MARGIN_RIGHT);  
      $obj_pdf->setPrintHeader(false);  
      $obj_pdf->setPrintFooter(false);  
      $obj_pdf->SetAutoPageBreak(TRUE, 10);  
      $obj_pdf->SetFont('helvetica', '', 12);  
      $obj_pdf->AddPage();  
      $content = '';  
      $content .= '  
      <h3 align="center">Export HTML Table data to PDF using TCPDF in PHP</h3><br /><br />  
      <table border="1" cellspacing="0" cellpadding="5">  
           <tr>  
                <th width="5%">ID</th>  
                <th width="30%">Name</th>  
                <th width="10%">Gender</th>  
                <th width="45%">Designation</th>  
                <th width="10%">Age</th>  
           </tr>  
      ';  
      $content .= busca_datos();  
      $content .= '</table>';  
      $obj_pdf->writeHTML($content);  
      $obj_pdf->Output('reporte.pdf', 'I');  
 }  
 ?>  
 <!DOCTYPE html>  
 <html>  
      <head>  
           <title>Exportando a PDF desde PHP</title>  
           <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">            
      </head>  
      <body>  
           <br /><br />  
           <div class="container" style="width:700px;">  
                <h3 align="center">Exportando a PDF desde PHP usando TCPDF en PHP</h3><br />  
                <div class="table-responsive">  
                     <table class="table table-bordered">  
                          <tr>  
                               <th width="5%">ID</th>  
                               <th width="30%">Name</th>  
                               <th width="10%">Gender</th>  
                               <th width="45%">Designation</th>  
                               <th width="10%">Age</th>  
                          </tr>  
                     <?php  
                     echo busca_datos();  
                     ?>  
                     </table>  
                     <br />  
                     <form method="post">  
                          <input type="submit" name="create_pdf" class="btn btn-danger" value="Crear PDF" />  
                     </form>  
                </div>  
           </div>  
      </body>  
 </html>  
    
asked by Junco Fuerte 30.11.2016 в 12:24
source

1 answer

3

It is very likely that your problem is due to the fact that some type of warning message appears before you generate the PDF or there may be a blank space before the initial <?php , etc.

You can silence the errors with error_reporting(0) and ini_set('display_errors', 0) , but I think it is better to send the warning messages to the server's log (instead of removing it on the screen) to debug the problem later. We will also block the data output to the client until the script finishes:

if (isset($_POST["create_pdf"])) {
  ob_start();
  error_reporting(E_ALL & ~E_NOTICE);
  ini_set('display_errors', 0);
  ini_set('log_errors', 1);
  /* ...
   Resto del código que genera el PDF
     ... */
  /* Limpiamos la salida del búfer y lo desactivamos */
  ob_end_clean();
  /* Finalmente generamos el PDF */
  $obj_pdf->Output('reporte.pdf', 'I');
}

Test if that works for you, but do not forget to check your server's log to find out what was the root problem you were suffering (maybe an unmapped variable or macro).

    
answered by 30.11.2016 / 12:39
source