FrameForge › Guides › HTML to PDF in Ruby on Rails

HTML to PDF in Ruby on Rails

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

Rails controller. Set FRAMEFORGE_KEY to your API key.

class InvoicesController < ApplicationController
  def show
    invoice = Invoice.find(params[:id])
    html = render_to_string(template: "invoices/pdf", layout: false, locals: { invoice: })

    uri = URI("https://api.frameforgeapi.com/render")
    res = Net::HTTP.post(uri,
      { html:, format: "pdf", width: 794, height: 1123 }.to_json,
      "Content-Type" => "application/json",
      "X-API-Key" => Rails.application.credentials.frameforge_key)

    return head :bad_gateway unless res.is_a?(Net::HTTPSuccess)
    send_data res.body, type: "application/pdf", disposition: "inline", filename: "invoice-#{invoice.id}.pdf"
  end
end

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

wicked_pdf wraps wkhtmltopdf (an old WebKit engine), and Grover uses Puppeteer, so you need Node and Chrome in your deploy.

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 · Ruby · Go · Java · C# / .NET · curl / Bash · Next.js · Laravel · Django