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>