Guides · Ruby · Net::HTTP
Take a retina-quality (2x) screenshot in Ruby with Net::HTTP
A default screenshot renders at 1 CSS pixel = 1 image pixel, which looks soft on any modern display and falls apart the moment a designer zooms in. "device_scale": 2 renders the page the way a MacBook does — double density — so text is crisp in decks, docs and social cards.
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/report/42",
format: "png",
width: 1280,
device_scale: 2,
})
http.request(req)
end
raise "snapdok error #{res.code}: #{res.body}" unless res.is_a?(Net::HTTPSuccess)
File.binwrite("retina.png", res.body)
The arithmetic to keep in mind: pixel dimensions are width × device_scale, so 1280 wide at scale 2 produces a 2560-px-wide image with roughly 4× the bytes of the 1× version. For thumbnails that will be displayed small, 1× is cheaper and looks identical after downscaling; spend the scale factor where humans will look closely.
If you arrive from Urlbox or ApiFlash muscle memory: device_scale_factor is accepted as an alias and normalised to the same thing — both spellings even share one cache entry, so mixed codebases do not double-render. JPEG ("format": "jpeg") at scale 2 is often the sweet spot for photography-heavy pages: retina sharpness, a fraction of the PNG size.
Same endpoint, one more trick: if the page you are rendering has a form on it, adding "pdf_forms": true to a PDF render brings it back with real, fillable AcroForm fields — a PDF people can type into, not a picture of one. How that works.
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
| Header | Meaning |
|---|---|
X-Cache | Both device_scale spellings hit the same cache entry. |
X-RateLimit-Remaining | Requests left in the current second. |
Full parameter reference: the docs. Hard numbers on caps and timeouts: limits.
Related guides
Same task, other stacks
- Take a retina-quality (2x) screenshot in Python with requests
- Take a retina-quality (2x) screenshot in Python with httpx
- Take a retina-quality (2x) screenshot in Node.js with built-in fetch
- Take a retina-quality (2x) screenshot in Node.js with axios
- Take a retina-quality (2x) screenshot in PHP with cURL extension
More with Ruby + Net::HTTP
- Convert an HTML form to a fillable PDF
- Get a fillable PDF of just the form — no nav, no footer
- Generate a fillable invoice PDF from a web page
- Turn an online registration form into a fillable PDF
- Wait for a JavaScript-rendered form, then make it fillable
- Verify a fillable PDF render without opening the file
- Convert a URL to PDF
- Take a full-page screenshot
- Capture very tall pages as numbered tiles
- Take a JPEG screenshot at a custom viewport size
- Use the 24-hour render cache to cut costs
- Take a screenshot of a URL
- Capture a JavaScript-heavy page after it settles
- Wait for a specific element before capturing
- Add page numbers and headers to a PDF