使用TCPDF插件生成pdf以及pdf的中文处理
做了这么多年项目,以前只是在别人的项目中了解过PHP生成pdf文件,知道并不难,但是涉及到了pdf开发库,首先介绍pdf库。
多种多样的pdf开发库
1.WKHTMLTOPDF
- <span style="font-family: Arial; font-size: 14px;"><?php
- $domain = "http://wiki.eoe.cn";
- $htmlUrl = $domain . "/show/html/slug/$slug";
- $_binpath = '/usr/local/bin/';
- $_binname = 'wkhtmltopdf';
- $savePath = "/User/Xia/eoecn/pdf/";
- if (!is_dir($savePath)) {
- //需要自己编写mkdirs函数
- @mkdirs($savePath);
- }
- //由于生成中文目录会乱码,这里过滤掉
- if (preg_match("/[x7f-xff]/", $slug)) {
- $filename = "wiki-slug-$id";
- }else {
- $filename = $slug;
- }
- $saveFile = $savePath . $filename . '.pdf';
- //判断是否已经存在
- if (file_exists($saveFile)) {
- die($saveFile);
- }
- $header = $domain . "/pdf/header";
- $command = $_binpath . $_binname
- . ' -T 15mm --header-spacing 5 --header-html ' . $header
- . ' --footer-right "[page]/[toPage]" ' . $htmlUrl . ' '
- . $saveFile;
- if ($exec) {
- exec($command, $output, $var);
- }
- ?></span>

- <?php
- $domain = "http://wiki.eoe.cn";
- $htmlUrl = $domain . "/show/html/slug/$slug";
- $_binpath = '/usr/local/bin/';
- $_binname = 'wkhtmltopdf';
- $savePath = "/User/Xia/eoecn/pdf/";
- if (!is_dir($savePath)) {
- //需要自己编写mkdirs函数
- @mkdirs($savePath);
- }
- //由于生成中文目录会乱码,这里过滤掉
- if (preg_match("/[x7f-xff]/", $slug)) {
- $filename = "wiki-slug-$id";
- }else {
- $filename = $slug;
- }
- $saveFile = $savePath . $filename . '.pdf';
- //判断是否已经存在
- if (file_exists($saveFile)) {
- die($saveFile);
- }
- $header = $domain . "/pdf/header";
- $command = $_binpath . $_binname
- . ' -T 15mm --header-spacing 5 --header-html ' . $header
- . ' --footer-right "[page]/[toPage]" ' . $htmlUrl . ' '
- . $saveFile;
- if ($exec) {
- exec($command, $output, $var);
- }
- ?>
代码托管在:https://code.google.com/p/wkhtmltopdf/
在Linux和mac os等其它平台安装文档:http://www.tecmint.com/install-wkhtmltopdf-html-page-to-pdf-converter-in-rhel-centos-fedora/
要想完好支持html中的url和其它,参考:http://www.cnblogs.com/timelyxyz/archive/2012/12/24/2831523.html
说明文档: http://madalgo.au.dk/~jakobt/wkhtmltoxdoc/wkhtmltopdf-0.9.9-doc.html
php使用方法: http://mikehaertl.github.com/phpwkhtmltopdf/
例子:
- <span style="font-family: Arial;"><?php
- require_once('WkHtmlToPdf.php');
- $pdf = new WkHtmlToPdf;
- // Add a HTML file, a HTML string or a page from a URL
- $pdf->addPage('/home/eoe/page.html');
- $pdf->addPage('<html>....</html>');
- $pdf->addPage('http://google.com');
- // Add a cover (same sources as above are possible)
- $pdf->addCover('mycover.pdf');
- // Add a Table of contents
- $pdf->addToc();
- // Save the PDF
- $pdf->saveAs('/tmp/new.pdf');
- // ... or send to client for inline display
- $pdf->send();
- // ... or send to client as file download
- $pdf->send('test.pdf');</span>

- <?php
- require_once('WkHtmlToPdf.php');
- $pdf = new WkHtmlToPdf;
- // Add a HTML file, a HTML string or a page from a URL
- $pdf->addPage('/home/eoe/page.html');
- $pdf->addPage('<html>....</html>');
- $pdf->addPage('http://google.com');
- // Add a cover (same sources as above are possible)
- $pdf->addCover('mycover.pdf');
- // Add a Table of contents
- $pdf->addToc();
- // Save the PDF
- $pdf->saveAs('/tmp/new.pdf');
- // ... or send to client for inline display
- $pdf->send();
- // ... or send to client as file download
- $pdf->send('test.pdf');
2.FPDF
FPDF是一个纯粹的通过PHP类来生成PDF文档的方法,需要生成的内容直接在PHP代码中来指定,生成文字,图片,线条等等,都有自己的方法。不足的是utf8和中文支持很差,分别需要调用chinese-unicode.php和chinese.php等扩展文件,附上一个简单的例子:
- <span style="font-family: Arial;"><?php
- require('chinese-unicode.php');
- $pdf=new PDF_Unicode();
- $pdf->Open();
- $pdf->AddPage();
- $pdf->AddUniCNShwFont('uni');
- $pdf->SetFont('uni','',20);
- $pdf->Write(10, "eoe移动开发者社区");
- $pdf->Ln();
- $pdf->MultiCell (120, 10, "开发者社区");
- $pdf->Cell (240, 10, "本文是用utf8编码格式");
- $pdf->Ln();
- $pdf->Output();
- ?></span>

