php 基於tcpdf插件生成pdf


之前在公司做了個項目,,需要導出pdf合同,,在網上找了很久,選擇用了tcpdf插件,,具體的插件網上可以搜到,中間遇到了很多的坑,慢慢的填。

先下好插件放到指定文件夾下

 

然后使用tcpdf插件里html生成pdf,,,不過有好多復雜html的樣式tcpdf識別不了,,,簡單點的可以。

 

require_once(FW_PATH.'/plugins/tcpdf/tcpdf.php');

class Controller_Pdf_Gps extends Controller_Base {
/**
* 
* @param string $html
* @param string $title
* @author wy
*/
public function actionWriteToPdf(){
$contractCarrentId = isset($_REQUEST['contract_carrent_id']) ? $_REQUEST['contract_carrent_id'] : 0;
$productTypeId = isset($_REQUEST['product_type_id']) ? $_REQUEST['product_type_id'] : 0;
$didiType = isset($_REQUEST['didi_type']) ? $_REQUEST['didi_type'] : 0;//滴滴合同沒有附件1、2標識, 用didi_type區分
//I,默認值,在瀏覽器中打開;D,PDF文件會被下載下來;F,文件會被保存在服務器中;S,PDF會以字符串形式輸出;E:PDF以郵件的附件輸出。
$title = isset($_REQUEST['title']) ? $_REQUEST['title'] : 'GPS安裝確認及使用規則告知書';
$headerLogo = isset($_REQUEST['header_logo']) ? $_REQUEST['header_logo'] : 'caixin1.png';
$headerLogoWidth = isset($_REQUEST['header_logo_width']) ? $_REQUEST['header_logo_width'] : 30;
$headerTitle = isset($_REQUEST['header_title']) ? $_REQUEST['header_title'] : '';
$headerString = isset($_REQUEST['header_string']) ? $_REQUEST['header_string'] : ' 發過火發過火發過火有限公司';

if($contractCarrentId <= 0) {
return $this->error('合同(租車)ID錯誤');
}
if($productTypeId <= 0) {
return $this->error('產品類型ID錯誤');
}

//contract_carrent
$contractCarrent = Remote::instance()->get(CAIXIN_BASE_DOMIAN, 'contract/carrent/detail', array('contract_carrent_id' => $contractCarrentId));
if($contractCarrent['code'] != 200 || !isset($contractCarrent['data']['contract_carrent'])) {
return $this->error('獲取合同(租車)信息錯誤');
}
$contractCarrent = $contractCarrent['data']['contract_carrent'];

//application_control
$appControl = Remote::instance()->get(CAIXIN_BASE_DOMIAN, 'application/control/detail', array('application_control_id' => $contractCarrent['application_control_id']));
if($appControl['code'] != 200 || !isset($appControl['data']['application_control'])) {
return $this->error('獲取申請流程控制信息錯誤');
}
$appControl = $appControl['data']['application_control'];

$html = '';
$html = $this->personInfo($appControl, $title, $didiType);
if($html == '') {
return $this->error('獲取不到轉換頁面');
}

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetTitle($title); //設置文件的title
$pdf->SetHeaderData($headerLogo, $headerLogoWidth, $headerTitle, $headerString);//設置頭部,比如header_logo,header_title,header_string及其屬性
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN)); //設置頁頭字體
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA)); //設置頁尾字體
$pdf->setPrintHeader(true); //頁面頭部橫線 false取消
$pdf->setPrintFooter(true); //頁面底部更顯 false取消
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(PDF_MARGIN_LEFT, 13, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(0);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$pdf->SetAutoPageBreak(false, PDF_MARGIN_BOTTOM);//自動分頁
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->AddPage();//增加一個頁面
$pdf->setPageMark();
$pdf->SetFont('stsongstdlight', '', 13);

$pdf->writeHTML($html, true, true, true, true, '');
// $pdf->writeHTML($html, true, 0, true, true);
// $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, 'C', true);
$pdf->lastPage();
ob_end_clean();

//寫入文件
$time = time();
$content = $pdf->Output($time.'.pdf', 'S');
if(in_array($productTypeId, array(4,5))) {

$category_path = WWW_ROOT.'/tmp_zip/合同'.$contractCarrentId.'/文件夾/文件名';
$file_path = WWW_ROOT.'/tmp_zip/合同'.$contractCarrentId.'/文件夾/文件名/'.$title.date('Ymd',time()).'.pdf';
}else if(in_array($productTypeId, array(6,7))) {

$category_path = WWW_ROOT.'/tmp_zip/合同'.$contractCarrentId.'/文件夾/文件名';
$file_path = WWW_ROOT.'/tmp_zip/合同'.$contractCarrentId.'/文件夾/文件名/'.$title.date('Ymd',time()).'.pdf';
}
if (!file_exists($category_path)){
mkdir($category_path,0777,true);
}
$fp = fopen($file_path,'w') or die("Unable to open file!");
fwrite($fp,$content);
fclose($fp);
return $this->output('ok');
}

