thinkphp integration series tcpdf class generates pdf files
The need for php to generate pdf files is not very common; of course it does exist;
Now that it has been integrated and used; let’s write a blog to explain it;
Sample project: http://git.oschina.net/shuaibai123/thinkphp-bjyadmin
1: Introduce tcpdf
/ThinkPHP/Library/Vendor/Tcpdf
Copy the entire tcpdf directory to your own project;
2: Function
/Application/Common/Common/function.php/**<code class="prettyprint linenums lang-php">/**<br>
* 生成pdf<br>
* @param string $html 需要生成的内容<br>
*/<br>
function pdf($html='<h1 style="color:red">hello word</h1>'){<br>
vendor('Tcpdf.tcpdf');<br>
$pdf = new Tcpdf(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);<br>
// 设置打印模式<br>
$pdf->SetCreator(PDF_CREATOR);<br>
$pdf->SetAuthor('Nicola Asuni');<br>
$pdf->SetTitle('TCPDF Example 001');<br>
$pdf->SetSubject('TCPDF Tutorial');<br>
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');<br>
// 是否显示页眉<br>
$pdf->setPrintHeader(false);<br>
// 设置页眉显示的内容<br>
$pdf->SetHeaderData('logo.png', 60, 'baijunyao.com', '白俊遥博客', array(0,64,255), array(0,64,128));<br>
// 设置页眉字体<br>
$pdf->setHeaderFont(Array('dejavusans', '', '12'));<br>
// 页眉距离顶部的距离<br>
$pdf->SetHeaderMargin('5');<br>
// 是否显示页脚<br>
$pdf->setPrintFooter(true);<br>
// 设置页脚显示的内容<br>
$pdf->setFooterData(array(0,64,0), array(0,64,128));<br>
// 设置页脚的字体<br>
$pdf->setFooterFont(Array('dejavusans', '', '10'));<br>
// 设置页脚距离底部的距离<br>
$pdf->SetFooterMargin('10');<br>
// 设置默认等宽字体<br>
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);<br>
// 设置行高<br>
$pdf->setCellHeightRatio(1);<br>
// 设置左、上、右的间距<br>
$pdf->SetMargins('10', '10', '10');<br>
// 设置是否自动分页 距离底部多少距离时分页<br>
$pdf->SetAutoPageBreak(TRUE, '15');<br>
// 设置图像比例因子<br>
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);<br>
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {<br>
require_once(dirname(__FILE__).'/lang/eng.php');<br>
$pdf->setLanguageArray($l);<br>
}<br>
$pdf->setFontSubsetting(true);<br>
$pdf->AddPage();<br>
// 设置字体<br>
$pdf->SetFont('stsongstdlight', '', 14, '', true);<br>
$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);<br>
$pdf->Output('example_001.pdf', 'I');<br>
}
* Generate pdf
* @param string $html Content to be generated
*/
function pdf($html='
Yunqi Conference Beijing Station: It is rare for Alibaba technical experts to appear on the scene. More than 100 people came this time? !