TP framework introduces tcpdf plug-in steps and solutions to TCPD Chinese garbled characters

little bottle
Release: 2023-04-06 12:06:01
forward
4740 people have browsed it

This article mainly talks about the steps to introduce the tcpdf plug-in into the TP framework and the solution to TCPD Chinese garbled characters. It has certain learning value. Friends in need can take a look. I hope it can help you.

When doing projects, I used HTML to generate PDF and found that the TCPDF plug-in function was more suitable, so I chose this plug-in. The specific process is as follows:

1. Download the latest version of TCPDF through Composer and switch to the program Run the following command in the root directory (DOS command switching under Windows):

composer require tecnickcom/tcpdf

After the command is successfully executed, TCPDF will be downloaded to the vendor folder in the program root directory, such as Picture:

There are more than 60 typical examples in examples. For what functions you need, just look at the examples. For the corresponding list of examples, see the TCPDF official website: https://tcpdf.org/examples/

2. Call TCPDF in the controller.

use TCPDF;

3. Paste the code in the official example directly into the method and run successfully.

$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;);
Copy after login

Notes:

1. Because TCPDF uses delimiters to output html and other content, $html in the above code must be in the same format until EOD.

2. ThinkPHP must turn off the debugging mode, otherwise the output will be garbled.

Solution to TCPD Chinese garbled characters
The latest version of TCPDF already supports Chinese. Just specify the Chinese encoding in the method of generating PDF.

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

Solution to the problem that TCPDF cannot save Chinese file names

When PHP uses TCPDF to generate PDF files, if the file name is Chinese, it will be directly filtered out. The following is TCPDF Solution to the inability to save Chinese file names:

Open the tcpdf.php file and find the output function, which is about line 7554.

1. Comment the following code, approximately at lines 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);
}
Copy after login

2. Search for the method code and replace the following code, approximately at lines 7639, 7670, 7693, and 7718.

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

Replaced with

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

The above codes are in case 'I': (print PDF), case 'D': (download PDF), case 'FD': (save) of this method. to local file) statement.

In this way, when PHP uses TCPDF to generate PDF files, it can be saved as a Chinese name.

Related tutorials: thinkPHP video tutorial

The above is the detailed content of TP framework introduces tcpdf plug-in steps and solutions to TCPD Chinese garbled characters. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template