_php instance of PDF document instance using Tcpdf in PHP

Source: Internet
Author: User
Tags error handling php script

In actual work, we want to use PHP to create a dynamic PDF document, there are many open source PHP to create a PDF class library, today I would like to introduce you to a good PDF library, it is tcpdf,tcpdf is a fast-generated PDF file PHP5 function package. Tcpdf is extended and improved based on fpdf, which enhances practical functionality.

Characteristics

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 Mandarin or have trouble handling Chinese)
6, automatic pagination, automatic page number, and so on.

How to use

You can get the latest version from tcpdf website: http://www.tcpdf.org. Website provides dozens of examples and documentation, download after decompression must pay attention to file path, how to use Tcpdf, can be completed from the following 5 steps:

1, require_once import tcpdf.php files and related configuration information;
2, the instantiation of tcpdf;
3, set the format of the PDF document, including document information, headers, footers, fonts, outside spacing, picture borders, pagination, etc.
4, import the content of the PDF document, can be a single line or multi-line simple string, can also be HTML format strings, etc.;
5, output PDF document.

code example:

Copy Code code 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 helloweba.com! ');
$pdf->setsubject (' TCPDF Tutorial ');
$pdf->setkeywords (' TCPDF, PDF, PHP ');

Set Header and footer information
$pdf->setheaderdata (' logo.png ', ' helloweba.com '), ' 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 default equal-width fonts
$pdf->setdefaultmonospacedfont (' Courier ');

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

Set up 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 helloweba.com ';

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

Output PDF
$pdf->output (' t.pdf ', ' I ');

When you save it, open it in your browser, or you will be prompted to download the generated PDF If your system has a PDF reader installed or if you use Google Chrome to open it directly in the browser.

Common classes for other PHP-generated PDFs

FPDF (http://www.fpdf.org/)

Html2pdf (http://html2pdf.seven49.net/)
Html2pdf can convert an HTML text into a printer-friendly PDF file. This PHP script is built on top of the fpdf php script.

TCPDF (http://tcpdf.sourceforge.net/)
Tcpdf is a PHP5 function package that is 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 can convert HTML with pictures, 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 web sites like msn.com, which are designed almost with CSS.

Html_topdf (http://www.rustyparts.com/pdf.php)
Html_topdf can convert any HTML document into a PDF document that has the same interface format under any platform and printer. It includes support for picture transformations, using style sheets to customize PDF files and error handling.

Cpdfwriter (http://freshmeat.net/projects/cpdfwriter/)
Cpdfwriter 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 an HTML to PDF conversion tool. Its core is a rendering engine that follows most CSS2.1 styles. Dompdf is style driven, which enables you to download and read external styles, the entire style label, and the style attributes of a single HTML element. It also supports the goal as 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: info-contact@alibabacloud.com 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.