PHP生成PDF文档的两种方式及HTML2PDF
一、PHP手册中提到的,用dll文件的扩展库,使用pdflib(下载地址:http://www.pdflib.com/download/pdflib-family/pdflib-7/)。
1.下载dll组件。
2.放在AppServ\php5\ext(即php扩展文件夹)下。
3.修改PHP.INI,增加extension=libpdf_php.dll这句,一般都在最后添加。
4.重启服务器。
缺点:因为pdflib是盈利的,但提供免费下载,只不过免费版的会在生成的PDF文件上面有水印,影响美观。
二、使用fpdf类,比较常见的方法,因为开源,文件下载地址:http://www.fpdf.org/,还有多国语言的手册,很不错。
无需修改任何设置,只要引入文件即可。
缺点:对中文支持不是很好,需要自己扩展。
三、HTML2FPDF:http://html2fpdf.sourceforge.net/下载类文件
实例:
<?
require_once('html2fpdf.php');
// activate Output-Buffer:
ob_start();
?>
<html>
<head>
<title>HTML 2 (F)PDF Project</title>
</head>
<body>
<div align="center"><img src="logo.gif" alt="HTML 2 PDF logo" /></div>
<h2 align="center">HTML 2 PDF Project</h2>
Below are the descriptions of each MENU button:
<ul>
<li><b><font color="red">HOME</font></b> - Returns to the main page (this one)</li>
<li><b>FEATURES</b> - Explains the script's main features</li>
<li><b>EXAMPLES</b> - A series of examples in which the script works</li>
<li><b>CLASS-DOC</b> - Some documentation about the used classes</li>
<li><b>LICENSE</b> - In case you want to distribute or modify the code</li>
<li><b>CREDITS</b> - (the name says it all)</li>
<li><b>FAQ</b> - Frequently asked questions...and answers!</li>
</ul>
<p>This project is hosted on Source Forge. DOWNLOAD IT <a
href="http://sourceforge.net/projects/html2fpdf" target="_blank">THERE</a>!</p>
This script was written on PHP 4.3.3 (should work on 4.0 and greater)
<br /><br />
<div style='background:#ccc;border:thin dashed black'>
This page was dinamically created using PHP ob_get_contents and HTML2FPDF
class.<br />
Read more on FAQ on how to make this or check the 2<sup>nd</sup> page (use the
'PageDown' keyboard key)</div><newpage>
<div style='background:#eee;font-weight:bold'><code>
<? $metaphp = htmlspecialchars($metaphp);
$metaphp = str_replace("\n",'<br>',$metaphp);
echo "<?".$metaphp."?>"; ?>
</div></code>
</body>
</html>
<?
// Output-Buffer in variable:
$html=ob_get_contents();
// delete Output-Buffer
ob_end_clean();
$pdf = new HTML2FPDF();
$pdf->DisplayPreferences('HideWindowUI');
$pdf->AddPage();
$pdf->WriteHTML($html);
$pdf->Output('doc.pdf','I');
?>