Generate a PDF in Ruby
The simplest way to generate a PDF in Ruby is to call the Docweave API with Net::HTTP: POST your HTML to /api/v1/pdf and write the returned bytes to a file — no headless browser to install or run yourself.
Ruby HTML to PDF, using Net::HTTP
Send an HTML string in the request body and write the PDF response to disk. This uses only the Ruby standard library — no gems to add:
require "net/http"
require "json"
require "uri"
api_key = "dw_live_your_key"
html = <<~HTML
<h1>Invoice INV-042</h1>
<p>Billed to Acme Inc.</p>
<p>Total due: $1,280.00</p>
HTML
uri = URI("https://docweave.dev/api/v1/pdf")
request = Net::HTTP::Post.new(uri)
request["Authorization"] = "Bearer #{api_key}"
request["Content-Type"] = "application/json"
request["Accept"] = "application/pdf"
request.body = JSON.generate(
source: { type: "html", html: html },
options: { format: "A4" },
idempotencyKey: "inv-042"
)
response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
http.request(request)
end
unless response.is_a?(Net::HTTPSuccess)
raise "PDF generation failed: #{response.code} #{response.body}"
end
File.binwrite("invoice.pdf", response.body)
puts "Wrote invoice.pdf"…or from a Ruby AI agent, via MCP
If your agent runs in Ruby, or your workflow calls tools over MCP from any language, the Docweave MCP server exposes a generate_pdf tool — the same underlying render, no HTTP boilerplate to write:
# From a Ruby-based AI agent, over MCP — no HTTP plumbing:
generate_pdf({
"source" => {
"type" => "html",
"html" => "<h1>Invoice INV-042</h1><p>Total due: $1,280.00</p>"
},
"outputPath" => "/tmp/invoice-042.pdf"
})
# Any MCP client (Claude, an agent framework, a custom Ruby MCP client)
# can call this tool once the server is registered — start it with:
# npx @docweave/mcpFAQ
What's the simplest way to generate a PDF in Ruby?
Call a PDF-generation API with Net::HTTP from the Ruby standard library: POST your HTML to /api/v1/pdf with an Authorization: Bearer header, then write the response body to a file. There's no headless browser to install, configure, or keep patched in your own Ruby environment.
Do I need wicked_pdf, Grover, or Prawn to generate PDFs in Ruby?
No. Those gems either wrap a local wkhtmltopdf/Chrome binary or build PDFs programmatically, and you have to manage that dependency yourself. Calling the Docweave API from Ruby offloads the rendering to the service — your code only needs an HTTP client, which Net::HTTP already gives you.
Can I generate a PDF from a URL instead of raw HTML in Ruby?
Yes. Set source to { "type" => "url", "url" => "https://example.com" } in the same JSON payload instead of the "html" key, and POST it the same way with Net::HTTP.
How do I avoid double-billing if my Ruby script retries a failed request?
Pass an idempotencyKey (for example an invoice number or job ID) in the request body. A retry with the same key returns the original PDF instead of rendering — and billing — again. Rendering is billed per document, not per page.
Ready to generate PDFs from Ruby in production?
Get an API key