Use TCPDF in PHP to generate PDF document instances. In actual work, we will use PHP to dynamically create PDF documents. Currently, there are many open-source PHP libraries to create PDF files. today I will introduce you to an excellent PDF library, it is TCPDF. T in actual work, we need to use PHP to dynamically create PDF documents. At present, there are many open source PHP to create PDF class libraries, today, I will introduce you to an excellent PDF library, which is TCPDF. TCPDF is a PHP5 function package used to quickly generate PDF files. TCPDF is extended and improved based on FPDF, enhancing practical functions.
Features
TCPDF has the following features:
1. page footer is supported;
2. HTML tag code is supported;
3. jpg, png, gif, and svg images are supported;
4. supported tables;
5. Chinese characters are supported. (some PDF files do not support Chinese characters or are difficult to process Chinese characters)
6. automatic paging, automatic page number, and so on.
How to use
You can get the latest version: http://www.tcpdf.org from TCPDF official website. The official website provides dozens of examples and instructions. after downloading and unzipping files, you must pay attention to the file path. to use TCPDF, perform the following five steps:
1. require_once import the tcpdf. php file and related configuration information;
2. instantiate TCPDF;
3. set the format of a PDF document, including the document information, header, footer, font, outer spacing, image border, and paging;
4. the content of the imported PDF document can be a single line or multiple lines of simple strings, or a string in HTML format;
5. output PDF documents.
Sample code:
The code is as follows:
Require_once ('tcpdf. php ');
// Instantiate
$ Pdf = new TCPDF ('P', 'mm', 'A4 ', true, 'utf-8', false );
// Set document information
$ Pdf-> SetCreator ('helloweba ');
$ Pdf-> SetAuthor ('yueguangguang ');
$ Pdf-> SetTitle ('welcome to helloweba.com! ');
$ Pdf-> SetSubject ('tcpdf Tutorial ');
$ Pdf-> SetKeywords ('tcpdf, PDF, php ');
// Set the header and footer information
$ Pdf-> SetHeaderData('logo.png ', 30, 'helloweba. com', 'application of WEB front-end technology in China ',
Array (255, 128), array ));
$ Pdf-> setFooterData (array (128, 0), array ));
// Set the header and footer fonts
$ Pdf-> setHeaderFont (Array ('stsonstcdlight ', '', '10 '));
$ Pdf-> setFooterFont (Array ('Helvetica ', '', '8 '));
// Set the default same width font
$ Pdf-> setdefamonomonospacedfont ('courier ');
// Set the spacing
$ Pdf-> SetMargins (15, 27, 15 );
$ Pdf-> SetHeaderMargin (5 );
$ Pdf-> SetFooterMargin (10 );
// Set pagination
$ Pdf-> SetAutoPageBreak (TRUE, 25 );
// Set image scale factor
$ Pdf-> setImageScale (1.25 );
// Set default font subsetting mode
$ Pdf-> setFontSubsetting (true );
// Set the font
$ Pdf-> SetFont ('stsongstdlight ', '', 14 );
$ Pdf-> AddPage ();
$ Str1 = 'Welcome to Helloweba.com ';
$ Pdf-> Write (0, $ str1, '', 0, 'L', true, 0, false, false, 0 );
// Output PDF
$ Pdf-> output('t}', 'I ');
Save the file and open it in the browser. if your system has a PDF reader installed or google chrome is used to open the file directly in the browser, the generated PDF file will be downloaded.
Other common php PDF generation classes
FPDF (http://www.fpdf.org /)
HTML2PDF (http://html2pdf.seven49.net /)
HTML2PDF converts an HTML text file into a printer-friendly PDF file. This PHP script is built on the fpdf php script.
TCPDF (http://tcpdf.sourceforge.net /)
TCPDF is a PHP5 function package used to quickly generate PDF files. TCPDF is extended and improved based on FPDF. Supports UTF-8, Unicode, HTML, and XHTML.
Html2ps (http://user.it.uu.se /~ Jan/html2ps.html)
Html2ps converts HTML with images, complex tables (including rowspan/colspan), layer/p, and css styles into Postscript and PDF. Html2ps supports CSS2.1 very well and is well compatible with incorrect HMTL. It can even convert websites that use CSS, such as msn.com.
HTML_ToPDF (http://www.rustyparts.com/pdf.php)
HTML_ToPDF converts any HTML document to a PDF document in the same format as any platform or printer. It supports image conversion and uses style sheets to customize PDF files and handle errors.
Cw.writer (http://freshmeat.net/projects/cpdfwriter)
Csf-writer is a PHP5 class that can output PDF documents. Based on TCPDF, FPDF, and other related scripts.
Dompdf (http://www.digitaljunkies.ca/dompdf)
Dompdf is a conversion tool from HTML to PDF. Its core is a Rendering engine that follows most CSS2.1 styles. Dompdf is style-driven. it can download and read external styles, the entire style tag, and the style attributes of a single HTML element. It also supports most HTML attributes.
...