PDFify Ruby Gem
Ruby client library for the PDFify HTML-to-PDF API. A DocRaptor alternative at 50% cheaper pricing.
Installation
Add this line to your application's Gemfile:
gem 'pdfify-client'And then execute:
bundle installOr install it yourself as:
gem install pdfify-clientQuick Start
require '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)Usage
Basic Conversion
# Simple HTML conversion
pdf = PDFify.convert(html: "<h1>Hello!</h1>")
File.binwrite("hello.pdf", pdf)Using Client Instance
# Create a client instance
client = PDFify::Client.new
# Convert HTML
pdf = client.convert(
html: "<html><body>Content here</body></html>"
)Test/Sandbox Mode
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
)Advanced Options
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
)Error Handling
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}"
endAvailable Error Classes
-
PDFify::APIError- Base error class -
PDFify::NetworkError- Connection/network errors -
PDFify::AuthenticationError- Invalid API key -
PDFify::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
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
endEnvironment Variables
You can also set the API key via environment variable:
PDFify.configure do |config|
config.api_key = ENV['PDFIFY_API_KEY']
endIntegration with Rails
Initializer
Create 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']
endController Example
class 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
endService Object Example
class 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).generateReplacing DocRaptor
If you're migrating from DocRaptor, here's a comparison:
DocRaptor:
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:
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.
API Reference
PDFify.configure { |config| ... }
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)
PDFify.convert(html:, **options)
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 missing -
PDFify::ConfigurationErrorif API key is not configured -
PDFify::APIErrorand subclasses for API errors
PDFify::Client.new(config = nil)
Create a new client instance.
Parameters:
-
config(PDFify::Configuration) - Custom configuration (optional, uses global config if nil)
Returns:
- PDFify::Client instance
License
The gem is available as open source under the terms of the MIT License.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/almokhtarbr/pdfify-client.
Support
- GitHub Issues: https://github.com/almokhtarbr/pdfify-client/issues