views:

699

answers:

6

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?

+2  A: 

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.

Pekka
+2  A: 

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.

David Caunt
A: 

I also recommend converting HTML to PDF:

  • It is easier to realise
  • It is easier to change the layout/design later
  • You could probably reuse some html of the page

When I once created a PDF generation for a website I used html2ps and then converted the postscript to pdf later.

Hippo
+1  A: 

Hi you have two options. You can use dompdf, which enables you to design your document as a regular html document and style it with css. You can turn this document into pdf. My experiences were good. I render a regular html page using Zend_View and turn this into pdf. This way you have an extremely flexible template, beacuse you can make all kind of display decisions in your html-generating view.

→ see the list of css-compatability.

Alternatively you could use livedocx. This also allows for a template based approach, but more rigidly and html-less. A integration for Zend Framework is on its way.

Exception e
A: 

I suggest the programmatic formatting.

In the .net world I've had distinctly negative experiences with html to pdf converters. They seem to fall into 2 categories:

The ones that 'parse' your html and attempt to turn it into a pdf. These often lack some really fundamental features like style sheets and support for anything but table based layout.

The ones that 'print' your html to an image and stick that into a pdf. These can actually produce a decent looking document. They seem to work by linking a browser into the library which causes threading issues and poor performance.

I've had mediocre experiences with direct pdf apis but php appears to have some significantly better libraries on this front.

JeffreyABecker
This is completely different for the php world indeed. The programmatic libraries like Zend_Pdf are the ones that suck. Like I recommended, dompdf is very good in html to pdf. It doesnt link a browser in the library or such stuff (nobody would even dare to think about this)
Exception e
A: 

I have had ample experience with all three approaches:

  1. Creating the document programmatically using TCPDF (which, although it was derived from FPDF, DOES support UTF-8 out of the box). Although I'm a huge fan of Zend Framework, it does need to be noted that Zend_Pdf is NOT rich enough yet in its feature set to be used in this way.

  2. Having a graphic designer create a template, loading it up using Zend_Pdf and then adding additional content to it. Zend_Pdf (1.10.2 - 1.10.7) worked fairly well in this case, EXCEPT when trying to instantiate a new page and add it to the document - this threw loads of errors.

  3. Converting from HTML to PDF using htmldoc. This approach sucked badly, but mainly due to limitations in htmldoc.

I haven't used TCPDF for HTML to PDF conversion, but judging by the overall quality of the code I'm guessing it would do quite a good job. (I've really pushed TCPDF to the limits, including rendering graphically rich documents in RTL character sets, Chinese, etc, and it has performed flawlessly). So if you do decide to do it programmatically, I would recommend using TCPDF but if you have a nice template from a graphic designer and intend to just add stuff on top, loading it up with Zend_Pdf and modifying from there is a reasonable choice (although you'll still end up implementing word wrapping and some other basic stuff).

JamesG