Home  >  Article  >  Backend Development  >  Example of using TCPDF to generate PDF documents in PHP_PHP tutorial

Example of using TCPDF to generate PDF documents in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:26:282570browse

In actual work, we need to use PHP to dynamically create PDF documents. There are currently many open source PHP class libraries for creating PDF. Today I will introduce to you an excellent PDF library, which is TCPDF. TCPDF is a PHP5 function package for quickly generating PDF files. TCPDF is expanded and improved based on FPDF, and has enhanced practical functions.

Features

TCPDF has the following features:

1. Support page footer;
2. Support HTML tag code;
3. Support jpg/png/gif/svg graphic images;
4. Support forms;
5. Support Chinese characters; (some PDFs do not support Chinese or it is very troublesome to process Chinese)
6. Automatic paging, automatic page numbering, etc.

How to use

You can get the latest version from the TCPDF official website: http://www.tcpdf.org. The official website provides dozens of examples and documentation. Be sure to pay attention to the file path after downloading and unzipping. How to use TCPDF can be completed in the following 5 steps:

1. require_once imports the tcpdf.php file and related configuration information;
2. Instantiate TCPDF;
3. Set the format of the PDF document, including document information, header, footer, font, outer spacing, picture border, paging, etc.;
4. The content of the imported PDF document can be a single line or multiple lines of simple strings, or strings in HTML format, etc.;
5. Output PDF documents.

Code example:

Copy code The code is as follows:

require_once('tcpdf.php');
//Instantiation
$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', 'Committed 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 font
$pdf->setHeaderFont(Array('stsongstdlight', '', '10'));
$pdf->setFooterFont(Array('helvetica', '', '8'));

//Set the default monospaced font
$pdf->SetDefaultMonospacedFont('courier');

// Set 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 Helloweba.com';

$pdf->Write(0,$str1,'', 0, 'L', true, 0, false, false, 0);

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

After saving, open it with a browser. If your system has a PDF reader installed or use Google Chrome to open it directly in the browser, otherwise you will be prompted to download the generated PDF.

Other common classes for php to generate PDF

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 for quickly generating 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 images, complex tables (including rowspan/colspan), layer/div and css styles into Postscript and PDF. html2ps has very good support for CSS2.1 and is well compatible with incorrect HMTL. It can even convert almost-CSS-designed websites like msn.com.

HTML_ToPDF(http://www.rustyparts.com/pdf.php)
HTML_ToPDF can convert any HTML document into a PDF document with the same interface format on any platform and printer. It includes support for image conversion, use of 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. At its core is a rendering engine that follows most CSS2.1 styles. dompdf is style driven, it is able to download and read external styles, entire style tags and style attributes of individual HTML elements. It also supports most HTML attributes currently.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/824700.htmlTechArticleIn actual work, we need to use PHP to dynamically create PDF documents. There are currently many open source PHP classes for creating PDF Library, today I will introduce to you an excellent PDF library, it is TCPDF, T...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn