Home >php教程 >php手册 >thinkphp integration series tcpdf class generates pdf files

thinkphp integration series tcpdf class generates pdf files

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

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='

hello word

'){
Vendor('Tcpdf.tcpdf');
$pdf = new Tcpdf(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
//Set printing mode
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('TCPDF Example 001');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
// Whether to display the header $pdf->setPrintHeader(false); //Set the content displayed in the header $pdf->SetHeaderData('logo.png', 60, 'baijunyao.com', 'Bai Junyao Blog', array(0,64,255), array(0,64,128)); // Set header font $pdf->setHeaderFont(Array('dejavusans', '', '12')); // Distance from header to top $pdf->SetHeaderMargin('5'); // Whether to display the footer $pdf->setPrintFooter(true); //Set the content displayed in the footer $pdf->setFooterData(array(0,64,0), array(0,64,128)); // Set footer font $pdf->setFooterFont(Array('dejavusans', '', '10')); // Set the distance between the footer and the bottom $pdf->SetFooterMargin('10'); // Set the default monospaced font $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); //Set row height $pdf->setCellHeightRatio(1); // Set the left, top and right spacing $pdf->SetMargins('10', '10', '10'); //Set whether to automatically break pages. How far away from the bottom is the page break $pdf->SetAutoPageBreak(TRUE, '15'); //Set image scale factor $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(); // Set font $pdf->SetFont('stsongstdlight', '', 14, '', true); $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true); $pdf->Output('example_001.pdf', 'I'); }Three: How to use Okay; there’s nothing more to say about this; I’ve written it all in the comments; More chestnuts:/ThinkPHP/Library/Vendor/Tcpdf/examples What needs to be noted is: You can write html tags; for example, it can recognize the font boldness and enlargement effect of h tags; You can write styles; but they are not fully supported; The official website of tcpdf requires you to bring your own ladder: http://www.tcpdf.org

thinkphp integration series tcpdf class generates pdf files

This article is Bai Junyao’s original article. There is no need to contact me for reprinting, but please indicate that it is from Bai Junyao’s blog http://baijunyao.com

Yunqi Conference Beijing Station: It is rare for Alibaba technical experts to appear on the scene. More than 100 people came this time? !

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