Home>Article>Backend Development> TP framework introduces tcpdf plug-in steps and solutions to TCPD Chinese garbled characters
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 = <<Welcome to TCPDF ! This is the first example of TCPDF library. This text is printed using the writeHTMLCell() method but you can also use: Multicell(), writeHTML(), Write(), Cell() and Text().
Please check the source code documentation and other examples for further information.
TO IMPROVE AND EXPAND TCPDF I NEED YOUR SUPPORT, PLEASE MAKE A DONATION!
EOD; $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true); $pdf->Output('example_001.pdf', 'I');
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('stsongstdlight', '', 12);
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] != 'F') { $name = preg_replace('/[\s]+/', '_', $name); $name = preg_replace('/[^a-zA-Z0-9_\.-]/', '', $name); }
2. Search for the method code and replace the following code, approximately at lines 7639, 7670, 7693, and 7718.
header('Content-Disposition: attachment; filename="'.basename($name).'"');
Replaced with
header('Content-Disposition: attachment; filename="'.$name.'"');
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!