PHP 使用tcPdf 生成pdf文件


1. composer 安裝
安裝命令:

composer require tecnickcom/tcpdf

2. 使用
<?php namespace pdf; use app\common\logic\Oss; use Dompdf\Dompdf; class Pdf {     /**      * @param $html 生成pdf 的html 代碼      * @param $file 保存的文件名稱      * @return string      */         public function createPdf($html,$file)     {                 $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');         // 設置頭部,比如header_logo,header_title,header_string及其屬性         //$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 001', PDF_HEADER_STRING, array(0,64,255), array(0,64,128));         // 設置尾部        $pdf->setFooterData(array(0,64,0), array(0,64,128));                 // 設置頁頭字體        $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));         // 設置頁尾字體        $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));         // 設置默認等寬字體        $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);         // 設置margins 參考css的margins        $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);         // 設置頁頭margins        $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);         // 設置頁腳margins        $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);         // 設置自動分頁        $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);         // 設置調整圖像自適應比例        $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);         // ---------------------------------------------------------         // 設置默認字體子集模式         $pdf->setFontSubsetting(true);         // Set font         // dejavusans is a UTF-8 Unicode font, if you only need to         // print standard ASCII chars, you can use core fonts like         // helvetica or times to reduce file size.         // 設置字體         $pdf->SetFont('cid0cs', '', 8);         // Add a page         // This method has several options, check the source code documentation for more information.         // 增加一個頁面         $pdf->AddPage();         // 設置文字陰影效果         $pdf->setTextShadow(array('enabled'=>true, 'depth_w'=>0.2, 'depth_h'=>0.2, 'color'=>array(196,196,196), 'opacity'=>1, 'blend_mode'=>'Normal'));         // 設置 邊距        $pdf->SetMargins(15,27,15);//        // 使用writeHTML打印文本         $pdf->writeHTML($html, true, false, true, false, '');        // reset pointer to the last page         $pdf->lastPage();        // ---------------------------------------------------------         // This method has several options, check the source code documentation for more information.         $file_path = './uploads/pdf/'.$file.'.pdf';         $pdf->Output($file_path, 'F');         if (OSS_UPLOAD) {             //oss上傳             $object = 'uid' . WID . '/applet/' . date("Ymd") . '/' . $file.'.pdf';             $Oss = new Oss();             $oss = $Oss->ossUploadFile($object, $file_path, true);             $url = $oss['info']['url'];             return $url;         }         return  $file_path;     } }復制代碼

注:以上代碼如果出現以下報錯

找到下面的文件將標紅處屏蔽

文章轉載自https://www.juchengvi.com/looknews/66


免責聲明!

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



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