Ruby client library for the PDFify HTML-to-PDF API. A DocRaptor alternative at 50% cheaper pricing.
Add this line to your application's Gemfile:
gem 'pdfify-client'And then execute:
bundle installOr install it yourself as:
gem install pdfify-clientrequire 'pdfify'
# Configure with your API key
PDFify.configure do |config|
config.api_key = "pfy_live_xxxxxxxxxx"
config.base_url = "https://api.pdfify.example.com" # optional
config.timeout = 60 # optional, default is 60 seconds
end
# Convert HTML to PDF
pdf = PDFify.convert(
html: "<html><body><h1>Hello World!</h1></body></html>"
)
# Save to file
File.binwrite("output.pdf", pdf)# Simple HTML conversion
pdf = PDFify.convert(html: "<h1>Hello!</h1>")
File.binwrite("hello.pdf", pdf)# Create a client instance
client = PDFify::Client.new
# Convert HTML
pdf = client.convert(
html: "<html><body>Content here</body></html>"
)Use test: true or sandbox: true to generate PDFs without counting against your quota:
pdf = PDFify.convert(
html: "<h1>Test</h1>",
test: true # doesn't count against quota
)pdf = PDFify.convert(
html: "<h1>Advanced</h1>",
profile: "docraptor", # CSS profile compatibility
css: "body { color: red; }", # Additional CSS
auto_compat: true, # Auto-detect template engine
template_engine: "pdfshift" # Specify engine explicitly
)begin
pdf = PDFify.convert(html: "<h1>Test</h1>")
File.binwrite("output.pdf", pdf)
rescue PDFify::QuotaExceededError => e
puts "Quota exceeded: #{e.message}"
rescue PDFify::ValidationError => e
puts "Invalid input: #{e.message}"
rescue PDFify::AuthenticationError => e
puts "Authentication failed: #{e.message}"
rescue PDFify::APIError => e
puts "API error: #{e.message}"
endPDFify::APIError- Base error classPDFify::NetworkError- Connection/network errorsPDFify::AuthenticationError- Invalid API keyPDFify::ValidationError- Invalid parameters (400)PDFify::QuotaExceededError- Monthly quota exceeded (403)PDFify::ContentTooLargeError- HTML content too large (413)PDFify::ServerError- Server-side errors (500)PDFify::ConfigurationError- Missing/invalid configuration
PDFify.configure do |config|
# Required: Your PDFify API key
config.api_key = "pfy_live_xxxxxxxxxx"
# Optional: API base URL (default: https://pdfify.example.com)
config.base_url = "https://api.pdfify.example.com"
# Optional: Request timeout in seconds (default: 60)
config.timeout = 60
endYou can also set the API key via environment variable:
PDFify.configure do |config|
config.api_key = ENV['PDFIFY_API_KEY']
endCreate config/initializers/pdfify.rb:
PDFify.configure do |config|
config.api_key = Rails.application.credentials.pdfify_api_key
# Or use ENV: config.api_key = ENV['PDFIFY_API_KEY']
endclass ReportsController < ApplicationController
def download_pdf
html = render_to_string(template: "reports/invoice", layout: "pdf")
pdf = PDFify.convert(html: html)
send_data pdf,
type: "application/pdf",
disposition: "attachment",
filename: "invoice-#{@invoice.id}.pdf"
end
endclass InvoicePdfGenerator
def initialize(invoice)
@invoice = invoice
end
def generate
html = ApplicationController.render(
template: "invoices/show",
layout: "pdf",
assigns: { invoice: @invoice }
)
PDFify.convert(html: html)
rescue PDFify::APIError => e
Rails.logger.error "PDF generation failed: #{e.message}"
raise
end
end
# Usage:
pdf = InvoicePdfGenerator.new(@invoice).generateIf you're migrating from DocRaptor, here's a comparison:
DocRaptor.configure do |config|
config.username = "YOUR_API_KEY"
end
docraptor = DocRaptor::DocApi.new
pdf = docraptor.create_doc(
document_content: html,
document_type: "pdf",
test: true
)PDFify.configure do |config|
config.api_key = "YOUR_API_KEY"
end
pdf = PDFify.convert(
html: html,
test: true
)It's that simple! Just swap the gem and update your configuration.
Configure the global PDFify settings.
Parameters:
config.api_key(String) - Your PDFify API key (required)config.base_url(String) - API base URL (optional, default: https://pdfify.example.com)config.timeout(Integer) - Request timeout in seconds (optional, default: 60)
Convert HTML to PDF.
Parameters:
html(String) - HTML content to convert (required)test(Boolean) - Enable test/sandbox mode (optional)sandbox(Boolean) - Alias fortest(optional)profile(String) - CSS profile to use (optional)css(String) - Additional CSS to inject (optional)auto_compat(Boolean) - Enable auto-compatibility mode (optional)template_engine(String) - Specify template engine (optional)
Returns:
- (String) Binary PDF data
Raises:
ArgumentErrorif HTML is missingPDFify::ConfigurationErrorif API key is not configuredPDFify::APIErrorand subclasses for API errors
Create a new client instance.
Parameters:
config(PDFify::Configuration) - Custom configuration (optional, uses global config if nil)
Returns:
- PDFify::Client instance
The gem is available as open source under the terms of the MIT License.
Bug reports and pull requests are welcome on GitHub at https://github.com/almokhtarbr/pdfify-client.
- GitHub Issues: https://github.com/almokhtarbr/pdfify-client/issues