wkhtmltopdf是C++编写的跨平台html转pdf开源程序
必须先安装wkhtmltopdf,并添加到环境变量,能够在“终端”正常调用。
这里主要讲一下php调用wkhtmltopdf的问题。
手写代码
编写一个函数,把html字符串,转换为pdf
首先定义一个html字符串,然后把html字符串使用 file_put_contents 写入html(注意带上meta指定charset,否则后面会乱码)
接下来将得到的html文件,调用exec函数与系统交互生成pdf文件。(注意 –enable-local-file-access 参数指定允许读取本地文件,因为从xx版本开始默认不允许读取本地文件)
$html = <<<eot
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<style>
.jl {font-size: 14px;color: #333;}
.profile {position: relative;height: 120px; margin-top: 20px;}
.profile img {width: 150px; height: 200px;}
.profile .info {margin: -160px 0 80px 170px;}
.jl h2 {margin: 20px 0 8px;font-size: 40px;color: #000;}
.jl h2 small {font-size: 16px; color: #999;}
.jl .base .p1 {margin-bottom: 15px;font-size: 14px;color: #d3ccd9;}
.jl .base .p1 span {color: #666;}
.jl .base ul {position: relative;}
.jl .base ul {padding:25px 0 15px 20px;border-top:1px dotted #999;}
.jl .base .label {position: relative; z-index: 20; display: inline-block; margin: -40px 0 10px; width: 80px; text-align: center; height: 30px; line-height: 30px; background: #eeeeee; font-size: 16px; padding: 0 15px; border-radius: 26px;}
.jl .base ul li {float: left;width: 33%;font-size: 14px;line-height: 30px;color: #999;white-space: nowrap;text-overflow:ellipsis;overflow: hidden;}
.jl .base ul li span {margin-left:3px;color: #000;}
.jl .base ul li.gzjl {float: none; width: 100%; color: #333;}
.jl .base ul li.gzjl .item {padding: 5px 20px; border-left: 3px solid #eee; margin: 25px 0;}
.jl .base ul li.gzjl .item p {padding: 3px 0;}
.jl .base ul li.gzjl .item p em {color: #999;}
.jl .base ul li.gzjl .item strong {font-size: 16px; font-weight: 500; margin-right: 30px;}
.jl .base ul li.gzjl .item .nr {position: relative; padding-left: 72px;}
.jl .base ul li.gzjl .item .nr em {position: absolute; left: 0; top: 3px;}
.jl .dl1 dt {margin: 20px 0 5px; color: #999;}
.jl .dl1 dd {line-height: 30px; word-wrap: break-word;}
.jl .lxr {line-height: 30px;}
</style>
<div class="wrap fn-clear">
<div class="rightMain">
<div class="jl">
<div class="pt base profile">
<img src="/static/images/noPhoto_100.jpg">
<div class="info">
<h2>佚名 <small>的简历</small></h2>
<p class="p1"><span>女 28岁</span> | <span>籍贯:扬州</span> | <span>现居:苏州</span> | <span>2021-06-07更新</span></p>
<span class="bh">职位编号:000061</span>
</div>
</div>
<div class="pt base lxr">
<ul class="fn-clear">
<div class="label">联系方式</div>
<p>联系电话18605222935</p>
<p>电子邮箱marrymmiss@163.com</p>
</ul>
</div>
<div class="pt base">
<ul class="fn-clear">
<div class="label">求职意向</div>
<li>意向地点:<span>江苏 苏州 吴中 城南</span></li>
<li>职位性质:<span>全职</span></li>
<li>意向职位:<span>销售工程师</span></li>
<li>期望薪资:<span>面议</span></li>
<li>到岗时间:<span>1周以内</span></li>
<li>目前状况:<span>离职</span></li>
</ul>
</div>
<div class="pt base">
<ul class="fn-clear">
<div class="label">工作经历</div>
<li>工作年限:<span>4 年</span></li>
<p class="fn-clear"></p>
<dl class="dl1">
<dd>事实上,条件合格是最基本的简历要求。<br>
一个 HR 大概只会花 30 秒去浏览一份简历,想想看,是不是跟顾客、看网页时浏览我们文案的状况很像?<br>
10 秒钟内,如果你的文案没能撬动用户的欲望按钮,他就会转去看下一家。同样,HR 在扫完你的基本信息、学历信息、职业信息和技能信息后,会在 5 秒钟内判断你是否符合岗位的硬性条件,这就是初筛环节。</dd>
</dl>
</ul>
</div>
<div class="pt base">
<ul class="fn-clear">
<div class="label">教育背景</div>
<li>最高学历:<span>本科</span></li>
<li>毕业学院:<span>南通大学</span></li>
<li>毕业时间:<span>2019-11-13</span></li>
<li>所学专业:<span>网络工程师</span></li>
<li>外语水平:<span>一般</span></li>
<li>计算机水平:<span>一般</span></li>
<li class="edu-other">其他教育:<span></span></li>
</ul>
</div>
<div class="pt base">
<ul class="fn-clear">
<div class="label">自我描述</div>
<dl class="dl1">
<dt>个人评价:</dt>
<dd>可可爱爱</dd>
</dl>
<dl class="dl1">
<dt>职业目标:</dt>
<dd>躺着赚钱</dd>
</dl>
</ul>
</div>
</div>
</div>
</div>
eot;
//生成pdf的函数,$html为需要写入到pdf文件里的字符串
function generatePdf($html){
//将字符串$html里的内容写入到$filename_path文件里,该文件为html
$filename = $filename_path = 1;
file_put_contents("{$filename_path}.html", $html);
//wkhtmltopdf将生成的html文件转为相应的pdf
exec("wkhtmltopdf --enable-local-file-access {$filename_path}.html {$filename_path}.pdf");
//判定指定的路径下是否存在相应的pdf
if(file_exists("{$filename_path}.pdf")){
//设置header信息
header("Content-type:application/pdf");
//设置下载的文件名称
header("Content-Disposition:attachment;filename={$filename}.pdf");
//火狐浏览器下文件名设定
if (stripos($_SERVER["HTTP_USER_AGENT"], 'Firefox')) {
header("Content-Disposition: attachment;filename*={$filename}.pdf");
}
//safari浏览器默
if (stripos($_SERVER["HTTP_USER_AGENT"], 'Macintosh')) {
header("Content-Disposition: attachment; filename={$filename}.pdf");
}
//下载文件到本地
echo file_get_contents("{$filename_path}.pdf");
//删除下载Pdf产生在服务器上的临时文件
unlink("{$filename_path}.pdf");
unlink("{$filename_path}.html");
}
exit;
}
generatePdf($html);
phpwkhtmltopdf
它是一个开源的php之wkhtmltopdf工程,对wkhtmltopdf进行了一些封装
为什么要用它?以及为什么不用?
如果简单的代码能实现你所需,则没必要再使用其他第三方,手写代码是最适合的。
如果所需比较复杂,一时难以实现,可考虑使用它,或参阅部分代码。
本篇完,还有疑问?留下评论吧