thinkPHP框架整合tcpdf插件操作示例
本文实例讲述了thinkphp框架整合tcpdf插件操作。分享给大家供大家参考,具体如下:
网上查了些关于tcpdf 使用教程,整合到tp的话,会有些小问题,由于基础还不是很扎实,花了点时间终于整合ok了。下面介绍步骤:
环境:
tp版本:tp3.2.2
tcpdf:tcpdf_6_2_3
步骤:
1. 将tcpdf_6_2_3.zip解压在web root目录下面,把examples文件夹下面的tcpdf_include.php文件拷贝到tcpdf文件夹下面,再把tcpdf/config/tcpdf_config.php内容替换成tcpdf/examples/config/tcpdf_config_alt.php中的内容
这里要注意的是:tcp_include.php中 $tcpdf_include_dirs
数组要多添加一行:“realpath('./').'/tcpdf/tcpdf.php'
,”
2. 新建test.php
<?php // include the main tcpdf library (search for installation path). require_once('./tcpdf/tcpdf_include.php'); // create new pdf document $pdf = new tcpdf(pdf_page_orientation, pdf_unit, pdf_page_format, true, 'utf-8', false); // set document information $pdf->setcreator(pdf_creator); $pdf->setauthor('nicola asuni'); $pdf->settitle('tcpdf example'); $pdf->setsubject('tcpdf tutorial'); $pdf->setkeywords('tcpdf, pdf, example, test, guide'); // set default header data $pdf->setheaderdata(pdf_header_logo, pdf_header_logo_width, pdf_header_title.' wisvalley', pdf_header_string); // set header and footer fonts $pdf->setheaderfont(array(pdf_font_name_main, '', pdf_font_size_main)); $pdf->setfooterfont(array(pdf_font_name_data, '', pdf_font_size_data)); // set default monospaced font $pdf->setdefaultmonospacedfont(pdf_font_monospaced); // set margins $pdf->setmargins(pdf_margin_left, pdf_margin_top, pdf_margin_right); $pdf->setheadermargin(pdf_margin_header); $pdf->setfootermargin(pdf_margin_footer); // set auto page breaks $pdf->setautopagebreak(true, pdf_margin_bottom); // set image scale factor $pdf->setimagescale(pdf_image_scale_ratio); // set some language-dependent strings (optional) if (@file_exists(dirname(__file__).'/lang/eng.php')) { require_once(dirname(__file__).'/lang/eng.php'); $pdf->setlanguagearray($l); } // --------------------------------------------------------- // set font //$pdf->setfont('helvetica', '', 20); $pdf->setfont('stsongstdlight', '', 20); // add a page $pdf->addpage(); $txt = 'your content'; $pdf->write(0, $txt, '', 0, 'l', true, 0, false, false, 0); $pdf->output('/var/www/example_038.pdf', 'i');//浏览器预览 //$pdf->output('example_038.pdf', 'f');//存储文件 //$pdf->output('example_038.pdf', 'd');//下载文件
这样就可以了。
下面说下我碰到的几个问题:
1.我把这些代码拷贝到tp控制器的某个方法里面报错:class 'home\controller\tcpdf' not found
解答:
$pdf = new tcpdf(pdf_page_orientation, pdf_unit, pdf_page_format, true, 'utf-8', false);
改成
$pdf = new \tcpdf(pdf_page_orientation, pdf_unit, pdf_page_format, true, 'utf-8', false);
对于tp3.2引入了命名空间,这个‘\
'很重要
2.tcpdf error: unable to create output file: example_038.pdf
解答:$pdf->output('/var/www/example_038.pdf', 'i');
路径要为据对路径。
附:tcpdf插件点击此处。
更多关于thinkphp相关内容感兴趣的读者可查看本站专题:《thinkphp入门教程》、《thinkphp模板操作技巧总结》、《thinkphp常用方法总结》、《codeigniter入门教程》、《ci(codeigniter)框架进阶教程》、《zend framework框架入门教程》及《php模板技术总结》。
希望本文所述对大家基于thinkphp框架的php程序设计有所帮助。