tp框架引入tcpdf插件步骤以及TCPD中文乱码的解决方法-php教程

资源魔 34 0
本篇文章次要讲述的是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, '', '', $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行。

一、正文如下代码,约莫正在7565-7568行:

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

二、搜寻该办法代码,交换以下代码,约莫辨别正在763九、7670、769三、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文件时就能够保留为中文称号了。

相干教程:thinkPHP视频教程

以上就是tp框架引入tcpdf插件步骤和TCPD中文乱码的处理办法的具体内容,更多请存眷资源魔其它相干文章!

标签: php开发教程 php开发资料 php开发自学 thinkphp5 TCPDF PDF文件

抱歉,评论功能暂时关闭!