Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML to PDF vs. Programmatically creating PDF via PHP

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?

like image 516
John Avatar asked Nov 28 '09 13:11

John


People also ask

Can PHP generate PDF?

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.

Which is better Tcpdf or FPDF?

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 .

How create PDF in PHP explain with example?

$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.


2 Answers

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.

like image 66
Pekka Avatar answered Oct 13 '22 01:10

Pekka


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.

like image 24
David Snabel-Caunt Avatar answered Oct 13 '22 02:10

David Snabel-Caunt