开发者

How can I render a web page on the server (no GUI) for printing?

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

开发者_StackOverflow中文版

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?


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