Guides · Ruby · Net::HTTP

Generate a fillable invoice PDF from a web page in Ruby with Net::HTTP

The usual invoice-PDF pipeline is a template engine, a PDF library, and an afternoon of coordinate arithmetic. If your app already renders the invoice as an HTML page with input fields (PO number, notes, approver name — whatever the recipient fills in), you can skip all of it: render that page with pdf_forms and the returned invoice keeps those inputs as real, typeable PDF fields.

Below is a complete, runnable Ruby program using Net::HTTP, no third-party dependency needed. It reads your API key from the SNAPDOK_KEY environment variable — free keys take about thirty seconds and need no card.

require "json"
require "net/http"
require "uri"

uri = URI("https://snapdok.io/v1/render")

res = Net::HTTP.start(uri.host, uri.port, use_ssl: true, read_timeout: 90) do |http|
  req = Net::HTTP::Post.new(uri)
  req["Authorization"] = "Bearer #{ENV.fetch("SNAPDOK_KEY")}"
  req["Content-Type"] = "application/json"
  req.body = JSON.generate({
    url: "https://your-app.com/invoices/1042/edit",
    format: "pdf",
    pdf_forms: true,
    pdf_form_only: true,
})
  http.request(req)
end

raise "snapdok error #{res.code}: #{res.body}" unless res.is_a?(Net::HTTPSuccess)

File.binwrite("invoice-1042.pdf", res.body)
puts "#{res["X-Form-Fields"]}  # fields the recipient can type into"

Two practical tips from rendering real invoice pages. First, pre-filled values carry over: render the edit view of the invoice with amounts and line items already populated, and the PDF arrives pre-filled, with only the recipient's fields left blank. Second, add "pdf_form_only": true when the invoice page lives inside your app's UI — it strips the sidebar and account chrome so the customer sees an invoice, not a screenshot of your dashboard. If extraction is unsure it falls back to the full page (check X-Form-Extracted), so the worst case is cosmetic, never a missing field.

Renders are cached for 24 hours (X-Cache: HIT responses are free), which suits invoices well: re-sending the same invoice email does not burn quota. When you regenerate after an amount changes, the body differs — new URL or changed page content means a fresh render, so you never serve a stale total from cache unless the URL and page are byte-identical.

Notes for Net::HTTP

Stdlib only — no gems. The traps Net::HTTP is famous for, all handled in the sample: TLS is not automatic (set use_ssl: true or you will POST in the clear to port 80 and get a redirect); the default read_timeout of 60 s is close enough to real render times that raising it is wise; and the response body is a binary-ready String, but write it with File.binwrite — plain File.write on Windows would mangle line-ending bytes in the PDF. Check is_a?(Net::HTTPSuccess): Net::HTTP never raises on HTTP error statuses.

Response headers worth reading

HeaderMeaning
X-Form-FieldsTypeable fields in the returned invoice.
X-Form-ExtractedWhether page chrome was stripped.
X-CacheHIT = served from the 24h cache, not charged.

Full parameter reference: the docs. Hard numbers on caps and timeouts: limits.

Related guides

Same task, other stacks

More with Ruby + Net::HTTP

In the wild