Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails convert html to image

I'm looking for a way to convert html tags to a image on the fly...

That means, that I want to be able to make a image_tag with a path to a method which returns the image created form html.

I was looking for a solution on that, but couldn't come up with a proper way to solve that...

Any Ideas?

Maechi

like image 601
Markus Avatar asked Dec 30 '10 15:12

Markus


2 Answers

IMGkit can do the job (details on github)

Create JPGs using plain old HTML+CSS

kit = IMGKit.new('http://google.com')
kit.to_jpg
kit.to_jpeg 
kit.to_png
kit.to_tif
kit.to_tiff

or in your controller

@kit = IMGKit.new(render_as_string)

format.jpg do
  send_data(@kit.to_jpg, :type => "image/jpeg", :disposition => 'inline')
end
like image 138
denisjacquemin Avatar answered Sep 17 '22 19:09

denisjacquemin


I'm going to take a wild guess here and guess that you want to convert HTML to an image, so take a "snapshot" of a web page or something. I'm not sure exactly how to do this in one step, but one way to do it is to use PDFKit to convert to PDF and then use RMagick to convert to whatever image format you want.

like image 27
sarahhodne Avatar answered Sep 16 '22 19:09

sarahhodne