Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an Image of a DIV in JavaScript (GIF/PNG)

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

like image 443
Ro. Avatar asked May 08 '09 10:05

Ro.


3 Answers

Maybe it's possible with the Canvas:

MDN - Drawing Graphics with Canvas

like image 106
Deniz Dogan Avatar answered Nov 20 '22 14:11

Deniz Dogan


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.

like image 33
Aaron Digulla Avatar answered Nov 20 '22 14:11

Aaron Digulla


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.

like image 1
Roman Avatar answered Nov 20 '22 13:11

Roman