首頁  >  文章  >  後端開發  >  tp框架引入tcpdf插件步驟以及TCPD中文亂碼的解決方法

tp框架引入tcpdf插件步驟以及TCPD中文亂碼的解決方法

little bottle
little bottle轉載
2019-04-26 17:31:244625瀏覽

這篇文章主要講述的是tp框架引入tcpdf插件步驟以及TCPD中文亂碼的解決方法,具有一定的學習價值,有需要的朋友可以看看,希望能夠幫助到你。

做專案時用到HTML產生PDF,發現TCPDF外掛功能比較適合,因此選擇了這個外掛具體流程如下:

1.透過Composer下載最新版TCPDF,切換到程序根目錄執行如下指令(Windows下DOS指令切換):

composer require tecnickcom/tcpdf

指令成功執行後,TCPDF會被下載到程式根目錄中的vendor資料夾,如圖:

examples中有60多個典型範例,需要什麼功能直接看範例即可,範例對應清單請見TCPDF官網:https://tcpdf.org/examples/

2.在控制器中呼叫TCPDF。

use TCPDF;

3.在方法中直接貼上官方範例中的程式碼,成功運行。

$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->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);
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$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->SetFont('dejavusans', '', 14, '', true);
$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'));
$html = <<<EOD
<h1>Welcome to <a href="http://www.tcpdf.org" style="text-decoration:none;background-color:#CC0000;color:black;"> <span style="color:black;">TC</span><span style="color:white;">PDF</span> </a>!</h1>
<i>This is the first example of TCPDF library.</i>
<p>This text is printed using the <i>writeHTMLCell()</i> method but you can also use: <i>Multicell(), writeHTML(), Write(), Cell() and Text()</i>.</p>
<p>Please check the source code documentation and other examples for further information.</p>
<p style="color:#CC0000;">TO IMPROVE AND EXPAND TCPDF I NEED YOUR SUPPORT, PLEASE <a href="http://sourceforge.net/donate/index.php?group_id=128076">MAKE A DONATION!</a></p>
EOD;
$pdf->writeHTMLCell(0, 0, &#39;&#39;, &#39;&#39;, $html, 0, 1, 0, true, &#39;&#39;, true);
$pdf->Output(&#39;example_001.pdf&#39;, &#39;I&#39;);

 注意事項:

1.因為TCPDF使用定界符的方式輸出html等內容,因此上述程式碼中的$html一直到EOD必須頂格。

2.ThinkPHP要關閉偵錯模式,不然輸出亂碼。

TCPD中文亂碼的解決方法
最新版本的TCPDF已經支援中文,在產生PDF的方法中顯示指定使用中文編碼即可。

$pdf->SetFont(&#39;stsongstdlight&#39;, &#39;&#39;, 12);

 TCPDF不能儲存中文檔案名稱的解決方法

PHP使用TCPDF產生PDF檔案時,如果檔案名稱是中文會被直接過濾掉,以下是TCPDF無法儲存中文檔案名稱的解決方法:

開啟tcpdf.php文件,找到output函數,大約在7554行。

1、註解以下程式碼,大約在7565-7568行:

if ($dest[0] != &#39;F&#39;) {
    $name = preg_replace(&#39;/[\s]+/&#39;, &#39;_&#39;, $name);
    $name = preg_replace(&#39;/[^a-zA-Z0-9_\.-]/&#39;, &#39;&#39;, $name);
}

 2、搜尋該方法程式碼,取代如下程式碼,大約分別在7639、7670、7693、7718行。

header(&#39;Content-Disposition: attachment; filename="&#39;.basename($name).&#39;"&#39;);

 替換為

header(&#39;Content-Disposition: attachment; filename="&#39;.$name.&#39;"&#39;);

 上述程式碼分別在該方法的case 'I':(列印PDF)、case 'D':(下載PDF)、case 'FD':(儲存到本地文件)語句中。

這樣PHP使用TCPDF產生PDF檔時就可以儲存為中文名稱了。

 相關教學:thinkPHP影片教學

#

以上是tp框架引入tcpdf插件步驟以及TCPD中文亂碼的解決方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:cnblogs.com。如有侵權,請聯絡admin@php.cn刪除