以上有些業務。

/**

     * 個人

     * @author yxf

     */

    private function personInfo($appControl, $title, $didiType) {

        //門店

        $store = Remote::instance()->get(CAIXIN_BASE_DOMIAN, 'store/detail', array('store_id' => $appControl['store_id']));

        if($store['code'] != 200 || !isset($store['data']['store'])) {

            return $this->error('獲取門店信息錯誤');

        }

        $store = $store['data']['store'];

        

        if($appControl['customer_type'] != 1) {

            return $this->error('客戶性質錯誤');

        }

        

        //申請單(自然人)

        $appPerson = Remote::instance()->get(CAIXIN_BASE_Y_DOMIAN, 'applicationperson/detail', array('application_person_id' => $appControl['application_person_id']));

        if($appPerson['code'] != 200 || !isset($appPerson['data']['application_person'])) {

            return $this->error($appPerson['msg']);

        }

        $appPerson = $appPerson['data']['application_person'];

        $appPerson['name'] = !empty($appPerson) ? $appPerson['name'] : '';

        /* 附件二:GPS安裝確認及使用規則告知書  */

        $html = <<<EOD

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>{$title}</title>

    <style>

        body {

            padding: 10px;

        }

 

        .color-red {

            color: red;

            text-decoration: underline;

        }

 

        h1 {

            text-align: center;

            font-size: 22px;

            position: relative;

        }

 

        h3 span {

            position: absolute;

            left: 0;

            top: 0;

        }

 

        h2 {

            font-size: 16px;

            margin: 5px 0;

            position: relative;

        }

 

        h3 {

            font-size: 16px;

            text-align: center;

            position: relative;

            margin-top: 50px;

            margin-bottom: 50px;

        }

 

        p {

            font-size: 14px;

        }

 

        table {

            border-collapse: collapse;

            width: 100%;

        }

 

        th {

            border: 1px solid #000;

        }

 

        td {

            border: 1px solid #000;

            padding: 5px 10px;

            font-size: 13px;

            line-height: 30px;

            height: 30px;

        }

 

        td img {

            max-width: 100%;

        }

 

        .title-xl {

            font-size: 16px;

        }

 

        .title {

            background: #d4d4d4;

            width: 110px;

            text-align: center;

        }

 

        .underline_xl {

            width: 200px;

            display: inline-block;

            border-bottom: 1px solid #000;

            vertical-align: bottom;

        }

 

        .underline_l {

            width: 150px;

            display: inline-block;

            border-bottom: 1px solid #000;

            vertical-align: bottom;

        }

 

        .underline_s {

            width: 50px;

            display: inline-block;

            border-bottom: 1px solid #000;

            vertical-align: bottom;

        }

 

        .item {

            display: table;

        }

 

        .text-no {

            display: table-cell;

            width: 50px;

            padding-left: 20px;

 

        }

 

        .text-no + span {

            display: table-cell;

 

        }

 

        .check {

            border: 1px solid #000;

            display: inline-block;

            width: 14px;

            height: 14px;

            position: relative;

            top: 3px;

 

        }

 

        .uncheck {

            border: 1px solid #000;

            display: inline-block;

            width: 14px;

            height: 14px;

            position: relative;

            top: 3px;

            overflow: hidden;

 

        }

 

        .uncheck:after {

            content: 'X';

            position: absolute;

            top: 0;

            left: 0px;

            font-size: 24px;

            font-style: normal;

            display: block;

            font-weight: 100;

            width: 14px;

            height: 14px;

            line-height: 16px;

            text-align: center;

        }

 

        .unline {

            border-bottom: 1px solid #000;

        }

 

        .unline .underline_s {

            border: 0;

            vertical-align: bottom;

        }

 

        .no_border {

            border: 0;

        }

 

        .width_s {

            width: 110px;

            text-align: center;

        }

    </style>

</head>

 

<body>

<p style="font-weight: bold">

EOD;

$html2 = '';    

if($didiType != 1) {        

$html2 .= <<<EOD

附件二:使用規則告知書

EOD;

}

$html2 .= <<<EOD

</p>

<h1 style="text-align:center;">使用規則告知書</h1>

<br/><br/>

<p>尊敬的客戶:{$appPerson['name']}<br/>

   

</p>

<div style="text-align:right;">

    <p>公司:{$store['invoice']}

    </p>

</div>

<br/>

<div style="text-align:right;">

    <p>

      年

      月

      日</p>

</div>

<p><b>本人已閱讀、理解並同意上述規則條款,自願執行上述規則,如因違反上述規則導致的任何后果由本人自行承擔。</b>

</p>

<div style="text-align: right;">

    <p>客戶(簽字):         </p>

    <p>

      年

      月

      日</p>

</div>

</body>

</html>

EOD;

        return $html1.$html2;

    }

  

以上具體的html,自行設計。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM