English 中文(简体)
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?

最佳回答

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.

问题回答

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.

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.

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

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.

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.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签