温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-07-18 08:25:37 来源:网络 阅读:733 作者:帅白123 栏目:web开发

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='<h2 >hello word</h2>'){     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

需要注明的就是:

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

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

  3. tcpdf的官网需要自带×××:http://www.tcpdf.org

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


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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI