thinkphp5生成PDF个人信息表 pdf 格式下载 - Go语言中文社区

thinkphp5生成PDF个人信息表 pdf 格式下载


生成的样式为

需要下载插件https://sourceforge.net/projects/tcpdf/files/latest/download

下载好后解压到

以下是生成pdf 代码吧这个函数写在thinkphp 公共函数中即可

function pdf($html='<h1 style="color:red">这是一个测试文件,生成pdf文件!</h1>',$file_name='_Education.pdf'){

            vendor('tcpdf.tcpdf');
            $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 001');
            $pdf->SetSubject('TCPDF Tutorial');
            $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
            // 是否显示页眉
            $pdf->setPrintHeader(false);
            // 设置页眉显示的内容
            $pdf->SetHeaderData('http://192.168.31.153/wanyoujiaoyu/public/static/admin/img/logo.png', 60, 'baijunyao.com', '白俊遥博客', array(0,64,255), array(0,64,128));
            // 设置页眉字体
            $pdf->setHeaderFont(Array('dejavusans', '', '12'));
            // 页眉距离顶部的距离
            $pdf->SetHeaderMargin('5');
            // 是否显示页脚
            $pdf->setPrintFooter(true);
            // 设置页脚显示的内容
            $pdf->setFooterData(array(0,64,0), array(0,64,128));
            // 设置页脚的字体
            $pdf->setFooterFont(Array('dejavusans', '', '10'));
            // 设置页脚距离底部的距离
            $pdf->SetFooterMargin('10');
            // 设置默认等宽字体
            $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
            // 设置行高
            $pdf->setCellHeightRatio(1);
            // 设置左、上、右的间距
            $pdf->SetMargins('10', '10', '10');
            // 设置是否自动分页  距离底部多少距离时分页
            $pdf->SetAutoPageBreak(TRUE, '15');
            // 设置图像比例因子
            $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
            if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
                require_once(dirname(__FILE__).'/lang/eng.php');
                $pdf->setLanguageArray($l);
            }
            $pdf->setFontSubsetting(true);
            $pdf->AddPage();
            // 设置字体
            $pdf->SetFont('stsongstdlight', '', 14, '', true);
           // $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
            $pdf->writeHTML($html);
            $pdf->Output($file_name, 'D');

    }

