Discover and share the best of the web!
Learn more about Digg by taking the tour.
Convert HTML to PDF with PHP
digitaljunkies.ca — Handy for PHP developers looking to generate PDFs from their HTML content: dompdf is an HTML to PDF converter written entirely in PHP5. It has no dependencies on external libraries (e.g. html2ps) and it supports CSS 2.1.
- 1408 diggs
- digg it
- jhowe, on 10/12/2007, -0/+3Dompdf works well. I've used it in one of my projects.
- KamikazeeDriver, on 10/12/2007, -4/+4I still find it easier to use a .pdf printer driver that "prints" whatever you want to a .pdf file.
- scsikool, on 10/12/2007, -0/+1Sort of nice... ;-)
Didn't support ol or fieldset in my test. I'll have to stick with fpdf in the meantime.
- bscott86, on 10/12/2007, -0/+1Awesome idea. How is the execution time?
- pornel, on 10/12/2007, -0/+2Not-so-good, as you can expect from that type of script.
- StephnDolenc, on 10/12/2007, -14/+4i recommend http://www.pdfonline.com/ if you want to convert alsmost anything (doc, ppt, sls, html, rtf, pps, txt, pub, jpg, gif, tiff, bmp, png, emf, wmf) to a pdf
however, i don't find this useful. pdf is the worst file format -- i hate adobe's reader openning in my browser and crashing firefox.- stacyspear, on 10/12/2007, -1/+2Why not set it to open in Acrobat instead.
- dxbmatt, on 10/12/2007, -0/+7This is different, it will allow you to generate PDF's programatically as opposed to individual file conversion.
Very useful, I currently use fpdf but I'll give this a spin.
As for the browser problem, try the 'PDF Download' extension for Firefox that detects a .pdf link and offers
you a choice of downloading to view offline, converting to HTML or opening in the browser - boycy, on 10/12/2007, -0/+3You can change that behaviour, anyway your comment is on Fx not the PDF format.
As stated pdfonline.com is for use by people, so not any help for automated PDF generation which is this toy's purpose. - AmitGupta, on 10/12/2007, -0/+2if you dislike Adobe Reader's slow performance, then there are other PDF readers available as well which you can try, like Foxit reader, it opens PDF files in a snap!! :)
- slonkak, on 10/12/2007, -1/+0I also agree with AmitGupta, I use FoxIt. It opens pdf's in less than a second. It's also free. I hate adobe, especially when it breaks and the only way to fix it is to format your computer :(
- mad4, on 10/12/2007, -3/+2I use fpdf
- Gnascher, on 10/12/2007, -0/+4Thank you for that information. However, the two are not comparable.
FPDF allows you to create PDF documents programmatically, that's true ... so does another good one ... ezPDF.
However, this one allows you to DIRECTLY from HTML to PDF programmatically ... something the other two mentioned above definitely do NOT do.
Too bad in requires PHP 5 though ... most of my clients who could use this currently are on a PHP4.x code base, and PHP5 is only just beginning to be deployed on most shared hosting providers. - dashifen, on 10/12/2007, -0/+1Actually, fpdf can be used to automatically change an html page to a pdf. Here's only the first example that I found using google (http://radekhulan.cz/item/html2pdf-convert-your-xhtml-to-pdf-easily) but there are a couple of other extensions to the fpdf class, most of which are linked from the fpdf site (http://www.fpdf.org/), which can also convert html to a pdf. Just search "html2pdf fpdf" on google and you'll get some good hits.
- Gnascher, on 10/12/2007, -0/+4Thank you for that information. However, the two are not comparable.
- sirber, on 10/12/2007, -0/+2sweet! I hope it's fast! :D
I've been using htmldoc for years and tryed html2ps/html2pdf (php version) but it's so slow.
Thanks!- sirber, on 10/12/2007, -0/+2no nested tables
slow on big html.
sad :( - miken32, on 10/12/2007, -1/+0Nobody should be nesting tables.
- sirber, on 10/12/2007, -0/+2no nested tables
- splitbrain, on 10/12/2007, -1/+2The problem with all the available PHP only solutions for PDF generation is their lack of proper UTF-8 handling (with font-subsetting). AFAIK only the commercial version of pdflib currently supports this :-(
- zaguar, on 10/12/2007, -1/+3True, the lack of support of UTF-8 in PHP/PDF content generation is a killer for some multi-lingual/national websites. They say on the site that only the licensed version has unicode, so for those who don't pay, no Unicode for you.
- DominicanZero, on 10/12/2007, -0/+2@StephnDolenc: That's quite the lame excuse, as Adobe Reader is not by a mile the only free software out there that can read PDF files.
Personally, I've been using Foxit Reader since around a year and a half now, and it's never disappointed me. It's blazing fast, it's easy, it's intuitive, it shows the KISS (Keep It Stupidly Simple) principle. True, for printing issues, I'll use Adobe Reader (or, if I'm at my office, the licensed copy of Jaws PDF Editor), but for everyday PDF handling, Foxit is what I use. So that's a pretty stupid and ignorant statement you're making there.- cypher35, on 10/12/2007, -2/+3i just use osx's built-in pdf support ^_^
- GreenLantern33, on 10/12/2007, -0/+1I was just looking for something like this! Thanks.
- ahdustin, on 10/12/2007, -0/+1I was just about to start looking for this sort of tool for a new project. Thanks.
- jameshartford, on 10/12/2007, -2/+1It doesn't look like it converts forms.
- Tezgno, on 10/12/2007, -0/+1I've been using this for about a year now (haven't updated to the newest version yet). As far as speed goes, it's pretty fast. On a dual 1GHz G4 XServe, creation takes about 3-4 seconds.
- kickarse, on 10/12/2007, -0/+4IT does have dependencies... Did you even read this page before you wrote the summary?
http://www.digitaljunkies.ca/dompdf/install.php- Discosis, on 10/12/2007, -0/+3Yeah, I was wondering how the OP defines "no dependencies", too.
"PDF rendering is currently provided either by PDFLib (www.pdflib.com) or by a bundled version the R&OS CPDF class written by Wayne Munro (www.ros.co.nz/pdf). (Some performance related changes have been made to the R&OS class, however). In order to use PDFLib with dompdf, the PDFLib PECL extension is required. Using PDFLib improves performance and reduces the memory requirements of dompdf somewhat, while the R&OS CPDF class, though slightly slower, eliminates any dependencies on external PDF libraries."
... I have no idea WTF I need to install now to use this.
- Discosis, on 10/12/2007, -0/+3Yeah, I was wondering how the OP defines "no dependencies", too.
- pornel, on 10/12/2007, -0/+2I was shocked to read "supports CSS2.1" - that'd be quite a feat!
Unfortunately it's support for CSS is much poorer than even IE4/NN4. From docs: "absolute & relative positioning and floats do not work,yet.", so "supports a few basic CSS properties" would be more accurate.- ihaterobots, on 10/12/2007, -0/+2You're totally correct. I recently tried to use dompdf for a project and the CSS support was pretty pathetic. Sure, it will catch font color and size, but that's about it.
Nobody likes to pay for software libraries, but we ended up buying the commercial version of Pdflib (mentioned above) and it worked as advertised. The documentation is terrible, but it's a pretty good package. If you're serious about using PHP to write PDF-centric apps, pdflib is a much more complete solution than dompdf.
Too bad really. In my searching I was really surprised there were not better open/free solutions.
- ihaterobots, on 10/12/2007, -0/+2You're totally correct. I recently tried to use dompdf for a project and the CSS support was pretty pathetic. Sure, it will catch font color and size, but that's about it.
- got-haggis, on 10/12/2007, -0/+1how does this compare to the commercial PDFLIB for php? The real power of PDFLIB is that you can open existing PDF files and add pages to them using PHP.
- jhowe, on 10/12/2007, -0/+2PDFLIB is going to be faster because (correct me if I'm wrong) it's an extension to PHP, and domPDF is a class.
- WoozyDuck, on 10/12/2007, -2/+0It doesnt work good!
I get this result when submitting a document (HTML):
Warning: sprintf() [function.sprintf]: Too few arguments in /home/benj/projects/dompdf-0.5/include/attribute_translator.cls.php on line 291
Warning: sprintf() [function.sprintf]: Too few arguments in /home/benj/projects/dompdf-0.5/include/attribute_translator.cls.php on line 291
Warning: sprintf() [function.sprintf]: Too few arguments in /home/benj/projects/dompdf-0.5/include/attribute_translator.cls.php on line 291
Unable to stream pdf: headers already sent
Warning: unlink(/home/benj/projects/dompdf-web/lib/res/broken_image.png) [function.unlink]: Permission denied in /home/benj/projects/dompdf-0.5/include/cpdf_adapter.cls.php on line 235- cypher35, on 10/12/2007, -0/+3first off, i think you mean to say it doesn't work "well"
secondly, did you take any time to properly set it up or did you throw the files on your server and hope for the best?
- cypher35, on 10/12/2007, -0/+3first off, i think you mean to say it doesn't work "well"
- raccettura, on 10/12/2007, -0/+1Nice until you see the memory requirements (32MB+ recommended). Eek. Per request.
- waterandfood, on 10/12/2007, -0/+1For what it's worth, there is a php extension in the works that will not require an external library like the current one does. It'll be faster as it won't be written in php but rather for php. It's api will most likely end up looking like fpdf.
- Snoopykiss, on 10/12/2007, -0/+1I've been using the HTML_ToPDF Extension from rustyparts.com for a while to generate reports to send to my managers. Works fine for me.
http://www.rustyparts.com/pdf.php- idiggit, on 10/12/2007, -0/+1I tried the demo and document it generate is plain no good. I have found rustyparts too but with no demo I guess I'll have to give it a spin on my system. ~(_X-(o)
- WoozyDuck, on 10/12/2007, -0/+1@cypher35:
I have used the ONLINE DEMO and got that error message! By the way who is the project manager/author of that online tool? Your boyfriend or something? In fact I have tried it now and I got another error message:
Fatal error: Call to a member function get_cellmap() on a non-object in /home/benj/projects/dompdf-0.5/include/table_cell_frame_reflower.cls.php on line 64
Here is a screenshot from the error as well:
http://img444.imageshack.us/img444/7319/shot3ev.gif
Isn't that a beautiful answer to your question?
Instead of being rude in replying, you better send these errors to him/her in order to improve the system FIRST and then advertising about it on internet!
Good luck!
p.s. I don't mind about my English cuz it is not my first language! so don't bother! - joeyjojo, on 10/12/2007, -0/+2Why take a great, accessible, open format like HTML and stick it in a crappy PDF file?
- jholdaway, on 10/12/2007, -0/+1PDF has its advantages, and it is getting more open, as mac users have known for years and vista is rumored to support.
- Snoopykiss, on 10/12/2007, -0/+0In my case, it's better for off-line viewing. For the reports I generate for my managers, it's much easier to email them one file with images and everything they need, rather than an HTML file and directory.
- Pieps457KHz, on 10/12/2007, -0/+0I use htmldoc.org but it kinda blows. It needs CSS support badly!
- Ensnared, on 10/12/2007, -0/+1I just use FPDF to generate PDF's from database content with PHP - that actually has no dependencies, as opposed to this (guess the poster didn't bother actually checking his facts).
Clientside I use PDFCreator to create PDF document in Windows, and cups-pdf to do the same in Linux. But I suppose this is just as good if you'd rather run a PHP-enabled webserver to get that kind of stuff done.- benjcarson, on 10/12/2007, -0/+3Just to clarify, dompdf does not *require* PDFlib. It really does work out of the box without any dependencies.
Here are the facts:
dompdf supports multiple rendering backends, which I guess is the source of the confusion. By "rendering backend" I mean a class or library that can draw text or graphics on a PDF (which is basically all that FPDF is). dompdf bundles a native PHP PDF renderer written by the ezPDF folks but it also supports PDFlib and even GD for rendering to PNGs or GIFs or whatever. The native PHP renderer gives you a working converter without any external dependencies, but if you're fortunate enough to have a licensed version of PDFlib, you get improved performance and Unicode support.
- benjcarson, on 10/12/2007, -0/+3Just to clarify, dompdf does not *require* PDFlib. It really does work out of the box without any dependencies.
- vigodagod, on 10/12/2007, -1/+1I've found that it works pretty good for what I need it for and the price is extremely reasonable. Free!
- Lochnivar, on 10/12/2007, -0/+0Excellent program, great job
- flo2006, on 10/11/2007, -0/+0If you need a converter for ASP.NET, here is a great tool: http://www.html-to-pdf.net
- skillipedia, on 10/11/2007, -0/+0How does it compare with Java iText ?
Digg is coming to a city (and computer) near you! Check out all the details on our