thinkphp5生成PDF個人資訊表 pdf 格式下載
阿新 • • 發佈:2019-01-10
生成的樣式為
需要下載外掛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("<", '<', $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">技術支援 :</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'); }