需求說明
在實際項目需求中,需要將資料以文檔的形式匯出
版本說明
ThinkPPHP 3.2.3
tcpdf6.2.12()
整合步驟
將tcpdf檔案夾放到thinkPHP的Library的Vender檔案夾下 在代碼中引入tcpdf 編寫代碼匯出pdf檔案,(tcpdf examples )
匯出pdf案例
public function export_pdf(){ob_start();vendor('tcpdf.tcpdf_import','simplewind/Core/Library/Vendor','.php');$pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); //設定字型$pdf->SetFont('stsongstdlight', '', 12);// ---------------------------------------------------------$title = $_REQUEST['title'];// set document information$pdf->SetCreator(PDF_CREATOR);$pdf->SetAuthor('Nicola Asuni');$pdf->SetTitle("$title");$pdf->SetSubject('TCPDF Tutorial');$pdf->SetKeywords('TCPDF, PDF, example, test, guide');// set default header data設定頁首$pdf->SetHeaderData('', '', $truename.'的文檔', $company_name);// set header and footer fonts設定頁首字型$pdf->setHeaderFont(Array('stsongstdlight', '', PDF_FONT_SIZE_MAIN));$pdf->setFooterFont(Array('stsongstdlight', '', 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);// set some language-dependent strings (optional)if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {require_once(dirname(__FILE__).'/lang/eng.php');$pdf->setLanguageArray($l);}// set default font subsetting mode$pdf->setFontSubsetting(true);// add a page$pdf->AddPage();//Cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=0, $link='', $stretch=0, $ignore_min_height=false, $calign='T', $valign='M')$pdf->setCellPaddings(1, 1, 1, 1);// set cell margins$pdf->setCellMargins(1, 1, 1, 1);// set color for background$pdf->SetFillColor(255, 255, 127);$txt = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.';// Multicell test儲存格$pdf->MultiCell(90, 5, "姓 名:$truename", 0, 'L', 0, 0, '', '', true);
//換行$pdf->Ln(8);$pdf->SetFont('stsongstdlight', 'B', 16);
//直接輸出文字$pdf->Write(5,"結果:", '', 0, '', false, 0, false, false, 0);//檔案名稱$filename=$truename."--".$telephone."pdf";
//輸出pdf$pdf->Output("$filename", 'I');ob_end_flush(); }
以上代碼用來顯示成一個線上預覽的pdf檔案,可以進行列印和下載操作,非常方便