I have a PHP application that needs to generate some PDF invoices and PDF timesheets with nice headers/footers. Some Stackoverflow users recommend using TCPDF to create the PDF documents. In my research, I discovered two approaches to generating PDFs:
1) Programmatically formatting the PDF like so:
$tcpdf->SetFillColor(255, 0, 0);
$tcpdf->SetTextColor(255);
$tcpdf->SetDrawColor(128, 0, 0);
$tcpdf->SetLineWidth(0.3);
$tcpdf->SetFont('', 'B');
2) Converting HTML to PDF
How do I decide which I approach I should use?
FPDF is a PHP class which allows you to generate PDF files and does not depend on additional PHP libraries. FPDF is free and can be downloaded from the official website's download section. The download package contains all necessary files, along with some tutorials on how to use it.
TCPDF is based on FPDF and adds a few more methods, as you observed. I just checked my code and the methods I'm using in TCPDF that aren't available in FDPF are SetCellPadding , SetAlpha , WriteHTML and WriteHTMLCell .
$pdf=new FPDF(); Example 1: The following example generates a PDF file with the given text in the code. The file can be downloaded or previewed as needed.
I always recommend creating PDFs dynamically but based on templates that can be edited using end-user tools or - at least - without having to be a programmer. In your case, that would probably be the HTML. To me, the ideal solution is to take a Office template, fill it with values programmatically, and output it to PDF, whenever technically possible. I do the office template filling part in PHP using tbsOOO and it works quite well.
Main reason for using a template-based approach over generating the document from scratch: the user can design the template as they see fit, and you will not be needed when the design needs changing. (And it will need changing one day.) Only when the inserted data needs to be changed or extended in some way, the programmer comes into play. That is a huge workload off your back.
I can recommend Zend_Pdf, which is a programmatic method according to your classification.
A HTML abstraction is nice but I don't believe you have the full power and control of a full geometry based drawing API. Be wary of libraries like fpdf which don't support UTF-8 out of the box, and use a poor man's parser to 'convert' HTML to PDF.
With regard to your application, I have produced a system which generates templated invoices. My users can upload a PDF template, which is extracted by Zend_Pdf. This template is then used for every page of the invoice generated, allowing easy customisation.
Note that my audience are mainly graphic designers who are capable of producing these templates, so it was the appropriate solution here. If your audience are only familiar with HTML, 'converting' HTML to PDF might be the best way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With