thinkphp整合系列之tcpdf类生成pdf文件
php生成pdf文件的需求是不怎么常见的;当然也是有的;
既然已经整合使用了;那就写篇博客来讲解下吧;
示例项目:http://git.oschina.net/shuaibai123/thinkphp-bjyadmin
一:引入tcpdf
/thinkphp/library/vendor/tcpdf
把tcpdf整个目录拷到自己的项目中;
二:函数
/application/common/common/function.php/**
* 生成pdf
* @param string $html 需要生成的内容
*/
function pdf($html='hello word'){
vendor('tcpdf.tcpdf');
$pdf = new \tcpdf(pdf_page_orientation, pdf_unit, pdf_page_format, true, 'utf-8', false);
// 设置打印模式
$pdf->setcreator(pdf_creator);
$pdf->setauthor('nicola asuni');
$pdf->settitle('tcpdf example 001');
$pdf->setsubject('tcpdf tutorial');
$pdf->setkeywords('tcpdf, pdf, example, test, guide');
// 是否显示页眉
$pdf->setprintheader(false);
// 设置页眉显示的内容
$pdf->setheaderdata('logo.png', 60, 'baijunyao.com', '白俊遥博客', array(0,64,255), array(0,64,128));
// 设置页眉字体
$pdf->setheaderfont(array('dejavusans', '', '12'));
// 页眉距离顶部的距离
$pdf->setheadermargin('5');
// 是否显示页脚
$pdf->setprintfooter(true);
// 设置页脚显示的内容
$pdf->setfooterdata(array(0,64,0), array(0,64,128));
// 设置页脚的字体
$pdf->setfooterfont(array('dejavusans', '', '10'));
// 设置页脚距离底部的距离
$pdf->setfootermargin('10');
// 设置默认等宽字体
$pdf->setdefaultmonospacedfont(pdf_font_monospaced);
// 设置行高
$pdf->setcellheightratio(1);
// 设置左、上、右的间距
$pdf->setmargins('10', '10', '10');
// 设置是否自动分页 距离底部多少距离时分页
$pdf->setautopagebreak(true, '15');
// 设置图像比例因子
$pdf->setimagescale(pdf_image_scale_ratio);
if (@file_exists(dirname(__file__).'/lang/eng.php')) {
require_once(dirname(__file__).'/lang/eng.php');
$pdf->setlanguagearray($l);
}
$pdf->setfontsubsetting(true);
$pdf->addpage();
// 设置字体
$pdf->setfont('stsongstdlight', '', 14, '', true);
$pdf->writehtmlcell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
$pdf->output('example_001.pdf', 'i');
}三:使用方法
好吧;这个没什么好说的了;全写注释里面了;
更多栗子:/thinkphp/library/vendor/tcpdf/examples
需要注明的就是:
可以写html标签的;比如说是识别h标签的字体加粗加大效果的;
可以写style样式;但是并不能完全支持;
tcpdf的官网需要自带梯子:http://www.tcpdf.org
本文为白俊遥原创文章,转载无需和我联系,但请注明来自白俊遥博客http://baijunyao.com
云栖大会北京站:阿里技术专家难得出镜,这次一下来了100多位?!