Use TCPDF to generate PDF document instances in PHP

Source: Internet
Author: User
Tags php pdf

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, and TCPDF is a PHP5 function package used to quickly generate PDF files. TCPDF is extended and improved based on FPDF, enhancing practical functions.


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: 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:
Copy codeThe 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! ');
$ 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 ';
$ 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 ( /)

HTML2PDF converts an HTML text file into a printer-friendly PDF file. This PHP script is built on the fpdf php script.

TCPDF ( /)
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 ( /~ Jan/html2ps.html)
Html2ps converts HTML with images, complex tables (including rowspan/colspan), layer/div, 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

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 (
Csf-writer is a PHP5 class that can output PDF documents. Based on TCPDF, FPDF, and other related scripts.

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.