HTML to PDF with Ruby and Playwright

Install Playwright and Playwright client for Ruby

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

Convert HTML to PDF and save to file

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>')
    page.pdf(path: './html.pdf')
  end
end

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

We created Urlbox to make converting HTML to images easy

Learn more about our website screenshot API.