生成pdf 格式代码如下

 public function pdf($data){
        $user_info=$data['user_info'];
        $cn_name=isset($user_info['user_info']['cn_name'])?$user_info['user_info']['cn_name']:"";
        $last_name=isset($user_info['user_info']['last_name'])?$user_info['user_info']['last_name']:"";
        $first_name=isset($user_info['user_info']['first_name'])?$user_info['user_info']['first_name']:"";
        $dob=isset($user_info['user_info']['dob'])?$user_info['user_info']['dob']:"";
        $gender=isset($user_info['user_info']['gender'])?$user_info['user_info']['gender']:"";
        $cn_address=isset($user_info['user_info']['cn_address'])?$user_info['user_info']['cn_address']:"";
        $cn_address_translation=isset($user_info['user_info']['cn_address_translation'])?$user_info['user_info']['cn_address_translation']:"";
        $au_address=isset($user_info['user_info']['au_address'])?$user_info['user_info']['au_address']:"";
        $email=isset($user_info['user_info']['email'])?$user_info['user_info']['email']:"";
        $country_code=isset($user_info['user_info']['country_code'])?'+'.$user_info['user_info']['country_code']:"";
        $phone=isset($user_info['user_info']['phone'])?$user_info['user_info']['phone']:"";

        $passport_no=isset($user_info['user_info']['passport_no'])?$user_info['user_info']['passport_no']:"";
       
        if($passport_no){
            $exp_date=isset($user_info['user_info']['exp_date'])?date('m-d-Y',strtotime($user_info['user_info']['exp_date'])):"";
        }else{
             $exp_date='';
        }
        $visa_subclass=isset($user_info['user_info']['visa_subclass'])?$user_info['user_info']['visa_subclass']:"";
        if($visa_subclass){
            $visa_ex_date=isset($user_info['user_info']['visa_ex_date'])?date('m-d-Y',strtotime($user_info['user_info']['visa_ex_date'])):"";
        }else{
            $visa_ex_date='';
        }
        $membership=isset($user_info['user_info']['membership'])?$user_info['user_info']['membership']:"";
        if($membership){
            $cover_end_date=isset($user_info['user_info']['cover_end_date'])?date('m-d-Y',strtotime($user_info['user_info']['cover_end_date'])):"";
         }else{
            $cover_end_date='';
         }
    
        if($data['user_info']['user_coe']){
            $coe_html='
            <div></div>
             <table border="1" cellpadding="10">
               <tr style="border: 1px solid #464747;height:25px;">
                    <td style="background-color:#fad3c7;width:155px">课程名</td>
                    <td  style="background-color:#fad3c7;width:145px">学校名</td>
                    <td style="background-color:#fad3c7;width:280px">时间</td>
                    <td  style="background-color:#fad3c7;width:100px">是否完成</td>
               </tr>
            ';
            $str='';
            foreach ($data['user_info']['user_coe'] as $key => $value) {
                $str.='<tr style="border: 1px solid #464747;height:25px;">';
                $str.='<td style="width:155px">'.$value['courses']['en_name'].'</td>';
                $str.='<td  style="width:145px">'.$value['school']['name'].'</td>';
                $str.='<td style="width:280px">'.date('d-m-Y',strtotime($value['course_start_date'])).' to '.date('d-m-Y',strtotime($value['course_end_date'])).'('.getMonthNum($value['course_start_date'],$value['course_end_date']).'months)</td>';

                if(strpos($value['status'],'"active"')){
                    $status='进行中';
                }else if(strpos($value['status'],'"expired"')){
                    $status='过期';
                }else if(strpos($value['status'],'"cancellation"')){
                    $status='取消';
                }else{
                    $status='取消';
                }
                $str.='<td  style="width:100px">'.$status.'</td>';
                $str.='</tr>';
            }
            $user_info_user_coe=$coe_html.$str.'</table>';
        }else{
             $user_info_user_coe='';
        }
        $data['gap']=collection($data['gap'])->toArray();
        if($data['gap']){
            $gap_html='<div></div>
          <table border="1" cellpadding="10">
           <tr style="border: 1px solid #464747;height:25px;">
                <td style="background-color:#fad3c7;width:200px">GAP 原因</td>
                <td  style="background-color:#fad3c7;width:200px">GAP 性质</td>
                <td style="background-color:#fad3c7;width:280px">时间</td>
           </tr>';
          $str='';
          foreach ($data['gap'] as $key => $value) {
              $str.='<tr style="border: 1px solid #464747;height:25px;">';
              $str.='<td style="width:200px">'.$value['cn_reason'].'</td>';
              $str.='<td  style="width:200px">'.str_replace("<", '&lt;', $value['en_reason']).'</td>';
              $str.='<td style="width:280px">'.$value['start'].' to '.$value['end'].'('.getMonthNum($value['start'],$value['end']).'months)</td>';
              $str.='</tr>';
          }
          $user_info_user_gap=$gap_html.$str.'</table>';
        }else{
           $user_info_user_gap='';
        }
        $html='
        <img src="'.$user_info['logo'].'" class="-logo" style="  display: block; width: 120px; height: auto;"/>
          <h3 style="font-size: 24px;width: 120px;color: #7d7d7d;letter-spacing: 2px;">万友教育</h3>
          <h5 style="font-size: 12px;margin: 4px 0 8px;width: 120px;-webkit-transform: scale(0.85);-ms-transform: scale(0.85);transform: scale(0.85);color: #babab9;">ONE U EDUCATION</h5>
          <h5 style="  font-weight: 700;color: #525252;font-size: 20px;margin-top: 10px;">'.$cn_name.' 同学申请表</h5>
          <p style=" margin-top: 8px;font-size: 16px;">*此表由万友科技自动生成,如有需求联系我们订购*</p>
          <table border="1" cellpadding="10">
           <tr style="border: 1px solid #464747;height:25px;">
                <td style="background-color:#fad3c7;width:120px">LastName</td>
                <td  style="width:220px"><p style="font-size:16px">'.$last_name.'</p></td>
                <td style="background-color:#fad3c7;width:120px">FirstName</td>
                <td  style="width:220px"><p style="font-size:16px">'.$first_name.'</p></td>
           </tr>
            <tr style="border: 1px solid #464747;height:25px;">
                <td style="background-color:#fad3c7;width:120px">DOB</td>
                <td  style="width:220px"><p style="font-size:16px">'.$dob.'</p></td>
                <td style="background-color:#fad3c7;width:120px">Gender</td>
                <td  style="width:220px"><p style="font-size:16px">'.$gender.'</p></td>
           </tr>
            <tr style="border: 1px solid #464747;height:25px;">
                <td style="background-color:#fad3c7;width:120px">Chinses address:</td>
                <td  style="width:220px"><p style="font-size:16px">'.$cn_address.'</p></td>
                <td style="background-color:#fad3c7;width:120px">Address Translate:</td>
                <td  style="width:220px"><p style="font-size:16px">'.$cn_address_translation.'</p></td>
           </tr>
            <tr style="border: 1px solid #464747;height:25px;">
                <td style="background-color:#fad3c7;width:120px">Australia address:</td>
                <td  style="width:220px"><p style="font-size:16px">'.$au_address.'</p></td>
                <td style="background-color:#fad3c7;width:120px">Notionality:</td>
                <td  style="width:220px"><p style="font-size:16px">Chinese</p></td>
           </tr>
            <tr style="border: 1px solid #464747;height:25px;">
                <td style="background-color:#fad3c7;width:120px">Email</td>
                <td  style="width:220px"><p style="font-size:16px">'.$email.'</p></td>
                <td style="background-color:#fad3c7;width:120px">Mobile</td>
                <td  style="width:220px"><p style="font-size:16px">'.$country_code.' '.$phone.'</p></td>
           </tr>
            <tr style="border: 1px solid #464747;height:25px;">
                <td style="background-color:#fad3c7;width:120px">Passport No:</td>
                <td  style="width:220px"><p style="font-size:16px">'.$passport_no.'</p></td>
                <td style="background-color:#fad3c7;width:120px">Expiry Date:</td>
                <td  style="width:220px"><p style="font-size:16px">'.$exp_date.'</p></td>
           </tr>
            <tr style="border: 1px solid #464747;height:25px;">
                <td style="background-color:#fad3c7;width:120px">Visa Type:</td>
                <td  style="width:220px"><p style="font-size:16px">'.$visa_subclass.'</p></td>
                <td style="background-color:#fad3c7;width:120px">Expiry Date:</td>
                <td  style="width:220px"><p style="font-size:16px">'.$visa_ex_date.'</p></td>
           </tr>
            <tr style="border: 1px solid #464747;height:25px;">
                <td style="background-color:#fad3c7;width:120px">OSHC Provider:</td>
                <td  style="width:220px"><p style="font-size:16px">'.$membership.'</p></td>
                <td style="background-color:#fad3c7;width:120px">Expiry Date:</td>
                <td  style="width:220px"><p style="font-size:16px">'.$cover_end_date.'</p></td>
           </tr>
         </table>
        '.$user_info_user_coe.$user_info_user_gap.'
         <div style="display: flex;font-size: 14px; -webkit-box-align: center;align-items: center;">
          <p class="slogan">技术支持&nbsp;:</p>
           <p>万友教育</p>
           <p>ONE U Education</p>
          <img src="https://crm.oneuedu.com/public/static/wedu/img/logo.jpg" style="width: 100px;height: 100px">
         </div>
';  
       pdf($html,$last_name.$first_name.' '.$dob.' One U Education信息表.pdf');
    }

 

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_23013025/article/details/86233897
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-01 22:34:09
  • 阅读 ( 1147 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