ThinkPHP5使用TCPDF生成PDF文件


在做項目時用到HTML生成PDF,發現TCPDF插件功能比較適合,因此選擇了這款插件,在使用時直接下載了官網的,發現生成出來的文字顯示不出來,不管英文還是中文,我猜測可能是下載的文件有問題的原因,經過一番琢磨成功在ThinkPHP5上使用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, '', '', $html, 0, 1, 0, true, '', true);
$pdf->Output('example_001.pdf', 'I');

 

注意事項:

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

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

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

$pdf->SetFont('stsongstdlight', '', 12);

 
TCPDF不能保存中文文件名的解決方法

PHP使用TCPDF生成PDF文件時,如果文件名是中文會被直接過濾掉,以下是TCPDF不能保存中文文件名的解決方法:

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

1、注釋以下代碼,大約在7565-7568行:

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

 2、搜索該方法代碼,替換如下代碼,大約分別在7639、7670、7693、7718行。

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

 替換為

header('Content-Disposition: attachment; filename="'.$name.'"');

 

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

這樣PHP使用TCPDF生成PDF文件時就可以保存為中文名稱了。

 


免責聲明!

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



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