stack twitter tryhackme rss linkedin cross

Wilco van Esch

Skip to main content

Search results

    Configuring the wkhtmltoimage path for IMGKit on Windows

    If you'd like to use IMGKit, you'll also have to install wkhtmltopdf, which includes wkhtmltoimage - a command line tool to generate images from an HTML page.

    From IMGKit's documentation:

    If you're on Windows or you installed wkhtmltoimage by hand to a location other than /usr/local/bin you will need to tell IMGKit where the binary is.

    If you're using Rails, you can define this in config/initializers/imgkit.rb If you're not using Rails, you can use the same definition where you're requiring imgkit.

    IMGKit.configure do |config|
      config.wkhtmltoimage = '/path/to/wkhtmltoimage'
    end
    

    In Windows, that path has to include the filename and extension. If you don't add it, you'll receive a NoExecutableError or a "Permission Denied" error.

    Example path:

    C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltoimage.exe

    You can test the configuration in irb like this:

    irb(main):003:0> html = "http://example.com/index.html"
    => "http://example.com/index.html"
    
    irb(main):004:0> kit = IMGKit.new(html, :quality => 50)
    => #<IMGKit:0x00000002fa0120
    @source=#<IMGKit::Source:0x00000002fa00d0
    @source="http://example.com/index.html">, @stylesheets=[],
    @javascripts=[], @options={:height=>0, :quality=>50}>
    

    If it doesn't work, try adding the path to your PATH environment variable.

    If it still doesn't work, you can amend the path in the gem's configuration file directly.

    # C:\Ruby##-x64\lib\ruby\gems\#.#.#\
    gems\imgkit-#.#.#\lib\imgkit\configuration.rb
    def wkhtmltoimage
      @wkhtmltoimage ||= begin
        path = (using_bundler? ? `bundle exec which wkhtmltoimage`
        : `which wkhtmltoimage`).chomp
        path =
        'C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltoimage.exe'
        if path.strip.empty?
    # Fallback
        path
      end
    end