ruby-on-railswkhtmltoimageimgkit

undefined method `to_file' for #<String:0xa042bf8> - IMGKit


I'm trying to take snapshots from some websites but I'm having some problems with IMGKit.

When I try to run this code on a Rails Task:

kit = IMGKit.new('http://www.adlibituradventures.com/')
kit = kit.to_img(:jpg) 
file_path = 'public/images/myimage.jpg'
file = kit.to_file(file_path)

I receive the following error:

rake aborted!
undefined method `to_file' for #<String:0xa042bf8>
/home/paladini/Documents/Projetos/bittle/btc-stores/lib/tasks/crawl_usebitcoin.rake:269:in `get_image'
/home/paladini/Documents/Projetos/bittle/btc-stores/lib/tasks/crawl_usebitcoin.rake:113:in `block (3 levels) in <top (required)>'
/home/paladini/Documents/Projetos/bittle/btc-stores/lib/tasks/crawl_usebitcoin.rake:97:in `each'
/home/paladini/Documents/Projetos/bittle/btc-stores/lib/tasks/crawl_usebitcoin.rake:97:in `block (2 levels) in <top (required)>'
/home/paladini/Documents/Projetos/bittle/btc-stores/lib/tasks/crawl_usebitcoin.rake:43:in `block (2 levels) in <top (required)>'
/home/paladini/.rvm/gems/ruby-2.0.0-p353@btc-stores/bin/ruby_executable_hooks:15:in `eval'
/home/paladini/.rvm/gems/ruby-2.0.0-p353@btc-stores/bin/ruby_executable_hooks:15:in `<main>'

My config/initializers/imgkit.rb:

IMGKit.configure do |config|
  config.default_options = {
    :'use-xserver' => true
  }
end

If I remove the configuration file, then execute the rake task I got:

rake aborted!
Command failed: /usr/local/bin/wkhtmltoimage --height 1000 --format jpg http://www.adlibituradventures.com/ -: Loading page (1/2)
QSslSocket: cannot resolve SSLv2_client_method               ] 10%
QSslSocket: cannot resolve SSLv2_server_method
No bp log location saved, using default.                     ] 36%
[000:000] Cpu: 6.37.5, x4, 2399Mhz, 2893MB
[000:000] Computer model: Not available
[000:000] Browser XEmbed support present: 1
[000:000] Browser toolkit is not Gtk2 (0).
[000:001] Using Xt toolkit
No bp log location saved, using default.
[000:000] Cpu: 6.37.5, x4, 2399Mhz, 2893MB
[000:000] Computer model: Not available
Rendering (2/2)                                                    
QPixmap: Cannot create a QPixmap when no GUI is being used   ] 25%
QPixmap: Cannot create a QPixmap when no GUI is being used
QPixmap: Cannot create a QPixmap when no GUI is being used
QPixmap: Cannot create a QPixmap when no GUI is being used
QPixmap: Cannot create a QPixmap when no GUI is being used
QPixmap: Cannot create a QPixmap when no GUI is being used
QPainter::begin: Paint device returned engine == 0, type: 2
QPainter::setRenderHint: Painter must be active to set rendering hints
QPainter::translate: Painter not active
QPainter::setPen: Painter not active
QPainter::setBrush: Painter not active
QPainter::setWorldTransform: Painter not active
QPainter::setOpacity: Painter not active
QPainter::setFont: Painter not active
*** NSPlugin Viewer  *** *** NSPlugin Viewer  *** ERROR: rpc_end_sync called when not in sync!
ERROR: rpc_end_sync called when not in sync!

So, I'm trying to use this configuration file because this tutorial tell's that the --use-xserver flag solve the problem with QPixMap. But i don't know if the problem is with IMGKit or wkhtmltoimage.

Before you ask, I'm currently using:

Please, someone can help me?


Solution

  • kit = kit.to_img(:jpg)

    then kit is changed to a string.

    If you want to keep kit, then change the variable name.