哈喽小伙伴们大家伙,昨天更新了PHP导出导入excel的方法,今天给大家带来的是,使用mpdf类库进行pdf的生成。
至于框架的安装之前文章已经说过了,所以直接进入主题
第一步、安装mpdf类库,通用使用composer进行安装
composer require mpdf/mpdf:8.0.3
安装完成后,就可以正常使用了,这个类库其实很简单,我们所需要的就是用一些前端基础进行以html的方式写好,绑定好所需要的数据进行构造,然后调用类库方法进行生成,下载即可,以下是我的业务代码:
<?php
namespace app\controller;
use app\BaseController;
use Mpdf\Mpdf;
class Markting extends BaseController
{
/*
* 生成PDF
* */
public function creatPdf()
{
$name = input('post.name');
$urgentRisk = input('post.urgentRisk');//紧急风险数量
$generalRisk = input('post.generalRisk');//一般风险数量
$riskIdList = input('post.riskIdList');;
$phone = input('post.phone');
$info = [
'name'=>$name,
'urgentRisk'=>$urgentRisk,
'generalRisk'=>$generalRisk,
'riskIdList'=>$riskIdList,
'phone'=>$phone
];
//根目录
$path = app()->getRootPath();
$str = $this -> creatHtmlModel($info);
//用html形式生成pdf
$html_content = $str;
$mpdfs = new Mpdf();
//自动分析录入内容字体
$mpdfs->autoScriptToLang = true;
$mpdfs->autoLangToFont = true;
//pdf文件存储路径
$fileUrl = $path.'public/pdf/'.$phone.'.pdf';
//以html为标准分析写入内容
$mpdfs->WriteHTML($html_content);
//生成文件
$mpdfs->Output($fileUrl);
//判断是否生成文件成功
if (is_file($fileUrl))
{
$url = 'http://saless.yyqadmin.com/pdf/'.$phone.'.pdf';
$condition = [
's_phone'=>$phone
];
$customerInfo = Customer::where($condition)->find();
$customerInfo -> save(['s_medical'=>$url]);
return json(['code'=>200,'data'=>['fileUrl'=>$url],'msg'=>'ok']);
}
else
{
return json(['code'=>201,'msg'=>'生成失败']);
}
}
/*
* 生成PDF的HTML
* */
function creatHtmlModel($info)
{
$str = '<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0">
<style>
.tip{
background-image: url("http://qn.yyqadmin.com/wechatImg/huo.png");
background-size: 160px;
background-repeat: no-repeat;
background-position: 0 90%;
line-height: 20px;
font-weight: bolder;
text-indent: 20px;
color: white;
display: inline;
font-size: 10px;
}
</style>
</head>
<body style="padding: 0;margin: 0">
<div class="doc_box" id="doc">
<div style="text-align: center">
<img src="http://qn.yyqadmin.com/wechatImg/kfdkZGbAdf.png" alt="" style="width: 500px">
</div>
</div>
<div style="line-height: 30px;margin-top: 60px">
<div style="text-align: center">
<h3>企法管家app体检报告</h3>
</div>
<div style="font-weight: bolder">
尊敬的'.$info['name'].'您好!
</div>
通过本次体检发现,您的企业面临的<span style="color: red">紧急风险</span>有'.$info['urgentRisk'].'点,<span style="color: orangered">一般风险</span>有'.$info['generalRisk'].'点,建议您聘请专业的法律顾问或联系我们对公司的业务进行全方位的风险排查,
对企业可能面临的法律风险建立有效的防控机制。
</div>
<div style="margin-top: 30px;font-weight: bolder;font-size: 24px">
您的企业存在 <span style="color: red">紧急风险</span>如下:
</div>';
//查询紧急风险
$riskIdList = explode(';',$info['riskIdList']);
$str_a = ''; //紧急风险内容
$str_b = '<div style="margin-top: 30px;font-weight: bolder;font-size: 24px">
您的企业存在 <span style="color: orangered">一般风险</span>如下:
</div>';//一般风险内容
$num_a = 1;
$num_b = 1;
for($i = 0;$i<count($riskIdList);$i++)
{
$riskInfo = Risk::find($riskIdList[$i]);
if($riskInfo['s_level'] === 0)
{
//查询解决方案
$solutionInfo = Solution::find($riskInfo['s_sid']);
$str_a .= '<div style="margin-top: 20px;line-height:30px">
<div class="tip">风险'.$num_a++.':</div>'.$riskInfo['s_riskname'].'
<div style="padding: 10px;background-color: #e5e5e5">'.$solutionInfo['s_solution'].'</div>
</div>';
}
else
{
//查询解决方案
$solutionInfo = Solution::find($riskInfo['s_sid']);
$str_b .= '<div style="margin-top: 20px;line-height:30px">
<div class="tip">风险'.$num_b++.':</div>'.$riskInfo['s_riskname'].'
<div style="padding: 10px;background-color: #e5e5e5">'.$solutionInfo['s_solution'].'</div>
</div>';
}
}
//结尾内容
$str_c = '<div style="line-height: 30px;margin-top: 40px">
<span style="color:orangered;">温馨提示:</span>以上结果为法律风险体检系统及大数据库综合分析结果,仅供贵单位参考,如需更多法律服务,可扫下方二维码进行下载
或致电 <span style="color:#1c3565;">400-000-1023</span>详细咨询。
<div style="text-align: center">
<img src="http://qn.yyqadmin.com/wechatImg/1_777336945_171_85_3_723463217_e189fa3227e6a93c3fd600eca47f9db3.png" alt="" style="width: 50%;height: 50%">
</div>
</div>
</body>
</html>';
return $str.$str_a.$str_b.$str_c;
}
}