Using PHP to generate PDF documents

Source: Internet
Author: User
Tags error handling


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.

View Demo Features

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:

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 header and footer information
$ pdf-> SetHeaderData (‘logo.png’, 30, ‘’, ‘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 the header and footer font
$ pdf-> setHeaderFont (Array (‘stsongstdlight‘, ‘‘, ‘10’));
$ pdf-> setFooterFont (Array (‘helvetica‘, ‘‘, ‘8‘));
// Set the default monospace font
$ pdf-> SetDefaultMonospacedFont (‘courier’);
// Set the spacing
$ pdf-> SetMargins (15, 27, 15);
$ pdf-> SetHeaderMargin (5);
$ pdf-> SetFooterMargin (10);
// set 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 ();
$ str1 = ‘Welcome to’;
$ 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.

★ If you need to save directly to a directory on disk:

$res $pdf->output (' aaa.pdf ', ' S '); file_put_contents $res);
Other common classes of PHP generated PDFs



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 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 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 is a PHP5 class capable of outputting PDF documents. Based on tcpdf,fpdf and other related scripts.


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

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.