使用tcpdf生成html表为pdf

时间:2018-06-25 11:12:56

标签: php html mysql printing tcpdf

我想使用tcpdf从html的表格中生成pdf报告, 使用tcpdf打印我的记录。但是当我尝试打印整个表格时,它会显示一个空白文件,没有错误,整个页面都是白纸

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("Student Report");  
$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, '10', PDF_MARGIN_RIGHT);  
$obj_pdf->setPrintHeader(false);  
$obj_pdf->setPrintFooter(false);  
$obj_pdf->SetAutoPageBreak(TRUE, 10);  
$obj_pdf->SetFont('helvetica', '', 11);  
$obj_pdf->AddPage();  
$content = '';  
$content .= '

<h4 align="center"> </h4>Sample<br /> 
<table border="1" cellspacing="0" cellpadding="0">  
     <tr>
        <th width="10%">No.</th>
        <th width="30%">Student Name</th>  
        <th width="15%">Course</th>  
        <th width="15%">Year & Section</th>  
        <th width="30%">Type</th>  
     </tr>      ';
$content .= getData(); 
$content .= '</table>'; 
$obj_pdf->writeHTML($content);  
$obj_pdf->Output('stud.pdf', 'I');

调用此函数从数据库获取数据

function getData()
{
$stud = '';
$conn = mysqli_connect("localhost", "root", "", "dsample2");
$sql = "SELECT *, CONCAT(stud_lname,', ',stud_fname,' ',stud_minitial) as fullName FROM `tbl_stud` ORDER BY `stud_lname` ASC";
$result = mysqli_query($conn, $sql);
$i=0;
while($row = mysqli_fetch_array($result))
{
    $stud .= '<tr>
                    <td>'.$i++.'</td>
                    <td>'.$row["fullName"].'</td>
                    <td>'.$row["stud_course"].'</td>
                    <td>'.$row["stud_year"].'</td>
                    <td>'.$row["stud_type"].'</td>
                </tr>
                ';
}
return $stud;
}

0 个答案:

没有答案