How to take screenshot of a website with Rails 3.1? — without using a service

后端 未结 4 402
[愿得一人]
[愿得一人] 2021-01-31 12:02

Almost every answer I\'ve found references using some existing service. Is there a way to do this using Rails 3.1 programmatically? This was dead easy to do wit

相关标签:
4条回答
  • 2021-01-31 12:21

    A simple, but Mac only, solution seems to be http://www.paulhammond.org/webkit2png/

    Just chmod -x that script and use as python webkit2png http://www.google.com/ and it creates 3 files:

    1. Full screenshot
    2. Thumbnail of the top most portion of site
    3. Thumbnail of the full screenshot
    0 讨论(0)
  • 2021-01-31 12:29

    There is a Rails gem for this task.

     gem install selenium-webdriver
    

    Simple use case:

    require 'selenium-webdriver'
     width = 1024
     height = 728
     driver = Selenium::WebDriver.for :firefox
     driver.navigate.to 'http://domain.com'
     driver.execute_script %Q{
       window.resizeTo(#{width}, #{height});
     }
     driver.save_screenshot('/tmp/screenshot.png')
     driver.quit
    
    0 讨论(0)
  • 2021-01-31 12:32

    This might help: https://github.com/csquared/IMGKit

    0 讨论(0)
  • 2021-01-31 12:32

    You could use wkhtmltoimage to load up the webpage and save it as an image, then imagemagick, (or one of the ruby wrappers for it) to crop it.

    wkhtmltoimage www.google.com output.jpg
    convert -crop 100x100+0+100 output.jpg cropped.jpg
    

    There isn't a prebuilt wkhtmltoimage binary for OSX though, so perhaps you may want to use wkhtmltopdf instead and then imagemagick to convert to an image.

    wkthmltopdf www.google.com output.pdf
    convert -crop 100x100+0+100 output.pdf cropped.jpg
    
    0 讨论(0)
提交回复
热议问题