Ruby

Generate PDFs in Ruby on Rails

Generate and send a PDF from a Rails controller with the standard library — no Wicked PDF.

PDF generation in Rails usually pulls in Wicked PDF or Prawn plus a wkhtmltopdf binary. PDFgen needs neither: POST your HTML and data with Net::HTTP from the standard library and send_data the result.

The controller action below renders a downloadable invoice. Swap in any HTML — including an ERB partial rendered to a string.

Generate a PDF in Ruby on Rails

POST your HTML and data to /api/v1/generate with your API key, then save the PDF that comes back. Swap in your own HTML or a saved template_id and loop over records to render at scale.

require "net/http"
require "json"
# app/controllers/invoices_controller.rb
class InvoicesController < ApplicationController
def show
res = Net::HTTP.post(
URI("https://pdfgen.com/api/v1/generate"),
{
html: "<h1>Invoice {{number}}</h1><p>Billed to {{client}} — total {{total}}</p>",
engine: "handlebars",
format: "A4",
data: { number: "INV-001", client: "Acme Corp", total: "$2,400.00" }
}.to_json,
"Authorization" => "Bearer #{Rails.application.credentials.pdfgen_key}",
"Content-Type" => "application/json"
)
send_data res.body, filename: "invoice.pdf", type: "application/pdf"
end
end

Convert existing HTML to PDF in Ruby on Rails

Already have finished HTML? Skip templating — render an ERB view to HTML, then convert it and send it with engine: "legacy", which tells the API your markup is final.

require "net/http"
require "json"
class InvoicesController < ApplicationController
def show
# Render an existing ERB view to an HTML string.
html = render_to_string(template: "invoices/show", layout: false)
res = Net::HTTP.post(
URI("https://pdfgen.com/api/v1/generate"),
{ html: html, engine: "legacy", format: "A4" }.to_json,
"Authorization" => "Bearer #{Rails.application.credentials.pdfgen_key}",
"Content-Type" => "application/json"
)
send_data res.body, filename: "invoice.pdf", type: "application/pdf"
end
end

How it works

  1. 1

    Send your HTML + data

    POST an HTML template (with optional Handlebars tokens) and a data object to the API.

  2. 2

    We render the PDF

    PDFgen renders it server-side — no headless browser for you to run or scale.

  3. 3

    Get the PDF back

    Receive the PDF binary (or a hosted link with export_type:"url") and serve it from Ruby on Rails.

Good to know

  • Net::HTTP ships with Ruby — no extra gem to add.
  • Store the key in Rails credentials or an environment variable, not in source.
  • Render an ERB template to a string and pass it as the html field if you prefer building markup in Rails.

Frequently asked questions

Do I still need Wicked PDF or wkhtmltopdf in Rails?
No. PDFgen renders for you, so a single Net::HTTP call replaces the Wicked PDF + wkhtmltopdf stack.
How do I send the PDF to the user?
Use send_data with type: "application/pdf" and a filename, as shown — Rails streams it as a download.
Can I use a gem like Faraday or HTTParty instead?
Yes — any HTTP client works. The example uses Net::HTTP only to avoid adding a dependency.

Generate PDFs in other languages

Your first PDF from Ruby on Rails in five minutes

Grab an API key, paste the snippet above, and render a PDF — no headless browsers to babysit. Build a reusable template with AI when you're ready.