RenderPDFs/Tutorials/Ruby
📘 Quickstart · 5 minutes

Generate PDFs in Ruby

Generate PDFs from HTML, Markdown, URLs or templates in Ruby — using Net::HTTP and the RenderPDFs REST API.

1. Install

RenderPDFs uses a plain REST API — no SDK required. For Ruby, install the dependency below and grab your API key from renderpdfs.com/signup (free, 100 PDFs/month, no credit card).

Installbash
# Net::HTTP is built into the Ruby stdlib — no install needed
gem install json  # only if not already available

2. Convert HTML to PDF

The simplest case: send an HTML string, get back a PDF binary. Anything Chromium renders works — Flexbox, Grid, web fonts, SVG, JavaScript.

HTML → PDFruby
require "net/http"
require "json"
require "uri"

uri = URI("https://api.renderpdfs.com/v1/generate")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true

req = Net::HTTP::Post.new(uri)
req["X-API-Key"]    = ENV["RENDERPDFS_KEY"]
req["Content-Type"] = "application/json"
req.body = { html: "<h1>Hello PDF</h1><p>Generated from Ruby</p>" }.to_json

res = http.request(req)
File.binwrite("hello.pdf", res.body)

3. Convert a URL to PDF

Pass url instead of html and RenderPDFs fetches the page, waits for JS, and snapshots it. Useful for archiving dashboards, public pages, or invoices served from your app.

URL → PDFruby
req.body = {
  url: "https://example.com",
  options: { format: "A4", printBackground: true },
}.to_json

res = http.request(req)
File.binwrite("page.pdf", res.body)

4. Use a built-in template

Skip the design work. RenderPDFs ships six battle-tested templates — invoice, receipt, report, contract, certificate, offer_letter. Send JSON, get a styled PDF.

Built-in invoice templateruby
req.body = {
  template: "invoice",
  data: {
    company: "Acme Inc.",
    invoice_number: "INV-001",
    items: [
      { description: "Consulting", quantity: 10, unit_price: 120 },
    ],
  },
}.to_json

5. Custom paper, margins, headers

Control page format, orientation, margins, and running headers/footers via the options object. All standard Chromium PDF settings are supported.

Custom paper size & marginsruby
req.body = {
  html: "<h1>Report</h1>",
  options: {
    format: "Letter",
    landscape: false,
    margin: { top: "20mm", bottom: "20mm", left: "15mm", right: "15mm" },
    printBackground: true,
  },
}.to_json

6. Store the PDF and share a link

For emailable links or webhook payloads, set store: true in the body. The response becomes { url, expires_in } — the PDF is hosted on our CDN for 24 hours by default.

Store the PDF and get a URLruby
req.body = { html: "<h1>Shared report</h1>", store: true }.to_json
result = JSON.parse(http.request(req).body)
puts "Download at #{result['url']} (expires in #{result['expires_in']}s)"

7. Notes & gotchas

Authentication

Every request needs an X-API-Key header. Grab a free key at renderpdfs.com/signup — 100 PDFs/month, no credit card. Treat the key like a password: keep it server-side, never expose it in browser code.

Response format

By default the endpoint streams back the raw PDF binary (Content-Type: application/pdf). Set `store: true` in the body and the response becomes { url, expires_in } — useful for emailing links or attaching to webhooks.

Handling errors

Non-2xx responses return JSON: { error: string }. The most common cases are 401 (bad API key), 402 (over plan quota), and 422 (invalid HTML or URL). Always parse the error body before retrying.

Other languages

Generate your first PDF in 60 seconds

100 free PDFs per month. No credit card. HTML, Markdown, URLs, templates, MCP — all included.