Home  >  Article  >  php教程  >  thinkphp整合系列之tcpdf类生成pdf文件

thinkphp整合系列之tcpdf类生成pdf文件

WBOY
WBOYOriginal
2016-07-06 13:29:191605browse

thinkphp整合系列之tcpdf类生成pdf文件
php生成pdf文件的需求是不怎么常见的;当然也是有的;

既然已经整合使用了;那就写篇博客来讲解下吧;

示例项目:http://git.oschina.net/shuaibai123/thinkphp-bjyadmin

一:引入tcpdf

/ThinkPHP/Library/Vendor/Tcpdf

把tcpdf整个目录拷到自己的项目中;

二:函数

/Application/Common/Common/function.php/**<br>  * 生成pdf<br>  * @param  string $html      需要生成的内容<br>  */<br> function pdf($html='<h1>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> }三:使用方法

好吧;这个没什么好说的了;全写注释里面了;

更多栗子:/ThinkPHP/Library/Vendor/Tcpdf/examples

需要注明的就是:

可以写html标签的;比如说是识别h标签的字体加粗加大效果的;

可以写style样式;但是并不能完全支持;

tcpdf的官网需要自带梯子:http://www.tcpdf.org

thinkphp整合系列之tcpdf类生成pdf文件

本文为白俊遥原创文章,转载无需和我联系,但请注明来自白俊遥博客http://baijunyao.com

云栖大会北京站:阿里技术专家难得出镜,这次一下来了100多位?!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn