• 技术文章 >后端开发 >PHP问题

    如何用php将html转pdf文件

    藏色散人藏色散人2020-07-06 09:18:28原创1292

    用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=" 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中文网其它相关文章!

    声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理
    专题推荐:php html pdf
    上一篇:PHP utf8转中文的方法 下一篇:总结mysql php乱码问题
    线上培训班

    相关文章推荐

    • pdf长图如何分页打印• HTML文件怎么转换成PDF文件• php中base64转pdf的方法• php中base64转pdf的方法是什么

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网