this is the part that I declare to generate pdf from table. I have watch the tutorial in youtube and followed all the steps and I still got error. Im new in programming field and this is one of my mini project. the error that i got is :
Notice: Undefined offset: 0 in C:\xampp\htdocs\tcpdff\tcpdf.php on line 17162
function fetch_data()
{
$output ='';
$conn = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
$query = "SELECT * FROM borang_permohonan";
$results = mysqli_query($conn, $query);
while($row = mysqli_fetch_array($results))
{
$output .='
<tr>
<td>'.$row["fullname"].' </td>
<td>'.$row["ic_no"].'</td>
<td>'.$row["email"].' </td>
</tr>
';
}
return $output;
}
if(isset($_POST["create_pdf"]))
{
require_once('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); //or true
$obj_pdf->SetAutoPAgeBreak(TRUE, 10);
$obj_pdf->SetFont('helvetica', '', 12);
$content = '';
$content .= '
<center><h2 >GSM 2019</h2></center>
<table align="center" border="1">
<thead>
<tr>
<th>fullname</th>
<th>ic no</th>
<th>email</th>
</tr>
</thead>
';
$content .= fetch_data();
$content .= '</table>';
$obj_pdf->writeHTML($content);
$obj_pdf->Output("sample.pdf", "I");
}
?>
Any answer will be appreciated! Thank you!
You need to call the AddPage
method before you start your WriteHtml
Good spot would probably be, right after SetFont
$obj_pdf->SetFont('helvetica', '', 12);
$obj_pdf->AddPage();
Instead of:
$obj_pdf->writeHTML($content);
Use this:
$obj_pdf->writeHTMLCell(0, 0 , '' , '' , $content);