吾生也有涯,而知也无涯~欢迎优化补充、指正!
近期有需求是:前端签名后结合平台协议生成签名文件
本例实现方法为:借助canvas生成签名图片,然后贴至协议底部保存为pdf文档
(本例在TP6环境下)借助TCPDF实现 官方提供多个例子点击查看
第一步: 引入组件
composer
composer require tecnickcom/tcpdf
或github直接下载导入
GitHub - tecnickcom/TCPDF: Official clone of PHP library to generate PDF documents and barcodes
放置于vendor下
public function saveSign(int $sign_id=0, int $accordId=10007) { if(!$sign_id){ return ; } $signUrl = (new UploadFile())->find($sign_id);//获取签名图片路径 if(empty($signUrl) || empty($signUrl->preview_url)){ return ; } $signUrl = $signUrl->preview_url; if(!file_exists('pdf')){ mkdir('pdf'); } if(file_exists('pdf/'.$sign_id.'.pdf')){ return ; } $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true,'UTF-8', false); $pdf->setCreator(PDF_CREATOR); $pdf->setAuthor('Nicola Asuni'); $pdf->setTitle('TCPDF Example 002'); $pdf->setSubject('TCPDF Tutorial'); $pdf->setKeywords('TCPDF, PDF, example, test, guide'); $pdf->setPrintHeader(false); $pdf->setPrintFooter(false); $pdf->setDefaultMonospacedFont(PDF_FONT_MONOSPACED); $pdf->setMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); $pdf->setAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM); $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); // droidsansfallback 中文字符 需额外引用放至vendor/tecnickcom/tcpdf/fonts $pdf->setFont('droidsansfallback', '', 14); $pdf->AddPage(); $center = (new Help()) -> find($accordId); // 协议内容 $txt = <<contentEOD; $pdf->writeHTMLCell(0, 0, '', '', $txt, 0, 1, 0, true, '', true); $pdf->Image($signUrl, '', '', 35, 20, '', '', 'B', false, 300, 'R', false, false, 1, false, false, false); $pdf->writeHTMLCell(0, 0, '', '', date('Y年m月d日'), 0, 1, 0, true, 'R', true); ob_clean(); // 可不加 $pdf->Output($_SERVER['DOCUMENT_ROOT'].'pdf/'.$sign_id.'.pdf', 'F'); }
效果如下
注: 需引入droidsansfallback解决中文乱码问题
来源地址:https://blog.csdn.net/Error_Q/article/details/127046850