57

Possible Duplicate:
Convert HTML + CSS to PDF with PHP?

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://example.com/invoices/3333

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

Any good libraries that do this will be fine.

Community
  • 1
  • 1
Ali
  • 261,656
  • 265
  • 575
  • 769
  • GrabzIt has a free PHP Library for converting HTML to PDF: http://grabz.it/html-to-pdf-image-api.aspx –  Dec 05 '16 at 15:12

1 Answers1

62

If you wish to create a pdf from php, pdflib will help you (as some others suggested).

Else, if you want to convert an HTML page to PDF via PHP, you'll find a little trouble outta here.. For 3 years I've been trying to do it as best as I can.

So, the options I know are:

DOMPDF : php class that wraps the html and builds 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 complex.

HTML2PS: same as DOMPDF, but this one converts first to a .ps (ghostscript) file, then, to whatever format you need (pdf, jpg, png). For me is little better than dompdf, but has the same speed problem.. but, 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 powerful.. seems like this is the best one (atm) for converting html pages to pdf on the fly; taking only 2 seconds for a 3 page xHTML document with CSS2. It is a recent project, anyway, the google.code page is often updated.

htmldoc : This one is a tank, it never really stops/crashes.. the project looks dead since 2007, but anyway if you don't need CSS compatibility this can be nice for you.

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
Strae
  • 18,807
  • 29
  • 92
  • 131
  • 3
    Daniel, which one is the best for CSS then? WkHTMLToPdf? – Ali Apr 09 '09 at 08:37
  • 1
    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 – Strae Apr 09 '09 at 08:44
  • Dan, can you call wkhtml through php? It seems like it needs to be called using shell manually – Ali Apr 09 '09 at 08:49
  • 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. – Strae Apr 09 '09 at 09:10
  • 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=3 otherise on windows system, you'll need to compile it under apache maybe (depends on your settings) – Strae Apr 09 '09 at 09:19
  • 1
    wkhtmltopdf has worked the best and with the least setup steps in my experience. – Andrew Ensley Jul 26 '12 at 17:18
  • Wkhtmltopdf is awesome and can convert exact html+css to pdf – Ahesanali Suthar Jun 24 '16 at 15:22
  • But they all fail with flexbox and grid, amirite? – s3c Mar 11 '21 at 12:10