How to convert HTML to a base64 image with Playwright and Ruby

The most popular way to do it is with the Grover gem. Grover is a wrapper around Playwright that makes it easy to generate images from HTML.

Install Playwright and Playwright client for Ruby

npm install playwright@1.27.1
gem install playwright-ruby-client

Get the base64 encoded image of some HTML

require 'playwright'

Playwright.create(playwright_cli_executable_path: './node_modules/.bin/playwright') do |playwright|
  playwright.chromium.launch(headless: false) do |browser|
    page = browser.new_page
    page.set_content('<html><body><h1>Hello World</h1></body></html>')
    @image = Base64.strict_encode64(page.screenshot())

puts @image


We created Urlbox to make converting HTML to images easy

Learn more about our website screenshot API.