- <?php
- require('chinese-unicode.php');
- $pdf=new PDF_Unicode();
- $pdf->Open();
- $pdf->AddPage();
- $pdf->AddUniCNShwFont('uni');
- $pdf->SetFont('uni','',20);
- $pdf->Write(10, "eoe移动开发者社区");
- $pdf->Ln();
- $pdf->MultiCell (120, 10, "开发者社区");
- $pdf->Cell (240, 10, "本文是用utf8编码格式");
- $pdf->Ln();
- $pdf->Output();
- ?>
3.TCPDF
TCPDF是一个用于快速生成PDF文件的PHP5函数包。TCPDF基于FPDF进行扩展和改进。支持UTF-8,Unicode,HTML和 XHTML。但是文件很多很大,配置起来比较复杂。相对而言对复杂的css渲染效果不好,而且不能支持html中太多的css文件,用php转换效率很慢。
不过官方的文档很多,例子也很多,也能够生成很漂亮的pdf文件。官方网址为:http://www.tcpdf.org/
本人使用的就是这个工具包,所以着重给大家介绍一下,个人认为TCPDF还是很好用的,足以满足大多数pdf应用,官方提供了丰富的文档以及例子,各种特性都提供了详细的例子,上手极快。
以下为TCPDF目录结构:
本人使用的是CI框架,在将这个库集成进来的时候,对这个包做了一些改动。
1.所有的基础配置信息都在tcpdf根目录“examples/include/tcpdf_config_alt.php”中,我们为了简化配置,也使用了它原生的配置文件,以简化在开发过程中的配置,将该文件的内容拷贝至CI框架application/conf/pdf_config.php中,找到“K_PATH_IMAGES”配置项,将其值修改为你项目中图片所在的位置,到时候在省城pdf时,所用到的图片资源,将从这里查找。
“PDF_HEADER_LOGO”用于配置pdf中header中logo图片,如需要的话设置成你自己的
还有其他一些常用的配置,如配置Top margin、Bottom margin、Left margin、Default main font name
中文问题
使用tcpdf时中文问题是比较常见,在新版的tcpdf中,已经支持中文了,在fonts目录下有个cid0cs.php和stsonstdlight.php文件,只要直接使用即可
- <span style="font-family: Arial;">$pdf->SetFont('cid0cs', '', 10);</span>

- $pdf->SetFont('cid0cs', '', 10);
- <span style="font-family: Arial;">$pdf->SetFont('stsongstdlight','', 10);</span>

- $pdf->SetFont('stsongstdlight','', 10);
但是经过本人的实践,使用这两种字体,的确是支持中文,但是英文的显示并不是太好,这种方式生成的PDF文件的优点是:文件体积小,生成快速。但也有缺点是,没有嵌入中文字体,只限于安装了Adobe Reader之后才能正常显示。那万一用户使用的是FoxIt Reader或者是Linux操作系统呢?显示效果就不一样了。因此,为了保证生成的PDF文件在任何环境下都有同样的显示效果,嵌入字体是必需的。 Windows下有很多中文字体,但是我们要用在TCPDF中的中文字体有下面几个要求: · 支持Unicode,因为TCPDF支持的是Unicode; · 体积越小越好; · 最好是也支持繁体中文; 将下载后的DroidSansFallback.ttf字体文件复制到tcpdf目录下的fonts目录,并保证web服务器对该目录有读写的权限,否则在生成tcpdf字体是会出错。DroidSansFallback.ttf下载地址
在你的代码中添加如下代码:
- <span style="font-family: Arial; font-size: 14px;">$fontname = $this->tcpdf->addTTFfont('dist/font/Droid_Sans_Fallback.ttf', 'TrueTypeUnicode', '', 32);</span>

- $fontname = $this->tcpdf->addTTFfont('dist/font/Droid_Sans_Fallback.ttf', 'TrueTypeUnicode', '', 32);
- <span style="font-family: Arial; font-size: 14px;">$this->tcpdf->SetFont($fontname, '', 10); </span>

