Using Tcpdf to generate PDF document instances in PHP _php tutorial

Source: Internet
Author: User
In the actual work, we want to use PHP dynamic creation of PDF documents, there are many open source PHP to create PDF class Library, today I will introduce you to a good PDF library, it is tcpdf,tcpdf is a rapid generation of PDF file PHP5 function package. Tcpdf is extended and improved based on fpdf, which enhances practical functionality.


The tcpdf has the following characteristics:

1, support page footer;
2, support HTML tag code;
3, support jpg/png/gif/svg graphics image;
4, support forms;
5, support Chinese characters; (some PDF classes do not support English or Chinese is quite troublesome)
6, automatic paging, automatic page numbering, and so on.

How to use

You can get the latest version from Tcpdf official website: The official website provides dozens of examples as well as the instruction document, after downloading the decompression must pay attention to the file path, how to use the Tcpdf, can complete from the following 5 steps:

1, require_once import tcpdf.php files and related configuration information;
2, the instantiation of tcpdf;
3. Format PDF document, including document information, header, footer, font, outer space, picture border, paging, etc.
4, import the contents of the PDF document, can be single-line or multiple lines of simple strings, or HTML-formatted strings, etc.;
5. Output PDF document.

code example:
Copy CodeThe code is as follows:
Require_once (' tcpdf.php ');
Instantiation of
$pdf = new TCPDF (' P ', ' mm ', ' A4 ', true, ' UTF-8 ', false);

Set up Document information
$pdf->setcreator (' Helloweba ');
$pdf->setauthor (' Yueguangguang ');
$pdf->settitle (' Welcome to! ');
$pdf->setsubject (' TCPDF Tutorial ');
$pdf->setkeywords (' TCPDF, PDF, PHP ');

Set Header and footer information
$pdf->setheaderdata (' logo.png ', ' ', ' dedicated to the application of Web front-end technology in China ',
Array (0,64,255), Array (0,64,128));
$pdf->setfooterdata (Array (0,64,0), Array (0,64,128));

Set Header and footer fonts
$pdf->setheaderfont (Array (' stsongstdlight ', ' ', ' 10 '));
$pdf->setfooterfont (Array (' Helvetica ', ' ', ' 8 '));

Set the default equal-width font
$pdf->setdefaultmonospacedfont (' Courier ');

Set spacing
$pdf->setmargins (15, 27, 15);
$pdf->setheadermargin (5);
$pdf->setfootermargin (10);

Set Page Paging
$pdf->setautopagebreak (TRUE, 25);

Set Image scale Factor
$pdf->setimagescale (1.25);

Set Default font subsetting mode
$pdf->setfontsubsetting (TRUE);

Set font
$pdf->setfont (' stsongstdlight ', ', 14);

$pdf->addpage ();

$str 1 = ' Welcome to ';

$pdf->write (0, $str 1, ', 0, ' L ', True, 0, False, False, 0);

Output PDF
$pdf->output (' t.pdf ', ' I ');
Open with a browser after saving, or you will be prompted to download the generated PDF If your system has a PDF reader installed or opens directly in the browser using Google Chrome.

Other common classes of PHP generated PDFs


Html2pdf (
Html2pdf is able to convert an HTML text into a printer-friendly PDF file. This PHP script is built on top of the fpdf php script.

Tcpdf is a PHP5 function package that is used to quickly generate PDF files. Tcpdf is extended and improved based on fpdf. Support for Utf-8,unicode,html and XHTML.

Html2ps (
HTML2PS can convert HTML with images, complex tables (including Rowspan/colspan), Layer/div and CSS styles into PostScript and PDF. HTML2PS support for CSS2.1 is very good and is well compatible with incorrect HMTL. It is even able to convert almost CSS-based websites like

Html_topdf (
Html_topdf can convert any HTML document into a PDF document in the same format as the interface in any platform or printer. It includes support for image conversion, using style sheets to customize PDF files and error handling.

Cpdfwriter (
Cpdfwriter is a PHP5 class capable of outputting PDF documents. Based on tcpdf,fpdf and other related scripts.

Dompdf (
Dompdf is an HTML-to-PDF conversion tool. Its core is a rendering engine that follows most of the CSS2.1 style. The dompdf is style-driven and is capable of downloading and reading the style attributes of an external style, an entire style label, and a single HTML element. It also supports the majority of HTML attributes. true techarticle in actual work, we want to use PHP dynamic creation of PDF documents, there are many open source PHP to create PDF class Library, today I give you to introduce a good PDF library, it is tcpdf,t ...

  • 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.