插件官网:TCPDF
博主用的是tp6框架 、tcpdf插件
composer require tecnickcom/tcpdf --ignore-platform-reqs 后面是忽略平台要求的参数
---------------中文乱码start------------------
关于中文乱码问题:
网上说的下载字体放入fonts 利用tools生产字体的 亲测不用
找到tcpdf_config.php
将 define ('PDF_FONT_NAME_MAIN', 'helvetica');
修改为define ('PDF_FONT_NAME_MAIN', 'stsongstdlight');
将 define ('PDF_FONT_NAME_DATA', 'helvetica');
修改为 define ('PDF_FONT_NAME_DATA', 'stsongstdlight');
$pdf->SetFont('stsongstdlight', '', 14, '', true);
stsongstdlight字体,是Adobe Reader的默认简体中文字体,TCPDF中已经内置这个字体的配置文件,我们直接调用就好
---------------中文乱码end------------------
正文开始
引入下载的插件:require (xxx/tcpdf/tcpdf.php)
完整代码如下: public function exportPdf() { $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); $pdf->SetCreator(PDF_CREATOR); $pdf->SetAuthor('RoyalsZch'); $pdf->SetTitle('123'); $pdf->SetSubject('123pdf'); $pdf->SetKeywords('TCPDF, PDF, example, test, guide'); // set default header data $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 001', PDF_HEADER_STRING, array(0,64,255), array(0,64,128)); $pdf->setFooterData(array(0, 64, 0), array(0, 64, 128)); // set header and footer fonts $pdf->setHeaderFont(array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN)); $pdf->setFooterFont(array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA)); // set default monospaced font $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); // set margins $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); $pdf->SetHeaderMargin(PDF_MARGIN_HEADER); $pdf->SetFooterMargin(PDF_MARGIN_FOOTER); // set auto page breaks $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM); // set image scale factor $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); $pdf->SetFont('stsongstdlight', '', 14, '', true); // $pdf->Output('pdf'.'.pdf', 'D'); // add a page $pdf->AddPage(); // 随便写HTML $htmlUrl=app()->getRootPath() . 'public/uploads/1.html'; $html=file_get_contents($htmlUrl); // output the HTML content $pdf->writeHTML($html, true, false, true, false, ''); // $pdf->Image('https://gvrboxwit.obs.cn-east-2.myhuaweicloud.com:443/web/202303/202303301615200163657.png', 15, 140, 75, 113, 'JPG', 'http://www.tcpdf.org', '', true, 150, '', false, false, 1, false, false, false); // reset pointer to the last page $pdf->lastPage(); //D是下载,I是在线写入 $pdf->Output('royals' . '.pdf', 'D'); }
$htmlUrl=app()->getRootPath() . 'public/uploads/1.html';获取h5页面里面内容,写入生成的pdf中(h5大多数格式在生成的pdf中是可以生效的)图片也是可以的
h5demo:
<!DOCTYPE html> <style> body { width: 200mm; height: 240mm; margin: 15mm auto; padding: 0; font-size: 10.5pt; border: black 1px solid; /*font-family: "simsun";*/ } .container { height: 180mm; width: 155mm; margin: 100px auto; } table { letter-spacing: 2px; line-height: 30px; width: 100%; border-collapse: collapse; table-layout: fixed; } .content { margin-top: 40px; } .between { margin-left: 30px; margin-top: 20px; } .between input { width: 300px; margin: 3mm 0; } .foot { margin-top: 100px; } .foot table { letter-spacing: 3.5px; } .qz { margin-top: 30px; } .qz table { line-height: 40px; } .table { float: right; } .foot span { text-decoration: black solid; border-bottom: 1px black solid; } h1 { text-align: center; } </style> <body> <div class="container"> <h1>授权委托书</h1> <p><img src="xxxxx" alt="" width="100px" height="100px"></p> <div class="content"> <table> <tr> <td> 现本人不可撤销的向 ******* 银行</td> </tr> <tr> <td><strong>授权:</strong></td> </tr> <tr> <td style="letter-spacing: 6px"> 本人因办理 *********** 业务,已经</td> </tr> <tr> <td>向 0000000 公司支付首付款,现委托贵</td> </tr> <tr> <td>行将本人的信用卡(卡号 xxxxxxxxxxx)</td> </tr> <tr> <td>进行启用,并将其余款项 **** 元,</td> </tr> <tr> <td>以透支形式扣收后支付给该公司,入账账户信息如下:</td> </tr> </table> </div> <div class="between"> <table> <tr> <td colspan="1" class="table">账户名称:</td> <td colspan="4"><input type="text" value="{$info.zhmc|default=$gs_name}" id="zhmc"></td> </tr> <tr> <td colspan="1" class="table">账 号:</td> <td colspan="4"><input type="text" value="{$info.zh|default=$bank_number}" id="zh"></td> </tr> <tr> <td colspan="1" class="table"><span style="letter-spacing: 11px">开户</span>行:</td> <td colspan="4"> **********</td> </tr> </table> </div> <div class="foot"> <table> <tr> <td> <span>本人在签署此授权委托书之前,已认真仔细阅读上述内容,清</span></td> </tr> <tr> <td><span>楚知晓并自愿承担由此引发的相关法律责任。</span></td> </tr> </table> </div> <div class="qz"> <table> <tr> <td colspan="5"></td> <td colspan="4">授权人:</td> </tr> <tr> <td colspan="6"></td> <td colspan="3">2023年07月05日</td> </tr> </table> </div> </div> </body>