Use Ruby standard library HTTP clients to generate PDFs from Rails, Sidekiq, Rake tasks, or scripts.
Build a JSON payload in Ruby, authenticate with X-API-Key, and write the returned PDF bytes to a file.
The API accepts a JSON request body. Send either a public URL or inline HTML in the required html parameter. For normal synchronous conversions, the response body is the generated PDF and should be saved as binary data.
Replace <your-api-key> or the environment variable with your html2pdf.app API key, then run the example from a trusted backend environment.
Use net/http to post JSON and write the successful response body to document.pdf.
require 'json'
require 'net/http'
require 'uri'
uri = URI('https://api.html2pdf.app/v1/generate')
request = Net::HTTP::Post.new(uri)
request['Content-Type'] = 'application/json'
request['X-API-Key'] = ENV.fetch('HTML2PDF_API_KEY')
request.body = { html: 'https://www.example.com' }.to_json
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
http.request(request)
end
unless response.is_a?(Net::HTTPSuccess)
raise "PDF generation failed: #{response.code}"
end
File.binwrite('document.pdf', response.body)These examples cover the next common production flows: generating from inline HTML with options and queuing a callback job.
Pass layout options with the HTML content when your Ruby app renders the PDF body.
require 'json'
require 'net/http'
require 'uri'
payload = {
html: '<h1>Receipt</h1><p>Paid in full.</p>',
format: 'A4',
marginTop: 40,
marginRight: 32,
marginBottom: 40,
marginLeft: 32,
filename: 'receipt.pdf'
}
uri = URI('https://api.html2pdf.app/v1/generate')
request = Net::HTTP::Post.new(uri)
request['Content-Type'] = 'application/json'
request['X-API-Key'] = ENV.fetch('HTML2PDF_API_KEY')
request.body = payload.to_json
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
http.request(request)
end
raise response.body unless response.is_a?(Net::HTTPSuccess)
File.binwrite('receipt.pdf', response.body)Use this shape from a job worker when a webhook should receive the completed PDF.
require 'json'
require 'net/http'
require 'uri'
payload = {
html: 'https://www.example.com/report',
callBackUrl: 'https://your-app.com/webhooks/html2pdf',
state: 'report-1042'
}
uri = URI('https://api.html2pdf.app/v1/generate')
request = Net::HTTP::Post.new(uri)
request['Content-Type'] = 'application/json'
request['X-API-Key'] = ENV.fetch('HTML2PDF_API_KEY')
request.body = payload.to_json
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
http.request(request)
end
raise response.body unless response.is_a?(Net::HTTPSuccess)Most integrations start with html and then add options as the document design becomes more specific.
html is required and accepts a public URL or raw HTML.format, landscape, width, and height control page size.marginTop, marginRight, marginBottom, and marginLeft control whitespace around the rendered page.media selects screen or print CSS rendering.callBackUrl switches the request to asynchronous generation and sends the completed PDF to your webhook.See the full API parameter reference for every supported field.
If the generated document is blank or missing styles, first confirm that the source URL is public and that required CSS, fonts, and images are reachable by the rendering service.