• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

如何用php将html转pdf文件

php 搞代码 4年前 (2022-01-04) 35次浏览 已收录 0个评论

用php将html转pdf文件的方法:首先下载并安装pdf;然后测试使用效果;接着用“shell_exec”这个函数在php里调用;最后解决分页问题即可。

之前有个客户需要把一些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=&quot<strong style="color:transparent">来源gaodaima#com搞(代@码网</strong>; 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>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:如何用php将html转pdf文件

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址