views:

187

answers:

1

I'm trying to print pages pragmatically with a PHP script, to an office printer.

Here's what I've got so far:

I have a printer installed on the server, and I can send jobs to it with PHP via the command line's print command. I can also write plain text files with my PHP script, and then add them to the print cue. So printing PHP generated plain text files with PHP works just fine.

The question is, how can I create printable files, using PHP, that actually incorporate layout and graphics? In other words, if I created a web page with images and css, and then displayed the page in Firefox, and clicked print, that's what I want to come off the printer. Except it needs to work on a system without a GUI, and without user intervention.

So far I have these possible ideas:

  • Use a PHP PDF library to generate a PDF file and add that to the print cue (very complicated to get the positioning right)

  • Use the GD library to generate an image file (merging images and text into one page-sized image), and sending the final image to the printer.

Both of the above are complicated though, and would be tedious to implement and maintain. I'm wondering if there might be a simpler solution? Laying out the page using html and CSS would be the easiest option. Is there a library or script that can produce a PDF of a rendered page from raw html/css markup?

Or a way to run firefox (or another browser) in command line only mode, but has full rendering capabilities that could be sent to a pdf file or the printer?

Long question short- I'm looking for a way to send a rendered html/css page to a printer...

Any ideas?

+3  A: 

I would avoid using the PHP PDF library as it has a lot of constraints regarding DIVs and nesting, etc. You would have to write your pages specifically for the PDF environment.

Check out this page: http://code.google.com/p/wkhtmltopdf/

This can be run from the server (or PHP) and it will actually use the webkit engine (browser) to build the page just like a normal browser and return it so you can do whatever you want with it. You should be able to print this, save it, email it, etc. (They will be in PDF format)

angryCodeMonkey
That's a really cool program!
Sonny
@sonny - Yeah, a friend at work turned me on to it the other day and I have only begun to dream up uses for it!
angryCodeMonkey
Thanks Shane, that looks awesome! Let me play with it for a bit and I'll let you know if it works (and mark solved).
Nick
@nick - I am going to be building a report email system this weekend using it, so let me know how it goes or if you need a hand.
angryCodeMonkey
That was Soooooo easy! Marked as solved. It does render a few things funny but nothing worth complaining about.One question- how do you size the web page to make it fit one pdf page without scaling down? In other words, what pixel dimensions should I make the site?
Nick
@nick - Honestly I haven't gotten that deep into using it yet, however this page (http://ciaranmcnulty.com/blog/2009/04/converting-html-to-pdf-using-wkhtmltopdf) shows some command line arguments to actually specify your 'browser' window size. Probably you will just need to figure out how to pass that setting in your code and see if that helps. Please let me know as I may need this soon. =) (Not in a place to test it right this second)
angryCodeMonkey
Have you played with this any more? I've got it working fine, except the images become blurry in the pdf it creates. It's a nice sharp jpeg when viewing the source page in a browser....
Nick