Inkspan › Guides › How to generate a PDF from HTML in Ruby on Rails

How to generate a PDF from HTML in Ruby on Rails

Call the Inkspan /v1/generate/html endpoint from Ruby on Rails to generate a PDF from HTML — no headless browser, font stack, or rendering infrastructure in your deploy. Keep your API key in the environment and POST.

send_data is the idiomatic way to return the PDF to a browser. Use Rails credentials or an environment variable for the key.

Ruby on Rails example

# app/controllers/pdf_controller.rb
require 'net/http'

def create
  uri = URI('https://api.inkspan.dev/v1/generate/html')
  res = Net::HTTP.post(uri, {"html": "<h1>Invoice</h1>"}.to_json, {
    'X-API-Key' => ENV.fetch('INKSPAN_KEY'),
    'Content-Type' => 'application/json',
  })

  raise res.body unless res.is_a?(Net::HTTPSuccess)
  send_data res.body, type: 'application/pdf', disposition: 'attachment'
end

Request parameters

NameTypeRequiredDescription
htmlstringyesThe HTML document or fragment to render.
filenamestringnoFilename in the response's Content-Disposition. Defaults to document.pdf.

Response

Returns a PDF as a binary stream with Content-Type: application/pdf and Content-Disposition: attachment; filename=document.pdf. Write the response body straight to disk — do not parse it as JSON.

Production notes

Wrap the call in a background job with Active Job if the document is large; a synchronous render blocks a Puma thread.

Errors

StatusMeaningFix
401Invalid or missing API keySend your key in the X-API-Key header.
402Monthly quota exceededThe response reports used/quota. Upgrade at inkspan.dev/#pricing.
422Missing or invalid parameterCheck required fields in the table above — the body names the offending field.

Failed requests do not count against your quota; only successful ones are metered.

Get a free API key →

Same operation in cURL · Python · Node.js · Other frameworks: Next.js · Django · Laravel · PHP · All guides