see7di

之前有个客户需要把一些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页面:

<!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了。

<!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问题交流平台,但是英文的。

文章来源:http://www.shwzzz.cn/news/xuetang/159.html

分类:

技术点:

相关文章: