我想在fpdf的帮助下将我的html表打印成pdf 我有这个代码
<!DOCTYPE html>
<html>
<body>
<table style="width:300px" border="1">
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
</table>
</body>
</html>
我想使用fpdf在pdf中打印此表。 你能说明一下吗?
答案 0 :(得分:2)
我认为你应该选择&#34; MPDF&#34;,它支持html生成pdf。
<?php
$html = '
<h1><a name="top"></a>mPDF</h1>
<h2>Basic HTML Example</h2>
This file demonstrates most of the HTML elements.
<h3>Heading 3</h3>
<h4>Heading 4</h4>
<h5>Heading 5</h5>
<h6>Heading 6</h6>
<p>P: Nulla felis erat, imperdiet eu, ullamcorper non, nonummy quis, elit. Suspendisse potenti. Ut a eros at ligula vehicula pretium. Maecenas feugiat pede vel risus. Nulla et lectus. Fusce eleifend neque sit amet erat. Integer consectetuer nulla non orci. Morbi feugiat pulvinar dolor. Cras odio. Donec mattis, nisi id euismod auctor, neque metus pellentesque risus, at eleifend lacus sapien et risus. Phasellus metus. Phasellus feugiat, lectus ac aliquam molestie, leo lacus tincidunt turpis, vel aliquam quam odio et sapien. Mauris ante pede, auctor ac, suscipit quis, malesuada sed, nulla. Integer sit amet odio sit amet lectus luctus euismod. Donec et nulla. Sed quis orci. </p>
<hr />
<div><img src="tiger.wmf" style="float:right;">DIV: Proin aliquet lorem id felis. Curabitur vel libero at mauris nonummy tincidunt. Donec imperdiet. Vestibulum sem sem, lacinia vel, molestie et, laoreet eget, urna. Curabitur viverra faucibus pede. Morbi lobortis. Donec dapibus. Donec tempus. Ut arcu enim, rhoncus ac, venenatis eu, porttitor mollis, dui. Sed vitae risus. In elementum sem placerat dui. Nam tristique eros in nisl. Nulla cursus sapien non quam porta porttitor. Quisque dictum ipsum ornare tortor. Fusce ornare tempus enim. </div>
';
include("../mpdf.php");
$mpdf=new mPDF();
$mpdf->WriteHTML($html);
$mpdf->Output();
exit;
?>
答案 1 :(得分:0)
首先,您应该将数据存储到(二维)数组中,如下所示:
$data = array(0 => array('firstname' => 'Jill',
'lastname' => 'Smith',
'number' => 50),
1 => array('firstname' => 'Eve',
'lastname' => 'Jackson',
'number' => 94),
2 => <... and so on...>);
然后,您将能够将数据用于HTML或FPDF。
在创建表格单元格时遍历数组,并使用相应的数据填充单元格。
在普通FPDF中,要创建表格,您必须使用Cell
(或MultiCell
,如果您希望单元格内的换行符)功能,并为每个单元格正确定位。
FPDF有一个插件,允许你逐行添加创建表格,这更容易:Table with MultiCells