之前有个客户需要把一些html页面生成pdf文件,然后我就找一些用php把html页面围成pdf文件的类。方法是可谓是找了很多很多,什么html2pdf,pdflib,FPDF这些都试过了,但是都没有达到我要的求。
pdflib,FPDF这两个方法是需要编写程序去生成pdf的,就也是讲不支持直接把html页面转换成pdf;html2pdf这个虽然可以把html页面转换成pdf文件,但是它只能转换一般简单的html代码,如果你的html内容要的是通过后台新闻编辑器排版的那肯定不行的。
纠结了半天,什么百度,谷歌搜索都用了,搜索了半天,功夫不负有心人,终于找到一个非常好用的方法了,下面就隆重介绍。
它就是:wkhtmltopdf,wkhtmltopdf可以直接把任何一个可以在浏览器中浏览的网页直接转换成一个pdf,首先说明一下它不是一个PHP类,而是一个把html页面转换成pdf的一个软件,但是它并不是一个简单的桌面软件,而且它直接cmd批处理的。而且php有个shell_exec()函数。下面就一步一步介绍如何用php来让它生成pdf文件的方法。
一,下载并安装pdf下载地址:http://code.google.com/p/wkhtmltopdf/downloads/list上面有各种平台下安装的安装包,英文不好的直接谷歌翻译一下。下面以 windows平台上使用举例,我的下载的是wkhtmltopdf-0.9.9-installer.exe这个版本,我在Win7 32位64位和windows 2003上安装测试都没有问题的。下载好以后直接安装就可以了,注意安装路径要知道,下面会用到的。安装好以后需要在系统环境变量变量名为”Path”的后添加:;C:PRogram Files (x86)wkhtmltopdf 也就是你安装的目录。安装好以后重启电脑。
二,测试使用效果直接在cmd里输入:wkhtmltopdf http://www.shwzzz.cn/ F:website1.pdf第一个是:运行软件名称(这个是不变的) 第二个是网址 第三个是生成后的路径及文件名。回车后是不是看生一个生成进度条的提示呢,恭喜您已经成功了,到你的生成目录里看看是不是有一个刚生成的pdf文件呢。
三,php里调用php里调用是很简单的,用shell_exec这个函数就可以了,如果shell_exec函数不能用看看php.ini里是否补禁用了。举例:<?php shell_exec(“wkhtmltopdf http://www.shwzzz.cn/ 1.pdf”) ?>
三,解决分页问题wkhtmltopdf 很好用,但也有些不尽人意。就是当一个html页面很长我需要在指定的地方分页那怎么办呢? wkhtmltopdf 开发者在开发的时候并不是没有考虑到这一点,例如下面这个html页面:
[html] view plain copy print?<!DOCTYPE html PUBLIC ”-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”> <html xmlns=“http://www.w3.org/1999/xhtml”> <head> <title>pdf</title> <meta http-equiv=“Content-Type” content=“text/html; charset=utf-8” /> </head> <style type=“text/CSS”> *{ margin:0px; padding:0px;} div{ width:800px; height:1362px;margin:auto;} </style> <body> <div style=“ background:#030”></div> <div style=“ background:#033”></div> <div style=“ background:#369”></div> <div style=“ background:#F60”></div> <div style=“ background:#F3C”></div> <div style=“ background:#F0F”></div> <div style=“ background:#0FF”></div> <div style=“ background:#FF0”></div> <div style=“ background:#00F”></div> <div style=“ background:#0F0”></div> <div style=“ background:#033”></div> <div style=“ background:#369”></div> <div style=“ background:#F60”></div> <div style=“ background:#030”></div> <div style=“ background:#033”></div> <div style=“ background:#369”></div> <div style=“ background:#F60”></div> <div style=“ background:#F3C”></div> <div style=“ background:#F0F”></div> <div style=“ background:#0FF”></div> <div style=“ background:#FF0”></div> <div style=“ background:#00F”></div> <div style=“ background:#0F0”></div> </body> </html><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>pdf</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><style type="text/css">*{ margin:0px; padding:0px;}div{ width:800px; height:1362px;margin:auto;}</style><body><div style=" background:#030"></div><div style=" background:#033"></div><div style=" background:#369"></div><div style=" background:#F60"></div><div style=" background:#F3C"></div><div style=" background:#F0F"></div><div style=" background:#0FF"></div><div style=" background:#FF0"></div><div style=" background:#00F"></div><div style=" background:#0F0"></div><div style=" background:#033"></div><div style=" background:#369"></div><div style=" background:#F60"></div><div style=" background:#030"></div><div style=" background:#033"></div><div style=" background:#369"></div><div style=" background:#F60"></div><div style=" background:#F3C"></div><div style=" background:#F0F"></div><div style=" background:#0FF"></div><div style=" background:#FF0"></div><div style=" background:#00F"></div><div style=" background:#0F0"></div></body></html>当我把它生成pdf的时候我想让每个块都是一页,经过无数次调试pdf的一页大约是1362px,但是越往后值就不对了,目前还不知道pdf一页是多少像素。
但是wkhtmltopdf 有个很好的方法,就是在那个div的样式后添加一个:page-break-inside:avoid;就ok了。
[html] view plain copy print?<!DOCTYPE html PUBLIC ”-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”> <html xmlns=“http://www.w3.org/1999/xhtml”> <head> <title>pdf</title> <link href=“css/style.css” rel=“stylesheet” type=“text/css” /> <meta http-equiv=“Content-Type” content=“text/html; charset=utf-8” /> </head> <style type=“text/css”> *{ margin:0px; padding:0px;} div{ width:800px; min-height:1362px;margin:auto;page-break-inside:avoid;} </style> <body> <div style=“ background:#030”></div> <div style=“ background:#033”></div> <div style=“ background:#369”></div> <div style=“ background:#F60”></div> <div style=“ background:#F3C”></div> <div style=“ background:#F0F”></div> <div style=“ background:#0FF”></div> <div style=“ background:#FF0”></div> <div style=“ background:#00F”></div> <div style=“ background:#0F0”></div> <div style=“ background:#033”></div> <div style=“ background:#369”></div> <div style=“ background:#F60”></div> <div style=“ background:#030”></div> <div style=“ background:#033”></div> <div style=“ background:#369”></div> <div style=“ background:#F60”></div> <div style=“ background:#F3C”></div> <div style=“ background:#F0F”></div> <div style=“ background:#0FF”></div> <div style=“ background:#FF0”></div> <div style=“ background:#00F”></div> <div style=“ background:#0F0”></div> </body> </html><span style=“font-family:arial,verdana,sans-serif”><span style=“white-space:normal”> </span></span>[html] view plain copy print?<!DOCTYPE html PUBLIC ”-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”> <html xmlns=“http://www.w3.org/1999/xhtml”> <head> <title>pdf</title> <link href=“css/style.css” rel=“stylesheet” type=“text/css” /> <meta http-equiv=“Content-Type” content=“text/html; charset=utf-8” /> </head> <style type=“text/css”> *{ margin:0px; padding:0px;} div{ width:800px; min-height:1362px;margin:auto;page-break-inside:avoid;} </style> <body> <div style=“ background:#030”></div> <div style=“ background:#033”></div> <div style=“ background:#369”></div> <div style=“ background:#F60”></div> <div style=“ background:#F3C”></div> <div style=“ background:#F0F”></div> <div style=“ background:#0FF”></div> <div style=“ background:#FF0”></div> <div style=“ background:#00F”></div> <div style=“ background:#0F0”></div> <div style=“ background:#033”></div> <div style=“ background:#369”></div> <div style=“ background:#F60”></div> <div style=“ background:#030”></div> <div style=“ background:#033”></div> <div style=“ background:#369”></div> <div style=“ background:#F60”></div> <div style=“ background:#F3C”></div> <div style=“ background:#F0F”></div> <div style=“ background:#0FF”></div> <div style=“ background:#FF0”></div> <div style=“ background:#00F”></div> <div style=“ background:#0F0”></div> </body> </html><span style=“font-family:arial,verdana,sans-serif”><span style=“white-space:normal”> </span></span>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>pdf</title><link href="css/style.css" rel="stylesheet" type="text/css" /><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><style type="text/css">*{ margin:0px; padding:0px;}div{ width:800px; min-height:1362px;margin:auto;page-break-inside:avoid;}</style><body><div style=" background:#030"></div><div style=" background:#033"></div><div style=" background:#369"></div><div style=" background:#F60"></div><div style=" background:#F3C"></div><div style=" background:#F0F"></div><div style=" background:#0FF"></div><div style=" background:#FF0"></div><div style=" background:#00F"></div><div style=" background:#0F0"></div><div style=" background:#033"></div><div style=" background:#369"></div><div style=" background:#F60"></div><div style=" background:#030"></div><div style=" background:#033"></div><div style=" background:#369"></div><div style=" background:#F60"></div><div style=" background:#F3C"></div><div style=" background:#F0F"></div><div style=" background:#0FF"></div><div style=" background:#FF0"></div><div style=" background:#00F"></div><div style=" background:#0F0"></div></body></html>http://code.google.com/p/wkhtmltopdf/这个是wkhtmltopdf问题交流平台,但是英文的。
wkhtmltopdf 中文参数详解
php使用事例:shell_exec(“wkhtmltopdf –orientation Landscape http://front.dfzx12.com/?r=report/studentgene/id/2 1234567.pdf”);shell_exec(“wkhtmltopdf –header-center ‘baobiao’ –page-width 50cm –header-line –margin-top 1cm –header-line 1.html oma11.pdf”);wkhtmltopdf [OPTIONS]… <input file> [More input files] <output file>常规选项 –allow <path> 允许加载从指定的文件夹中的文件或文件(可重复)–book* 设置一会打印一本书的时候,通常设置的选项 –collate 打印多份副本时整理 –cookie <name> <value> 设置一个额外的cookie(可重复) –cookie-jar <path> 读取和写入的Cookie,并在提供的cookie jar文件 –copies <number> 复印打印成pdf文件数(默认为1) –cover* <url> 使用HTML文件作为封面。它会带页眉和页脚的TOC之前插入 –custom-header <name> <value> 设置一个附加的HTTP头(可重复) –debug-javascript 显示的Javascript调试输出 –default-header* 添加一个缺省的头部,与页面的左边的名称,页面数到右边,例如: –header-left ‘[webpage]’ –header-right ‘[page]/[toPage]’ –header-line –disable-external-links* 禁止生成链接到远程网页 –disable-internal-links* 禁止使用本地链接–disable-javascript 禁止让网页执行JavaScript –disable-pdf-compression* 禁止在PDF对象使用无损压缩 –disable-smart-shrinking* 禁止使用WebKit的智能战略收缩,使像素/ DPI比没有不变 –disallow-local-file-access 禁止允许转换的本地文件读取其他本地文件,除非explecitily允许用 –allow –dpi <dpi> 显式更改DPI(这对基于X11的系统没有任何影响) –enable-plugins 启用已安装的插件(如Flash –encoding <encoding> 设置默认的文字编码 –extended-help 显示更广泛的帮助,详细介绍了不常见的命令开关 –forms* 打开HTML表单字段转换为PDF表单域 –grayscale PDF格式将在灰阶产生–help Display help –htmldoc 输出程序HTML帮助 –ignore-load-errors 忽略claimes加载过程中已经遇到了一个错误页面 –lowquality 产生低品质的PDF/ PS。有用缩小结果文档的空间 –manpage 输出程序手册页 –margin-bottom <unitreal> 设置页面下边距 (default 10mm) –margin-left <unitreal> 将左边页边距 (default 10mm) –margin-right <unitreal> 设置页面右边距 (default 10mm) –margin-top <unitreal> 设置页面上边距 (default 10mm) –minimum-font-size <int> 最小字体大小 (default 5) –no-background 不打印背景–orientation <orientation> 设置方向为横向或纵向 –page-height <unitreal> 页面高度 (default unit millimeter) –page-offset* <offset> 设置起始页码 (default 1) –page-size <size> 设置纸张大小: A4, Letter, etc. –page-width <unitreal> 页面宽度 (default unit millimeter) –passWord <password> HTTP验证密码 –post <name> <value> Add an additional post field (repeatable) –post-file <name> <path> Post an aditional file (repeatable) –print-media-type* 使用的打印介质类型,而不是屏幕 –proxy <proxy> 使用代理 –quiet Be less verbose –read-args-from-stdin 读取标准输入的命令行参数 –readme 输出程序自述–redirect-delay <msec> 等待几毫秒为JS-重定向(default 200) –replace* <name> <value> 替换名称,值的页眉和页脚(可重复) –stop-slow-scripts 停止运行缓慢的JavaScripts –title <text> 生成的PDF文件的标题(第一个文档的标题使用,如果没有指定) –toc* 插入的内容的表中的文件的开头–use-xserver* 使用X服务器(一些插件和其他的东西没有X11可能无法正常工作) –user-style-sheet <url> 指定用户的样式表,加载在每一页中–username <username> HTTP认证的用户名 –version 输出版本信息退出 –zoom <float> 使用这个缩放因子 (default 1) 页眉和页脚选项–header-center* <text> (设置在中心位置的页眉内容) –header-font-name* <name> (default Arial) (设置页眉的字体名称)–header-font-size* <size> (设置页眉的字体大小)–header-html* <url> (添加一个HTML页眉,后面是网址)–header-left* <text> (左对齐的页眉文本)–header-line* (显示一条线在页眉下)–header-right* <text> (右对齐页眉文本)–header-spacing* <real> (设置页眉和内容的距离,默认0)–footer-center* <text> (设置在中心位置的页脚内容) –footer-font-name* <name> (设置页脚的字体名称) –footer-font-size* <size> (设置页脚的字体大小default 11)–footer-html* <url> (添加一个HTML页脚,后面是网址)–footer-left* <text> (左对齐的页脚文本)–footer-line* 显示一条线在页脚内容上)–footer-right* <text> (右对齐页脚文本)–footer-spacing* <real> (设置页脚和内容的距离)./wkhtmltopdf –footer-right ‘[page]/[topage]’ http://www.baidu.com baidu.pdf./wkhtmltopdf –header-center ‘报表’ –header-line –margin-top 2cm –header-line http://192.168.212.139/oma/ oma.pdf表内容选项中 –toc-depth* <level> Set the depth of the toc (default 3) –toc-disable-back-links* Do not link from section header to toc –toc-disable-links* Do not link from toc to sections –toc-font-name* <name> Set the font used for the toc (default Arial) –toc-header-font-name* <name> The font of the toc header (if unset use –toc-font-name) –toc-header-font-size* <size> The font size of the toc header (default 15) –toc-header-text* <text> The header text of the toc (default Table Of Contents) –toc-l1-font-size* <size> Set the font size on level 1 of the toc (default 12) –toc-l1-indentation* <num> Set indentation on level 1 of the toc (default 0) –toc-l2-font-size* <size> Set the font size on level 2 of the toc (default 10) –toc-l2-indentation* <num> Set indentation on level 2 of the toc (default 20) –toc-l3-font-size* <size> Set the font size on level 3 of the toc (default 8) –toc-l3-indentation* <num> Set indentation on level 3 of the toc (default 40) –toc-l4-font-size* <size> Set the font size on level 4 of the toc (default 6) –toc-l4-indentation* <num> Set indentation on level 4 of the toc (default 60) –toc-l5-font-size* <size> Set the font size on level 5 of the toc (default 4) –toc-l5-indentation* <num> Set indentation on level 5 of the toc (default 80) –toc-l6-font-size* <size> Set the font size on level 6 of the toc (default 2) –toc-l6-indentation* <num> Set indentation on level 6 of the toc (default 100) –toc-l7-font-size* <size> Set the font size on level 7 of the toc (default 0) –toc-l7-indentation* <num> Set indentation on level 7 of the toc (default 120) –toc-no-dots* Do not use dots, in the toc轮廓选项 –dump-outline <file> 转储目录到一个文件 –outline 显示目录(文章中h1,h2来定) –outline-depth <level> 设置目录的深度(默认为4)页脚和页眉 * [page] 由当前正在打印的页的数目代替 * [frompage] 由要打印的第一页的数量取代 * [topage] 由最后一页要打印的数量取代 * [webpage] 通过正在打印的页面的URL替换 * [section] 由当前节的名称替换 * [subsection] 由当前小节的名称替换 * [date] 由当前日期系统的本地格式取代 * [time] 由当前时间,系统的本地格式取代 ./wkhtmltopdf –footer-right ‘[page]/[topage]’ http://www.baidu.com baidu.pdf ./wkhtmltopdf –header-center ’报表’ –outline –header-line –margin-top 2cm –header-line http://www.hao123.com/ hao123.pdf ./wkhtmltopdf –header-left ‘[webpage]’ –footer-center ‘测试([page]/[toPage])’ http://www.baidu.com baidu.pdf内容转载自http://blog.csdn.net/mengke1124/article/details/43604889
新闻热点
疑难解答
图片精选