Generating an PDF document instance using TCPDF in PHP

  • 2021-07-06 10:25:00
  • OfStack

In practice, we want to use PHP to dynamically create PDF documents. At present, there are many open source PHP to create PDF class libraries. Today, I will introduce an excellent PDF library, which is TCPDF, and TCPDF is an PHP5 function package for quickly generating PDF files. TCPDF is expanded and improved based on FPDF, and its practical functions are enhanced.

Characteristic

TCPDF has the following features:

1. Support page footer;
2. Support HTML tag code;
3. Support jpg/png/gif/svg graphics and images;
4. Support forms;
5. Support Chinese characters; (Some PDF classes do not support Chinese or it is quite troublesome to process Chinese)
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. Official website provides several 10 examples and explanatory documents. After downloading and decompressing, you must pay attention to the file path. How to use TCPDF can be completed from the following 5 steps:

1. require_once import tcpdf. php file and related configuration information;
2. Instantiate TCPDF;
3. Set the format of PDF document, including document information, header, footer, font, outer spacing, picture border, paging, etc.;
4. Import the contents of PDF documents, which can be single-line or multi-line simple strings, or strings in HTML format;
5. Output the PDF document.

Code example:

 
require_once('tcpdf.php');
// Instantiation
$pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);
 
// Setting document information
$pdf->SetCreator('Helloweba');
$pdf->SetAuthor('yueguangguang');
$pdf->SetTitle('Welcome to helloweba.com!');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, PHP');
 
// Setting header and footer information
$pdf->SetHeaderData('logo.png', 30, 'Helloweba.com', ' Committed to WEB Application of 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 up paging
$pdf->SetAutoPageBreak(TRUE, 25);
 
// set image scale factor
$pdf->setImageScale(1.25);
 
// set default font subsetting mode
$pdf->setFontSubsetting(true);
 
// Set fonts
$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 in a browser. If your system has PDF reader installed or uses google chrome to open it directly in a 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 converts one 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 an PHP5 function package for fast generation of PDF files. TCPDF is expanded and improved based on FPDF. Support for 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 CSS 2.1 very well and is well compatible with incorrect HMTL. It can even convert websites designed almost using CSS, such as msn. com.

HTML_ToPDF(http://www.rustyparts.com/pdf.php)
HTML_ToPDF can convert any HTML document into an PDF document with the same interface format under 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 an 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 an Rendering engine that follows most of the CSS 2.1 style. dompdf is style-driven and can download and read external styles, entire style labels, and style attributes for individual HTML elements. It also supports most HTML attributes.


Related articles: