How to respond_to PNG or JPG in Rails and generate image from HTML?
Check out here: http://weblog.rubyonrails.org/2006/12/19/using-custom-mime-types
Mime::Type.register "image/png", :png
# then in your controller action
def show
respond_to do |format|
format.html { }
format.png { }
end
end
UPD
What about image generating. If you need to convert your HTML page into image. You can use wkhtmltoimage
http://code.google.com/p/wkhtmltopdf/downloads/detail?name=wkhtmltoimage-0.10.0_beta2-static-amd64.tar.bz2&can=4&q=
There is no gem like pdfkit
for wkhtmltopdf
but it is easy to use.
Also you can use pdfKIT
gem and after that convert PDF to PNG with imagemagick. That is quite easy too.
UPD
Istead of using SnapShot
I prefer to use IMGKit
gem
https://github.com/csquared/IMGKit
Related videos on Youtube
Voldy
Fullstack and mobile developer. Currently enjoying coding in Elixir and Swift.
Updated on June 04, 2022Comments
-
Voldy almost 2 years
I am looking for a gem or solution to generate image in controller response.
It would be nice if it's possible to do in controller like that:
respond_to :html, :png def show ... respond_to do |format| format.html format.png { ??? } # some html to png converter end end
When the
png
format is requested the response handles with template:#show.png.haml %h1 Some title %p Some content
The result should be an image.
I know about
pdf
generation solutions PDFKit, prawn and am looking for image generation.Does anybody know working solution/example? Any starting point would be very appreciated.
-
Voldy about 13 years@floor, thanks for response. My bad, I was not very clear with my question. The question was more about image generation from html. Now I clarified it.
-
fl00r about 13 yearsYou need to convert your html to image? look upate
-
Voldy about 13 yearsthanks again. There is a gem for
wkhtmltopdf
: github.com/siuying/websnap. And here is an example freezzo.com/2010/07/29/…