HTML to PNG with Ruby and Selenium

Install Selenium Webdrivers for Ruby

gem install webdrivers

Convert HTML to PNG and save to file

require 'webdrivers'

options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--headless')

driver = Selenium::WebDriver.for :chrome, options: options

html = "<html><body><h1>Hello World</h1></body></html>"

driver.get("data:text/html;charset=utf-8," + html)

driver.save_screenshot("./html.png")

Find out how to change the size of the viewport with Ruby and Selenium.

We created Urlbox to make converting HTML to images easy

Learn more about our website screenshot API.