登录  /  注册

如何用php将html转pdf文件

藏色散人
发布: 2020-07-06 09:18:28
原创
4345人浏览过

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

如何用php将html转pdf文件

之前有个客户需要把一些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里是否补禁用了。
举例:

三,解决分页问题
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 有个很好的方法,就是在那个p的样式后添加一个: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问题交流平台,但是英文的。

很多相关知识,请访问PHP中文网

以上就是如何用php将html转pdf文件的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号