php采用tcpdf生成pdf支持中文,图片

来源:互联网 发布:软件测试培训资料 编辑:程序博客网 时间:2024/05/18 23:54
上次找到fpdf发现文档支持不足。而且含有php5.4不能用了的过期函数,图片进去也显示不了等问题。

特意找到这个tcpdf。

下载的地址
http://sourceforge.net/projects/tcpdf/files/

搞一个试试

中文字体包下载

http://sourceforge.net/projects/hawebs/files/Assistance/PHP/Droid%20Sans%20Fallback%20-%20PHP.zip/download
下载后,将它解压放在droidsansfallback.php、droidsansfallback.z以及droidsansfallback.ctg.z这三个文件复制到 TCPDF\fonts 下面即可

注意代码中
$pdf->SetFont(‘droidsansfallback‘, ‘‘, 12);
此行代码控制中文显示
require_once(‘../config/lang/chi.php‘);
表示调用中文包

控制图片输出
$pdf->Image(‘../images/3.jpg‘, ‘‘, ‘‘, 0, 0, ‘‘, ‘‘, ‘‘, false, 300, ‘‘, false, false, 1, false, false, false);
‘‘‘‘
代码直接在下载的example包中复制008那个进行修改。

代码如下
<?phprequire_once(‘../config/lang/chi.php‘);require_once(‘../tcpdf.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(‘中文‘);$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.‘ 008‘, 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$pdf->setLanguageArray($l);// ---------------------------------------------------------// set default font subsetting mode$pdf->setFontSubsetting(true);// set font$pdf->SetFont(‘droidsansfallback‘, ‘‘, 12);// add a page$pdf->AddPage();// set color for text$pdf->SetTextColor(0, 63, 127);// write the text$pdf->Write(5, ‘中文,你支持么。chinese support‘, ‘‘, 0, ‘‘, false, 0, false, false, 0);$pdf->SetXY(40, 40);$pdf->Image(‘../images/3.jpg‘, ‘‘, ‘‘, 0, 0, ‘‘, ‘‘, ‘‘, false, 300, ‘‘, false, false, 1, false, false, false);// ---------------------------------------------------------//Close and output PDF document$pdf->Output(‘example_008.pdf‘, ‘I‘);//============================================================+// END OF FILE                                                //============================================================+


样例地址包罗万象
http://localhost/tcpdf/examples/

官方网站地址
http://www.tcpdf.org/
下载地址
http://sourceforge.net/projects/tcpdf/files/

月小升下载这个的时候,发现更新日志有2012年8月22日的记录,说明还有人在维护。太牛了。
首发于http://java-er.com - http://java-er.com/blog/php-tcpdf/
原创粉丝点击