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
# 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
endFrequently 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
Purchase orders PDF API
Generate purchase orders as PDFs from HTML.
Compare vs self-hosted Puppeteer
How self-hosted Puppeteer and Rendral differ on pricing, features, and setup.
PDFs in Vercel
A step-by-step guide to generating PDFs from Vercel.
A free tier, five endpoints, and SDKs for Node and Python.