如何解决在PHP中使用TCPDF获得未定义偏移量0的错误

时间:2019-02-27 03:14:18

标签: php html mysql pdf tcpdf

这是我声明要从表生成pdf的部分。我在youtube上观看了该教程,并按照所有步骤进行操作,但仍然出现错误。我是编程领域的新手,这是我的小型项目之一。 我得到的错误是:

  

注意:未定义的偏移量:第17162行在C:\ xampp \ htdocs \ tcpdff \ tcpdf.php中

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

?>

任何答案将不胜感激!谢谢!

1 个答案:

答案 0 :(得分:0)

代替:

$obj_pdf->writeHTML($content);

使用此:

$obj_pdf->writeHTMLCell(0, 0 , '' , '' , $content);