views:

4038

answers:

8

Is it possible to convert a HTML page to PDF using PHP, and if so, how can it be done?

Specifically, the page is an invoice generated dynamically. So I would like it loaded using:

http://site.com/invoices/3333

And the HTML output would have to be converted to PDF.

Any good libraries that do this will be fine.

A: 

Yes, absolutely.

There are off-the shelf solutions that you can purchase that'll accept an HTML input and convert eh file for you.

There is also a common PDFLib that you can use to generate your PDF's using PHP but it requires you to create them manually by setting all the elements on the page.

This is another one I've used that's been pretty nice

jerebear
+1  A: 

You could use PDFlib. There's a documentation on php.net on how to utilize it. Alas, you have to live with a rather obtrusive watermark if you don't buy a license for PDFlib.

Michael Barth
+1  A: 

I personally don't trust all those services that are offered for free. Here is an application you can download: http://www.tufat.com/script19.htm

soulmerge
A: 

dompdf: http://www.digitaljunkies.ca/dompdf/

takes a html input string and returns the pdf-string.

it's not browser-level quality and has problems with css, but it works well enough for simple text documents.

Schnalle
+10  A: 

If your meaning is to create a pdf from php, pdflib will help you (as some other suggested).

Else, if you want to convert an HTML page in pdf via PHP, you'll find a little trouble outta here.. 3 years im triyng to do it as best as i can.

So, the options i know are:

DOMPDF : php class that wrap the html and build the pdf. Works good, customizable (if you know php), based on pdflib, if i remember right it takes even some CSS. Bad news: slow when the html is big or many complex.

HTML2PS: same of DOMPDF, but this one convert first in .ps (ghostscript), then, in whatever format you need (pdf, jpg, png). For me is little better then dompdf, but have the same speed problem.. oh, better compatibility with css.

Those two are php classes, but if you can install some software on the server, and access it throught passthru() or system(), give a look to these too:

wkhtmltopdf: based on webkit (safari's wrapper), is really fast and powerfull.. seem like is the best one (atm) for convert on the fly html pages to pdf, taking only 2 seconds for a 3 pages xHTML document with CSS2. Is a recent project, anyway, the google.code page is often updated.

htmldoc : this one is a tank, it really never stop/crash.. the project seem death in the 2007, but anyway if you dont need css compatibility this can be nice for ya.

DaNieL
Daniel, which one is the best for CSS then? WkHTMLToPdf?
Click Upvote
Yes, actually.we are triyn to use firefox too (with a command line addon by torisugary), becose, you know, nothing will take css better then a browser. wkhtmltopdf run on safari, so actually is the best, imho.I hope that mozilla will do something like it with gecko soon
DaNieL
Dan, can you call wkhtml through php? It seems like it needs to be called using shell manually
Click Upvote
Sure, passthru() should work.something like passthru('wkhtmltopdf_version.sh http://yourhtmlpage.html yourpdf.pdf' $return); would work well if php and apache permissions settings allow that. Tried on a windows machine, works good.
DaNieL
Oh, sorry i forget a thing: for unix-like systems there is actual a problem with the display of wkhtml - give a look at here if is your case:http://code.google.com/p/wkhtmltopdf/issues/detail?id=3otherise on windows system, you'll need to compile it under apache maybe (depends on your settings)
DaNieL
+2  A: 

what about TCPDF?

has lots of features, and one of advantage using it you can convert html to pdf on the fly

norewind
Your link to TCPDF is wrong, it should be: http://www.tcpdf.org/
stukelly
A: 

Also this one that does a goog job : html2pdf

A: 

The problems with the php classes are AFAIK 2:

  1. Speed: php take longs to parse the html structure and convert it in pdf with pdflib
  2. Reliability: when a new css, or style attribute will come, how to keep the script updates? how much time?

I think the only way to have an accurated conversion is using a browser wrapper becose, for definition, will always be the better html reader.

I suggest another link with some comparison: LINK That's where i read about wkhtmltopdf.. give a read to all the comments too, many others way are there...

DaNieL