


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 = <<<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');
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Use Python and WebDriver to screenshot web pages and save them as PDF files Summary: During web development and testing, it is often necessary to screenshot web pages for analysis, recording, and reporting. This article will introduce how to use Python and WebDriver to take screenshots of web pages and save the screenshots as PDF files for easy sharing and archiving. 1. Install and configure SeleniumWebDriver: Install Python: Visit the Python official website (https:

With the advent of the Internet and the digital age, images are used more and more widely, especially in some situations where it is necessary to add watermarks or background images to images to ensure information security and copyright protection. At this time, we can use PHP language and TCPDF library to implement watermarking and background image processing of images. The following is the specific implementation method. 1. Install the TCPDF library TCPDF is an open source PHP class library used to create PDF documents, but it also provides some tools to create images. The installation of the TCPDF class library is relatively simple, we

CakePHP is a very popular PHP framework that provides many convenient methods for web application development. TCPDF is a very commonly used PDF generation library when we need to generate PDF files in applications. This article will introduce how to use TCPDF in CakePHP. Install TCPDF First, we need to install TCPDF in our CakePHP project. This can be done in a few ways, such as manually copying the TCPDF to the project's v

Conversion method: 1. Online conversion tools, such as pdfcrowd and online2pdf, can quickly convert HTML files to PDF without installation. 2. Browser plug-ins, such as Chrome's HTML to PDF plug-in, allow conversion to be performed directly in the browser. 3. Professional software, such as Adobe Acrobat and Foxit PhantomPDF, provide more comprehensive functions and support fast batch conversion and advanced output options.

Solution to the error reported when deploying thinkphp5 in Pagoda: 1. Open the Pagoda server, install the php pathinfo extension and enable it; 2. Configure the ".access" file with the content "RewriteRule ^(.*)$ index.php?s=/$1 [QSA ,PT,L]”; 3. In website management, just enable thinkphp’s pseudo-static.

Barcodes and QR codes are important tools widely used in modern life. They are widely used in various fields such as logistics, retail, medical, payment, etc. This article will introduce how to create barcodes and QR codes using PHP and the TCPDF library. 1. What is TCPDF? TCPDF is a PHP library for generating PDF documents. It's free, open source, and already widely used in many projects. In addition to generating PDF documents, TCPDF also provides the function of creating barcodes and QR codes. 2. How to install T

Solution to thinkphp5 url rewriting not working: 1. Check whether the mod_rewrite.so module is loaded in the httpd.conf configuration file; 2. Change None in AllowOverride None to All; 3. Modify the Apache configuration file .htaccess to "RewriteRule ^ (.*)$ index.php [L,E=PATH_INFO:$1]" and save it.

How to remove the thinkphp5 title bar icon: 1. Find the favicon.ico file under the thinkphp5 framework public; 2. Delete the file or choose another picture to rename it to favicon.ico and replace the original favicon.ico file.