- $this->tcpdf->SetFont($fontname, '', 10);
这句代码将你下载的ttf字体自动转化为tcpdf使用的字体,将在font目录下增加这几个文件“droid_sans_fallback.ctg.z,droid_sans_fallback.php,droid_sans_fallback.z”,生成之后,以后就可以直接使用这种字体了。
以下为完整的代码:
- <span style="font-family: Arial; font-size: 14px;">//加载配置文件以及pdf类
- require_once(APPPATH . 'config/pdf_config.php');
- $this->load->library('tcpdf/tcpdf');
- // set document information
- $this->tcpdf->SetCreator(PDF_CREATOR);
- $this->tcpdf->SetAuthor('aaron');
- $this->tcpdf->SetTitle($cur_order['title']);
- $this->tcpdf->SetSubject('lvpad');
- $this->tcpdf->SetKeywords('lvpad, china tour, guide');
- // set default header data
- $this->tcpdf->SetHeaderData('logo.png', PDF_HEADER_LOGO_WIDTH, 'lvpad order', '');
- // set header and footer fonts
- $this->tcpdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
- $this->tcpdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
- // set default monospaced font
- $this->tcpdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
- // set margins
- $this->tcpdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
- $this->tcpdf->SetHeaderMargin(PDF_MARGIN_HEADER);
- $this->tcpdf->SetFooterMargin(PDF_MARGIN_FOOTER);
- // set auto page breaks
- $this->tcpdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
- // set image scale factor
- $this->tcpdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
- //set some language-dependent strings (optional)
- $l['a_meta_charset'] = 'UTF-8';
- $l['a_meta_dir'] = 'ltr';
- $l['a_meta_language'] = 'cn';
- // TRANSLATIONS --------------------------------------
- $l['w_page'] = 'page';
- $this->tcpdf->setLanguageArray($l);
- // 设置字体,如果要支持中文 则选择支持中文的字体
- $fontname = $this->tcpdf->addTTFfont('dist/font/Droid_Sans_Fallback.ttf', 'TrueTypeUnicode', '', 32);
- $this->tcpdf->SetFont($fontname, '', 10);
- // add a page
- $this->tcpdf->AddPage();
- $this->tcpdf->setJPEGQuality(75);
- // Image example with resizing
- $this->tcpdf->Image('images/pdf.jpg', 10, 20, 190, 60, 'JPG', 'http://lvpad.com', '', false, 150, '', false, false, 1, false, false, false);
- // output the HTML content
- $this->tcpdf->writeHTML($html_content, true, false, true, false, '');
- // reset pointer to the last page
- $this->tcpdf->lastPage();
- //Close and output PDF document
- $this->tcpdf->Output($cur_order['title'] . '.pdf', 'I'); </span>

