rails convert html to image

Markus picture Markus · Dec 30, 2010 · Viewed 17.9k times · Source

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

Answer

denisjacquemin picture denisjacquemin · Nov 28, 2011

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