. 今天在做合同签章时,需要导出PDF,因此,我就引入了tcpdf。直接上代码如下:
/**
* 详情&&打印
* @param $params
* @return array
*/
public function detail($params)
{
$first = DB::table('contract_file as f')->select('f.*', 'c.name','c.desc')
->leftJoin('contract_template_category as c','f.template_id','=','c.id')
->where('c.id',$params['id'])
->first();
if(!$first || empty($first->desc)){
return ['msg'=>'合同不存在','status'=>false];
}
$content = htmlspecialchars_decode($first->desc,ENT_QUOTES);
$emp = DB::table('employees as e')->where('e.uuid',$first->account_uuid)
->select('fullName', 'employeeID', 's.name as sub_company_name',
'd.deptName', 'des.designation', 'l.job_level_name',
'employees_status', 'employees_type', 'joiningDate',
'conversion_date', 'contract_period', 'contract_end',
'id_type', 'id_card', 'gender',
'marital_status', 'culture_type', 'permanent_type',
'registered_residence', 'nation', 'mobileNumber',
'e.company_email as emp_company_email','e.email as emp_email','localAddress',
'c.company_name','address','c.name as company_relation_name','contact',
'c.email as company_email'
)
->leftJoin('companies as c','c.id','=','e.company_id')
->leftJoin('designation as des','des.id','=','e.designation')
->leftJoin('department as d','d.id','=','e.deptid')
->leftJoin('employees_job_level as l','l.id','=','e.job_level_id')
->leftJoin('sub_companies as s','s.id','=','e.company_branch_id')
->first();
//获取所有的标签
//$ids = ContractDynamicField::pluck('id')->toArray();
$labels = ContractDynamicField::pluck('label')->toArray();
$titles = ContractDynamicField::pluck('title')->toArray();
//$id_labels = array_combine($labels, $ids);
$title_labels = array_combine($labels, $titles);
$new_content = $content;
preg_match_all('/\{.*?\}/i', $content, $matches);
$keyVal = $this->labelKey();
$valKey = array_flip($keyVal);
$label_value_arr = array();
foreach ($title_labels as $tk=>$title){
if(isset($valKey[$title])){
$k = $valKey[$title];
$label_value_arr[$tk] = $emp->$k;
}
}
//获取标签对应的ID
if (!empty($matches[0])) {
foreach ($matches[0] as $match) {
try {
if (!in_array($match, $labels)) {
return ['status' => false, 'msg' => trans('service.20540')];//您用了系统里没有的标签
}
//替换
if (isset($label_value_arr[$match])) {
$new_content = str_replace($match, $label_value_arr[$match], $new_content);
}
} catch (\Exception $exception) {
return ['status' => false, 'msg' => trans('service.20096')];//保存到数据库出错
}
}
}
$title = $first->code;//标题
$html = $new_content;//内容
if($params['is_export']==1){
return $this->downloadPDF($html,$title);
}
return $html;
}
. 导出PDF
/**
* @param $html
* @param $title
* return mixed
*/
public function downloadPDF($html,$title)
{
$pdf = new \TCPDF();
// 设置文档信息
//$pdf->SetCreator(PDF_CREATOR);
//$pdf->SetAuthor('这里是作者');
//$pdf->SetTitle('这里是标题');
//$pdf->SetSubject('这里是主题');
//$pdf->SetKeywords('这里是关键字');
// 设置页眉和页脚信息
$pdf->SetHeaderData('', 30, '', '', [0, 64, 255], [0, 64, 128]);
$pdf->setFooterData([0, 64, 0], [0, 64, 128]);
// 设置页眉和页脚字体
$pdf->setHeaderFont(['stsongstdlight', '', '10']);
$pdf->setFooterFont(['helvetica', '', '8']);
// 设置默认等宽字体
$pdf->SetDefaultMonospacedFont('courier');
// 设置间距
$pdf->SetMargins(15, 15, 15);//页面间隔
$pdf->SetHeaderMargin(5);//页眉top间隔
$pdf->SetFooterMargin(10);//页脚bottom间隔
$pdf->setCellPaddings('',5,'',5);//设置上下左右间距
// 设置分页
$pdf->SetAutoPageBreak(true, 25);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// set default font subsetting mode
$pdf->setFontSubsetting(true);
//设置字体 stsongstdlight支持中文
$pdf->SetFont('stsongstdlight', '', 14);
//设置加密和密码
//$pdf->SetProtection($permissions = array('print', 'modify', 'copy', 'annot-forms', 'fill-forms', 'extract', 'assemble', 'print-high'), $user_pass = '123456', $owner_pass = null, $mode = 0, $pubkeys = null );
//换行符
$pdf->Ln(5);
//第一页
$pdf->AddPage();
//设置文本背景阴影
$pdf->setTextShadow(array('enabled' => true, 'depth_w' => 0.2, 'depth_h' => 0.2, 'color' => array(196, 196, 196), 'opacity' => 1, 'blend_mode' => 'Normal'));
// Print text using writeHTMLCell()
$pdf->writeHTMLCell(0, 10, '', '', $html, 0, 1, 0, true, '', true);
//标题
$title = $title.'.pdf';
//输出PDF
$pdf->Output($title, 'D');//默认是I:在浏览器中打开,D:下载,F:在服务器生成pdf ,S:只返回pdf的字符串
}
```