<?php
namespace base;
use app\service\ResourcesService;
use TCPDF;
* PDF
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2023-03-07
* @desc html转pdf采用mpdt库、配置转pdf采用tcpdf库
*/
class PDF extends TCPDF
{
public $filename;
public $root_path;
public $path;
public $output_type;
public $title;
public $is_header;
public $is_footer;
public $header_logo;
public $header_name;
public $footer_content;
public $background_images;
public $watermark;
* 参数初始化
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2023-03-07
* @desc description
* @param [array] $params [输入参数]
*/
public function __construct($params = [])
{
parent::__construct();
$this->filename = empty($params['filename']) ? date('YmdHis').'.pdf' : $params['filename'];
$this->output_type = empty($params['output_type']) ? 'I' : $params['output_type'];
if(!empty($params['title']))
{
$this->title = $params['title'];
}
$this->is_header = isset($params['is_header']) ? $params['is_header'] : false;
$this->is_footer = isset($params['is_footer']) ? $params['is_footer'] : false;
if(!empty($params['header_logo']))
{
$this->header_logo = $params['header_logo'];
}
if(!empty($params['header_name']))
{
$this->header_name = $params['header_name'];
}
if(!empty($params['footer_content']))
{
$this->footer_content = $params['footer_content'];
}
if(!empty($params['background_images']))
{
$this->background_images = $params['background_images'];
}
if(!empty($params['watermark']))
{
$this->watermark = $params['watermark'];
}
$this->root_path = isset($params['root_path']) ? $params['root_path'] : ROOT.'public';
$this->path = isset($params['path']) ? $params['path'] : DS.'static'.DS.'upload'.DS.'file'.DS.'pdf'.DS.date('Y').DS.date('m').DS.date('d').DS;
}
* TCPDF基础初始化
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2023-03-07
* @desc 采用TCPDF组件
* @param [array] $params [输入参数]
* @return [object] [实例对象]
*/
public function BaseInit($params = [])
{
$this->setPrintHeader($this->is_header);
$this->setPrintFooter($this->is_footer);
$this->setFooterData(array(0,64,0), array(0,64,128));
$this->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$this->SetFooterMargin(PDF_MARGIN_FOOTER);
$this->setAutoPageBreak(true, 15);
$this->setMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$this->SetDefaultMonospacedFont('courier');
$this->setImageScale(1.25);
$this->SetFont('stsongstdlight', '', 12, '', true);
if(!empty($this->title))
{
$this->setTitle($this->title);
}
$this->AddPage();
}
* 头设置
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2023-03-09
* @desc description
*/
public function Header()
{
parent::Header();
$this->BackgroundImage();
if(!empty($this->header_logo))
{
$this->Image($this->header_logo, 15, 4, 30, 6);
}
if(!empty($this->header_name))
{
$this->SetFont('stsongstdlight', '', 12, '', true);
$this->Cell(0, 16, $this->header_name, 0, 1, 'R');
}
}
* 尾设置
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2023-03-09
* @desc description
*/
public function Footer()
{
parent::Footer();
$this->BackgroundImage();
if(!empty($this->footer_content))
{
$this->SetY(-15);
$this->SetFont('stsongstdlight', '', 8);
$this->setTextColor(136, 136, 136);
$this->Cell(0, 15, $this->footer_content, 0, 0, 'C');
}
}
* 设置背景图
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2023-03-09
* @desc description
*/
public function BackgroundImage()
{
if(!empty($this->background_images))
{
$break_margin = $this->getBreakMargin();
$auto_page_break = $this->AutoPageBreak;
$this->SetAlpha(0.08);
$this->SetAutoPageBreak(false, 0);
$this->Image($this->background_images, 0, 0, 210, 297, '', '', '', false, 300, '', false, false, 0);
$this->SetAutoPageBreak($auto_page_break, $break_margin);
$this->setPageMark();
$this->SetAlpha(1);
}
}
* html转PDF
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2023-03-07
* @desc 采用MPDF组件
* @param [array] $params [输入参数]
*/
public function HtmlToPDF($params = [])
{
if(empty($params['html']))
{
return DataReturn(MyLang('common_extend.base.pdf.content_empty_tips'), -1);
}
$pdf = new \Mpdf\Mpdf([
'autoScriptToLang' => true,
'autoLangToFont' => true,
'default_font' => 'gb',
'mode' => 'utf-8',
]);
if(!empty($this->watermark))
{
$pdf->SetWatermarkImage($this->watermark, 0.5);
$pdf->showWatermarkImage = true;
}
if($this->is_header && (!empty($this->header_logo) || !empty($this->header_name)))
{
$header = '<table width="100%" style="margin:0 auto;border-bottom: 1px solid #666; vertical-align: middle; font-family:serif;"><tr>';
$header .= '<td width="40%">';
if(!empty($this->header_logo))
{
$header .= '<img src="'.$this->header_logo.'" height="15" />';
}
$header .= '</td>';
$header .= '<td width="40%" align="right" style="font-size: 9pt; color: #666;">';
if(!empty($this->header_name))
{
$header .= $this->header_name;
}
$header .= '</td>';
$header .= '</tr></table>';
$pdf->SetHTMLHeader($header);
}
if($this->is_footer)
{
$footer = '<table width="100%" style=" vertical-align: bottom; font-family:serif; font-size: 9pt;"><tr>
<td width="10%"></td>';
if(!empty($this->footer_content))
{
$footer .= '<td width="80%" align="center" style="font-size:14px;color:#999">'.$this->footer_content.'</td>';
}
$footer .= '<td width="10%" align="right" style="color: #666;">{PAGENO}/{nb}</td>
</tr></table>';
$pdf->SetHTMLFooter($footer);
}
$dir = $this->IsMkdir();
if($dir['code'] != 0)
{
return $dir;
}
if(!empty($this->title))
{
$pdf->SetTitle($this->title);
}
$pdf->WriteHTML($params['html']);
$type = empty($params['type']) ? 'I' : $params['type'];
$file = ($type == 'F') ? $dir['data'].$this->filename : $this->filename;
$pdf->Output($file, $type);
if($type == 'F')
{
$result = [
'dir' => $dir['data'].$this->filename,
'root' => $this->root_path,
'path' => $this->path,
'filename' => $this->filename,
'url' => ResourcesService::AttachmentPathViewHandle($this->path.$this->filename),
];
return DataReturn(MyLang('operate_success'), 0, $result);
}
die;
}
* 路径不存在则创建
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-06-29
* @desc description
*/
private function IsMkdir()
{
$dir = str_replace(['//', '\\\\'], ['/', '\\'], $this->root_path.$this->path);
if(!is_dir($dir))
{
if(mkdir($dir, 0777, true) === false)
{
return DataReturn(MyLang('common_extend.base.pdf.dir_create_fail_tips'), -1);
}
}
return DataReturn(MyLang('operate_success'), 0, $dir);
}
}
?>