1

Currently I am cursed with trying to develop a Ruby on Rails app in Windows and then deploy to Linux. I am looking for a gem/plugin that will allow me to capture screenshots and feed them back to the user. WebSnap looked promising but I keep running into issues (error: Could not locate wkhtmltoimage-proxy executable) even though I have the bat files in the path and in just about any folder I can think of.

So, does anyone have a suggestion for a library that will work on Windows that will allow me to do this? Or in another vein a way to resolve the wkhtmltoimage-proxy executable issue?

Code:

format.png {  
  html = render :action => "show.html.erb", :layout => "application.html.erb"

  Rails.logger.debug("html: " + html.inspect)
  snap = WebSnap.new(html,  :format     => 'png',
                            :'scale-h'  => nil,
                            :'scale-w'  => nil,
                            :'crop-h'   => nil,
                            :'crop-w'   => nil,
                            :quality    => 100,
                            :'crop-x'   => nil,
                            :'crop-y'   => nil )

  send_data snap.to_bytes,  :filename     => "dashboard.png",
                            :type         => "image/png",
                            :disposition  => 'inline'
}
Jordan Running
  • 91,621
  • 15
  • 164
  • 165
ScottJShea
  • 6,683
  • 11
  • 41
  • 65

1 Answers1

3

The problem appears to be that WebSnap's developer just didn't think to make it work on Windows. You can spot the problem right in the source:

def initialize(url_file_or_html, options={})
  # ...
  raise NoExecutableError.new if wkhtmltoimage.nil? || wkhtmltoimage == ''
end
# ...

def wkhtmltoimage
  @wkhtmltoimage ||= `which wkhtmltoimage-proxy`.chomp
end
#                       ^-- derp

Basically to find the path to the executable, WebSnap calls which, which is a *nix command not available on most Windows machines.

You have a couple options here:

  1. You could patch it yourself to work correctly (and submit a pull request to the developer, thereby becoming an unsung hero to fellow Windows developers who would encounter this same issue in the future).

  2. You could file an issue with the developer and hope that s/he fixes it quickly enough for your project.

  3. You could check out the answers on this question for getting a which-equivalent command on Windows.

  4. You could run your app under Cygwin, which bundles most common Linux commands.

  5. You could monkey-patch or subclass the library, something like:

    class MySnapper < WebSnap::Snapper
      ExecPath = '/absolute/path/to/wkhtmltoimage-proxy'
    
      def wkhtmltoimage
        super
    
        @wkhtmltoimage = ExecPath if @wkhtmltoimage.nil? || @wkhtmltoimage.empty?
        @wkhtmltoimage
      end
    end
    
    # And then instead of WebSnap::Snapper.new, use MySnapper.new
    
  6. You could invoke wkhtmltoimage directly. That is assuming a Windows binary comes bundled with wkhtmltopdf/wkhtmltox or you can build it yourself. It's short on docs but if you scroll down to 13 Apr. 2011 on this page you'll see a useful comment, or you could try to infer the correct parameters from WebSnap's source.

I'm most in favor of Option 1 because it both solves your problem and helps out other devellopers. However, Option 5 is probably the quickest and easiest--that is, unless there are more parts of the gem that only work on unixy platforms.

Community
  • 1
  • 1
Jordan Running
  • 91,621
  • 15
  • 164
  • 165
  • 2
    Jordan, thank you. I will go for option one. The which thing was throwing me; I thought it was something about the setup not working right; never occurred to me it was a *nix based command. – ScottJShea Sep 26 '11 at 18:29