views:

2187

answers:

7

Hi,

I'm wondering is there a JavaScript library available that would allow me to generate an Image from the contents of a DIV.

Basically this is required for some Server-Side Printing code, which needs to print a background from the Browser.

What I'd ultimately like to do would be encode the DIV contents into PNG format and post up the encoded data with the print operation.

Any ideas if this is possible ?

[EDIT] What I have is a mapping application where background data is coming from an image server straight into a browser DIV (Think Google Maps). That div is background to me main data. When Print is pressed the server generates a PDF from the data it knows about, but knows nothing about the browser's background data. What I'd really like is to be able to provide the server with the browsers background image in some way!

Cheers, Ro

A: 

You can create an image tag from JavaScript but not the actual image in it: JS has no commands to allocate memory for the bitmap and it has no commands to render anything on it.

The usual solution is to have a report generator on the server which creates the image on request. Look at BIRT or JasperReports.

[EDIT] Based on your comment, the solution is simple: Examine the DIV, find the URL for the background image and replace the DIV with an IMG element. Put the URL into the SRC attribute and then print.

Aaron Digulla
True, thats how other things work....What I have is a mapping application where background data is coming from an image server straight into a browser DIV (Think Google Maps).That div is background to me main data.When Print is pressed the server generates a PDF from the data it knows about, but knows nothing about the browser's background data.What I'd really like is to be able to provide the server with the browsers background image in some way!
Ro
Isn't there the new "Canvas"? I think you can draw stuff using that?
Deniz Dogan
Yes, the Canvas should work, too, if the print engine supports it.
Aaron Digulla
A: 

I really don't think this is possible on the browser, certainly not without some kind of plugin.

Could you send some coordinate info or something to the web server and that way have the web server request the same map image from the image server?

mike nelson
A: 

Generating images was only possible in IE5 :( Then due to security reasons it was dropped. I'm still missing it.

Thinker
+2  A: 

Maybe it's possible with the Canvas:

https://developer.mozilla.org/en/drawing_graphics_with_canvas

Deniz Dogan
A: 
Ro
A: 

Does it have to be done on the browser side? I have seen where you can do a server side call and the MIME type on the server response is the image type. I think the example I'm thinking of was for b64 encoded jpegs in a db, but the process should be the same. The response would be the data that is currently in your DIV. Sorry if I'm way off base.

dsrekab
A: 

Very interesting question.

Actually I solve this problem using ajax (transfer images' positions to the server, server creates one image from pieces, save it and send url to the client). I don't very like this solution but I don't know other yet.

Roman