Janak
Janak

Reputation: 5085

How can I save a webpage as an image in my rails app?

In my rails app I have a need to save some webpages and display them to the user as images. For example, how would I save www.google.com as an image?

Upvotes: 1

Views: 480

Answers (4)

flitzwald
flitzwald

Reputation: 20240

There is a command line utility called CutyCapt that is using the WebKit-Rendering engine to render HTML-Pages into various image formats. Maybe this is for you? http://cutycapt.sourceforge.net/

Upvotes: 7

PreciousBodilyFluids
PreciousBodilyFluids

Reputation: 12011

Prohibitively difficult to do in pure Ruby, so you'd want to use an external service for this. Browsershots does it, for example, and it looks like they have an api, although I haven't used it myself. Maybe someone else can chime in with alternative but similar services.

You'll also want to read up on delayed_job or something similar, to make sure you're accessing those page images as a background task and that it doesn't interfere with your actual application.

Upvotes: 1

bezmax
bezmax

Reputation: 26142

You can't do it easily (probably can't do it at all). Each page is just a text - html data. The view you want to make an image of is a rendered page. Browser renders the page using tonns of techniques like html parsing, javascript parsing, css parsing, font rendering, etc.. To make the screenshot of google page - you would need to do all the rendering somewhere in memory and then take a screenshot of rendered page.

That task is almost impossible (there is nothing fully impossible). If you are really eager to donate tonns of time to accomplish that task - you should do this steps: 1) Find some opensource rendering engine. Firefox would do. 2) Find some way to communicate between ruby-on-rails and that engine. 3) Wire it all together and see the results.

However, I see steps 1 and 2 as nearly impossible.

Upvotes: 0

Related Questions