Rendral

Stack · Ruby on Rails

Generate PDFs in Ruby on Rails

The Rails ecosystem's PDF gems mostly wrap wkhtmltopdf — a project archived in 2023 whose renderer predates flexbox. wicked_pdf and PDFKit inherit that engine, and Grover swaps it for Puppeteer, which means Node and Chrome on every dyno.

Use render_to_string on the view you already have and POST the HTML. send_data streams the result to the browser, and your Gemfile gains nothing but an HTTP client you probably already use.

01Call it from Ruby on Rails

render.ts
# app/controllers/invoices_controller.rb
require "faraday"
require "json"

class InvoicesController < ApplicationController
  def show
    html = render_to_string(
      template: "invoices/show",
      layout: "pdf",
      formats: [:html]
    )

    res = Faraday.post("https://rendral.com/api/v1/pdf") do |req|
      req.headers["Authorization"] = "Bearer #{ENV.fetch('RENDRAL_API_KEY')}"
      req.headers["Content-Type"] = "application/json"
      req.body = {
        html: html,
        options: { format: "A4", pageNumbers: true }
      }.to_json
    end

    send_data res.body, filename: "invoice.pdf", type: "application/pdf"
  end
end

Frequently asked

Can I keep using my existing ERB views?

Yes. render_to_string produces the same HTML your browser gets; use a dedicated pdf layout if you want to drop navigation and scripts.

What replaces wicked_pdf's page numbering?

Set pageNumbers: true for a centred footer, or supply your own header and footer HTML — the same CSS-driven approach, without the binary.

Do asset-pipeline URLs work?

Only if they're absolute. Use asset_url rather than asset_path so images and stylesheets resolve from outside your app.

Is Heroku supported?

Yes, and it's a good fit — nothing to install via buildpacks, and no slug-size cost for a bundled Chrome.

Related

Start rendering in minutes.

A free tier, five endpoints, and SDKs for Node and Python.