Which plug-in do you use when exporting PHP to PDF

Posted by Adam on Fri, 15 Nov 2019 15:28:30 +0100



Preparation

First of all, the related class libraries are queried, such as FPDF, zendPDF,TcPDF, etc. First of all, I chose FPDF first. It can be said that there is no problem except Chinese characters. Chinese characters are disorderly and there is no good solution after reading the latest version, so I have to give up. Later, I found TcPDF, which was specially for Chinese support. At first, it was not very good for Chinese font support, but I found that someone made a Chinese language pack to make TcPDF more perfect.

 

brief introduction

TCPDF is a popular PHP class for generating PDF documents. TCPDF is currently the only PHP library that fully supports UTF-8 Unicode and right to left writing languages including bi-directional scripts.

 

TCPDF documentation

I. first call TCPDF file

require_once('tcpdf.php');

2. Instantiate TCPDF page direction (P = portrait, L = landscape), measurement (mm), page format

$pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);

 

Here is an example of using TCPDF. It's very simple. Look at the code

 1   vendor('Pdf');
 2         $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
 3 
 4         $pdf->SetCreator(PDF_CREATOR);
 5         $pdf->SetAuthor('sunnier');
 6         $pdf->SetTitle('123');
 7         $pdf->SetSubject('123');
 8         $pdf->SetKeywords('sunnier');
 9 
10 // set default header data
11         $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
12 
13 // set header and footer fonts
14         $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
15         $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
16 
17 // set default monospaced font
18         $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
19 
20 // set margins
21         $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
22         $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
23         $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
24 
25 // set auto page breaks
26         $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
27 
28 // set image scale factor
29         $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
30 
31 // set some language-dependent strings (optional)
32         global $l;
33         $pdf->setLanguageArray($l);
34 
35 // ---------------------------------------------------------
36 
37 // set font
38         $pdf->SetFont('simfang', '', 10);
39 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
40 // Print a table
41 
42 // add a page
43         $pdf->AddPage();
44 
45 // Write casually HTML
46         $html = 'adsf';
47 
48 // output the HTML content
49         $pdf->writeHTML($html, true, false, true, false, '');
50 
51 // reset pointer to the last page
52         $pdf->lastPage();
53         $pdf->Output('Ha ha ha'.pdf', 'I');
54  

 

Above, you can continuously add addPage, add multiple pages, and finally Output the file with your own name.
Demonstration map:

 

It can be seen that there is no problem that pictures can be inserted in it. It is OK to write according to HTML. Of course, html is not only supported, but it is more suitable here.

Topics: PHP