1

由于之前工作需要每月份生成一份pdf样式的账单给客户,于是百度了一番,决定使用TCPDF工具,网上中文文档少的可怜,在此记录下自己写的部分代码。
项目结构如下:(有需求的联系博主)
QQ截图20200305104627.png
入口调用文件为pdf.php,代码如下:

<?php
require_once('tcpdf.php'); 

/**
 * @param $PDFpath  PDF保存路径
 * @param $OrderNo  账单号做文件名
 * @param $Email    用户邮箱
 * @param $Total    总金额 美元
 * @param $Currency 币种符号
 * @param $Payables  汇率后应支付金额
 * @param $Repayment 截至还款日期
 * @param $BeforeMonth 生成账单月份
 * @param $EngAmount 英文格式金额
 * @param $AddressInfo 用户地址
 * @param $CName 货币名称
 */
function CreatePDF($PDFpath,$OrderNo,$Email,$Total,$Currency,$Payables,$Repayment,$BeforeMonth,$EngAmount,$AddressInfo,$CName) {
    //实例化 
    $pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false); 

    // 设置文档信息 
    /*$pdf->SetCreator('Helloweba'); 
    $pdf->SetAuthor('yueguangguang'); */
    /*$pdf->SetTitle('Welcome to shewentm.com!'); 
    $pdf->SetSubject('TCPDF Tutorial'); 
    $pdf->SetKeywords('TCPDF, PDF, PHP'); */
    
    // 设置页眉和页脚信息 
    $pdf->SetHeaderData('', 15, '','BEIJING SHENWEN INTELLECTUAL PROPERTY MANAGEMENT CO.LTD.
    Suite 1607A Dahangjiye Mansion     Tel:86-10-8268 5045 Fax:86-10-8268 4574
    No.33 North Road Renda,Haidian District,Beijing 10080,PR China',  
          array(0,0,0), array(0,64,128)); 
    $pdf->setPrintFooter(false);
    //$pdf->setFooterData('', 15, '','yejiao',  array(0,0,0), array(0,64,128)); 
     
    // 设置页眉和页脚字体 
    $pdf->setHeaderFont(Array('cid0kr', '', '12')); 
    $pdf->setFooterFont(Array('cid0kr', '', '8')); 
     
    // 设置默认等宽字体 
    $pdf->SetDefaultMonospacedFont('courier'); 
     
    // 设置间距 
    $pdf->SetMargins(15, 27, 15); 
    $pdf->SetHeaderMargin(5); 
    $pdf->SetFooterMargin(10); 
     
    // 设置分页 
    $pdf->SetAutoPageBreak(TRUE, 25); 
     
    // set image scale factor 
    $pdf->setImageScale(1.25); 
     
    // set default font subsetting mode 
    $pdf->setFontSubsetting(true); 
     
    //设置字体
    $pdf->SetFont('cid0kr', '', 13); 
    //$pdf->SetFont('cid0jp', '', 13); 
    //$pdf->SetFont('times', '', 13); 
     
    $pdf->AddPage(); 
     
    //设置左边距
    $pdf->SetLeftMargin(15); 
    $date = date('Y-m-d');    //生成账单日期
    $fill = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
    $contents = "<br/><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b><u>INVOICE</b></u></p>
                <p><b>$fill No.$OrderNo</b></p>
                <p><b>$fill $date</b></p>
                <p></p><p>
                <p><b> $AddressInfo </b></p><p></p>
                <p><b><u>Re:Trademark Service in China</u></b></p>
                <p></p>
                <p><b>&nbsp;&nbsp;&nbsp;&nbsp;Total Cost : $ $Total(Please access SHEWEN to check expense details)</b></p>
                <p><b>&nbsp;&nbsp;&nbsp;&nbsp;Unit in $CName : $Currency $Payables </b></p>
                <p></p>
                <p><b><u>TOTAL:</u></b></p>
                <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b><u>$EngAmount</u></b></p>
                <p></p><p></p><p></p>
                <p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p>
                <hr/>
                <br/>Please pay this bill within 15 days by wire transfer to our account as follows:<br/>
                Beneficiary's bank:HUA XIA BANK H.O<br/>
                SWIFT BIC:HXBKCNBJ030<br/>
                Beneficiary:BEIJING SHEWEN INTELLECTUAL PROPERTY MANAGEMENT CO.LTD<br/>
                A/C No:10277000000660845";
    $pdf->Image('logo.jpg',0,170,200);    //显示图片
    $pdf->writeHTML($contents);    
    $name =  $OrderNo . '.pdf';
    $path =  $PDFpath . $name;
    //在浏览器打开PDF
    $pdf->Output($path, 'I');
    //生成PDF文件
    //$res = $pdf->Output($name, 'S');
    //file_put_contents($path, $res);
}

需要注意的是,因为字体问题,有很多字体不支持特殊符号。博主在试了很多字体文件,最终找到droidsansfallback字体支持特殊符号(并不确定支持所有特殊符号)。
QQ截图20200305110131.png


如若时光萧瑟去丶
111 声望9 粉丝

weakChickenPeng.