- //加载配置文件以及pdf类
- require_once(APPPATH . 'config/pdf_config.php');
- $this->load->library('tcpdf/tcpdf');
- // set document information
- $this->tcpdf->SetCreator(PDF_CREATOR);
- $this->tcpdf->SetAuthor('aaron');
- $this->tcpdf->SetTitle($cur_order['title']);
- $this->tcpdf->SetSubject('lvpad');
- $this->tcpdf->SetKeywords('lvpad, china tour, guide');
- // set default header data
- $this->tcpdf->SetHeaderData('logo.png', PDF_HEADER_LOGO_WIDTH, 'lvpad order', '');
- // set header and footer fonts
- $this->tcpdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
- $this->tcpdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
- // set default monospaced font
- $this->tcpdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
- // set margins
- $this->tcpdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
- $this->tcpdf->SetHeaderMargin(PDF_MARGIN_HEADER);
- $this->tcpdf->SetFooterMargin(PDF_MARGIN_FOOTER);
- // set auto page breaks
- $this->tcpdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
- // set image scale factor
- $this->tcpdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
- //set some language-dependent strings (optional)
- $l['a_meta_charset'] = 'UTF-8';
- $l['a_meta_dir'] = 'ltr';
- $l['a_meta_language'] = 'cn';
- // TRANSLATIONS --------------------------------------
- $l['w_page'] = 'page';
- $this->tcpdf->setLanguageArray($l);
- // 设置字体,如果要支持中文 则选择支持中文的字体
- $fontname = $this->tcpdf->addTTFfont('dist/font/Droid_Sans_Fallback.ttf', 'TrueTypeUnicode', '', 32);
- $this->tcpdf->SetFont($fontname, '', 10);
- // add a page
- $this->tcpdf->AddPage();
- $this->tcpdf->setJPEGQuality(75);
- // Image example with resizing
- $this->tcpdf->Image('images/pdf.jpg', 10, 20, 190, 60, 'JPG', 'http://lvpad.com', '', false, 150, '', false, false, 1, false, false, false);
- // output the HTML content
- $this->tcpdf->writeHTML($html_content, true, false, true, false, '');
- // reset pointer to the last page
- $this->tcpdf->lastPage();
- //Close and output PDF document
- $this->tcpdf->Output($cur_order['title'] . '.pdf', 'I');
生成的pdf就是这样的:
以上中文问题已经解决,当然,你还可以寻找其他的字体,然后进行尝试。
使用TCPDF插件生成pdf以及pdf的中文处理的更多相关文章
- php 基于tcpdf插件生成pdf
之前在公司做了个项目,,需要导出pdf合同,,在网上找了很久,选择用了tcpdf插件,,具体的插件网上可以搜到,中间遇到了很多的坑,慢慢的填. 先下好插件放到指定文件夹下 然后使用tcpdf插件里ht ...
- php生成pdf,php+tcpdf生成pdf, php pdf插件
插件例子:https://tcpdf.org/examples/ 下载tcpdf插件: demo // Include the main TCPDF library (search for insta ...
- C#使用iTextSharp+ZXing.Net+FreeSpire.PDF生成和打印pdf文档
项目需求(Winform)可以批量打印某个模板,经过百度和摸索,使用iTextSharp+ZXing.Net+FreeSpire.PDF三个类库实现了生成pdf.生成条形码和打印pdf功能. 首先在项 ...
- jquery插件导出excel和pdf(解决中文乱码问题)
参考文件:http://jackyrong.iteye.com/blog/2169683 https://my.oschina.net/aruan/blog/418980 https://segmen ...
- 利用PDF.JS插件解决了本地pdf文件在线浏览问题(根据需要隐藏下载功能,只保留打印功能)
我是在IE11和谷歌上做的测试,都可以显示,把做出的东西记录下来,方便大家还有自己学习! 可以在IIS7服务器上也可以下载Tomcat来做服务器 Tomcat下载地址 http://pan.bai ...
- Qt 生成word、pdf文档
需求:将软件处理的结果保存为一个报告文档,文档中包含表格.图片.文字,格式为word的.doc和.pdf.生成word是为了便于用户编辑. 开发环境:qt4.8.4+vs2010 在qt的官网上对于p ...
- 将div的内容生成清晰的PDF、高清PDF
//需要引入html2canvas.js.jquery.js文件 html: <button type="button" class="btn btn-primar ...
- 使用pdfjs插件在线预览PDF文件
前言 本文介绍在html中使用 pdfjs插件在线预览PDF文件的方法. 实现步骤 下载 pdfjs 并引入项目中 到PDFJS官网 http://mozilla.github.io/pdf.js/g ...
- HTML生成横向的PDF
HTML生成PDF请参照:https://www.cnblogs.com/yunfeiyang-88/p/10984740.html 如要生成横向的PDF:在html模板的style标签里面加入@pa ...
随机推荐
- Python与PHP通过XMLRPC进行通信
Python与PHP通过XMLRPC进行通信:服务器端用Python,客户端用PHP. 服务器端:xmlrpc_server.py #!/usr/bin/python # coding: UTF-8 ...
- onreadystatechange()事件
onreadystatechange(): 存储函数(或函数名),当 readyState 改变时,就会触发 onreadystatechange() 事件. xmlhttp.onreadystat ...
- Net操作Excel(终极方法NPOI)
NPOI 待学习 http://www.cnblogs.com/stone_w/archive/2012/08/02/2620528.html
- maven 项目无法发布,无法编译的解决办法
1 Web Deployment Assembly信息都合理2 重新clear项目,让JAVA代码重新生成.class文件在target目录中
- createjs easal.js制作了一个很简单的链路图
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- Mac下安装nginx
试图折腾了一下手动安装,太多依赖,繁琐的要死.只好装了一个homebrew , 具体安装homebrew的教程网上查吧,就是一句话(ruby -e "$(curl -fsSL https:/ ...
- Java性能调优
一.JVM内存模型及垃圾收集算法 1.根据Java虚拟机规范,JVM将内存划分为: New(年轻代) Tenured(年老代) 永久代(Perm) 其中New和Tenured属于堆内存,堆内存会从JV ...
- C#封装好的Win32API
Kernel.cs using System; using System.Runtime.InteropServices; using System.Text; using HANDLE = Syst ...
- 从jQuery源码阅读看 dom load
最近两天不忙的时候再回过来研究一下jquery的源码,看到$(document).ready()时,深入的研究了一下dom的加载问题. 我们都知道,window.onload可以解决我们的js执行时机 ...
- c#摄像头编程实例 (转)
c#摄像头编程实例 摄像头编程 安装摄像头后,一般可以找到一个avicap32.dll文件 这是一个关于设想头的类 using system;using System.Runtime.Intero ...