There are several cases for a web application where a dynamic PDF file is a great solution for your digital documents. Think about order documents in your eCommerce application, coupons for your marketing campaign or just the PDF version from your web pages. There are plenty of situations were a dynamic PDF document is a great way to solve problems, too. Using PHP it’s possible generate those PDF files. There are several free PHP classes and there is the PDFlib library which is a commercial product. This list is shows the most popular PHP classes and libraries.
Generate PDF files using PHP
The following classes are written to generate PDF files “from the scratch” using PHP functions:
FPDF
The FPDF class is written written for PHP4 and was the first free PHP script for creating PDF files “on the fly”. I’ve used this class 8 years ago to generate several documents in several eCommerce applications. The project website offers some great documentation and also a lot of useful examples. The latest version is from June 2011 and I hope the next version is written for PHP5. The original version doesn’t support UTF-8, but since while there is class named tFPDF which supports UTF-8.
TCPDF
Like many other PDF scripts is TCPDF based on FPDF, too. The TCPDF class was in the beginning a rewrite for PHP5 with support for UTF-8. Now is this project a great PDF generator with great image functions including support for the CMYK color mode.
The missing support for UTF-8 in FPDF was the reason that I use TCPDF class for all my projects since a view years now. If you have used FPDF in the past, it’s very easy to make the move. The documentation for TCPDF project is not so good as for the FPDF class, but there are a lot of well documented examples.
Zend_Pdf
The Zend framework has also PDF generation class, which has less functions than FPDF or the other classes. Like many other classes inside the Zend Framework is the documentation very limited for this class as well. If you’re already using the Zend Framework for your project and you need some basic PDF function, you should go for this PDF solution.
PDFlib
The PDFlib is the commercial PDF solution for PHP. If installed (compiled), you can use the PDFlib functions right in your PHP code without the need to include a class file or library. This might work better and is often faster, but product has also a “great” price. I’ve worked with PDFlib in one of the projects in the past and PDFlib is absolutely not a bad one. If you look for the PDF documentation on the PHP website, the information is outdated and isn’t updated for the current version anymore. At the moment that your project is bigger and creating PDF files is an important part in your project you should think about this PDF library for PHP.
Import PDF files and add content using PHP
The classes above are tools for creating PDF files using PHP code, for example to generate PDF invoice documents. Using these tools you need to create any element in your PDF document. The following classes will help you to use existing PDF files for your dynamic PDF file.
FPDI
FPDI is a great class extension for the FPDF and the TCPDF class script. It’s easy: The PHP code opens a file, stores it in the web sevrer’s memory and you can add text and images. The following example will show how it works.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class myExtension fpdi public function addContent($pdffile) $this->setSourceFile($pdffile); $page = $this->ImportPage(1); $this->AddPage(); $this->useTemplate($page); $this->$this->Image(‘someimage.png’, 150, 5, 40, 20, ‘PNG’); $this->createTextField($string, 25, 50, 40); public function createTextField($string, $x, $y, $width, $height = 2) $this->SetXY($x,$y); $this->Setfont(“Arial”,“”,10); $this->Cell($width, $height, $string, 0, 0, “”); $pdf = new myExtension(); $pdf->addContent(‘origpdffile.php’); $pdf->Output(‘newpdffile.pdf’, ‘I’); |
Note, this is just an example on how to use the FPDI class. Since the object is written for PHP5 you need to use the TCPDF class or you need to change the code into an object for PHP4.
PDI (a PDFlib product)
There is also some PDF import function, called PDI, for the PDFlib library. According to their website it’s required to buy this extension or class together with PDFlib as an bundle. I never used this PDF import class, but I think it’s important to mention the script in this article.
HTML to PDF conversion with PHP
There are a lot of requests for HTML to PDF conversion. This feature is useful if you like to generate a PDF version from your website dynamically. The following two projects are the most important classes (if you talk about available information and frequent updates).
mPDF
mPDF is based on FPDF and HTML2FPDF, with a number of enhancements. The script is able to process utf-8 encoded HTML code and is able to use CSS stylesheets for the generated PDF layout.
dompdf
dompdf is an HTML to PDF converter which is able to read/parse CSS 2.1 compliant HTML layouts. The script is able to render styles from external stylesheets and the style attributes from individual HTML elements. The PDF rendering function is currently provided either by PDFLib or the R&OS CPDF class written by Wayne Munro.
These are a lot of tools for doing some PDF job, if I forgot an important class or library please mention them in a comment.
Generate PDF Files using PHP Scripts
No comments:
Post a Comment