Using PHP to generate PDF documents

Source: Internet
Author: User


Original: Using PHP to generate PDF documents





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.


Using PHP to generate PDF documents


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.


Characteristics


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: http://www.tcpdf.org. 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:


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 header and footer information
$ pdf-> SetHeaderData (‘logo.png’, 30, ‘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 the default monospace font
$ pdf-> SetDefaultMonospacedFont (‘courier’);
 
// set 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.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


FPDF



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



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 msn.com.



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.



Using PHP to generate PDF documents


Related Article

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.