FrameForge › Guides › HTML to PDF in Ruby

HTML to PDF in Ruby

Send HTML (or a URL) to the FrameForge API and get a PDF back. It is rendered in real Chromium, so modern CSS, web fonts and JavaScript all work. No browser to install or babysit.

The code

Ruby 3 standard library (net/http). Set FRAMEFORGE_KEY to your API key.

require "net/http"
require "json"

uri = URI("https://api.frameforgeapi.com/render")
req = Net::HTTP::Post.new(uri, {
  "Content-Type" => "application/json",
  "X-API-Key" => ENV.fetch("FRAMEFORGE_KEY"),
})
req.body = {
  html: "<h1>Invoice #1001</h1><p>Total: £49.00</p>",
  format: "pdf", width: 794, height: 1123,
}.to_json

res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true, read_timeout: 60) { |http| http.request(req) }
raise "Render failed: #{res.code} #{res.body}" unless res.is_a?(Net::HTTPSuccess)
File.binwrite("invoice.pdf", res.body)

How it works

Rendering a URL instead of HTML

{"url": "https://example.com/report", "format": "pdf", "width": 1280, "height": 800, "fullPage": true}

Doing it yourself instead

Grover (Puppeteer) and Ferrum drive headless Chrome from Ruby, and Prawn builds PDFs programmatically without HTML.

An API makes sense when you don't want Chromium on your servers, or when you need images (PNG/JPEG) and PDFs from the same call. If you render a few PDFs a month on a server you already run, self-hosting is perfectly reasonable.

Free tool: Website to PDF. Other languages: Node.js · Python · PHP · Go · Java · C# / .NET · curl / Bash · Next.js · Laravel · Django · Ruby on Rails