Official Ruby SDK for TemplateFox - Generate PDFs from HTML templates via API.
Add to your Gemfile:
gem 'templatefox'Then run:
bundle installOr install directly:
gem install templatefoxrequire 'templatefox'
# Initialize the client
TemplateFox.configure do |config|
config.api_key['ApiKeyAuth'] = 'your-api-key'
end
api = TemplateFox::PDFApi.new
# Generate a PDF
request = TemplateFox::CreatePdfRequest.new(
template_id: 'YOUR_TEMPLATE_ID',
data: {
name: 'John Doe',
invoice_number: 'INV-001',
total_amount: 150.00
}
)
begin
response = api.create_pdf(request)
puts "PDF URL: #{response.url}"
puts "Credits remaining: #{response.credits_remaining}"
rescue TemplateFox::ApiError => e
puts "Error: #{e.message}"
end- Template-based PDF generation - Create templates with dynamic variables, generate PDFs with your data
- Multiple export options - Get a signed URL (default) or raw binary PDF
- S3 integration - Upload generated PDFs directly to your own S3-compatible storage
- Ruby 2.7+ - Compatible with Ruby 2.7 and above
request = TemplateFox::CreatePdfRequest.new(
template_id: 'TEMPLATE_ID',
data: { name: 'John Doe' },
export_type: 'url', # 'url' or 'binary'
expiration: 86400, # URL expiration in seconds
filename: 'invoice-001' # Custom filename
)
response = api.create_pdf(request)templates_api = TemplateFox::TemplatesApi.new
# List all templates
templates = templates_api.list_templates
templates.templates.each do |template|
puts "#{template.id}: #{template.name}"
end
# Get template fields
fields = templates_api.get_template_fields('TEMPLATE_ID')
fields.each do |field|
puts "#{field.key}: #{field.type} (required: #{field.required})"
endaccount_api = TemplateFox::AccountApi.new
# Get account info
account = account_api.get_account
puts "Credits: #{account.credits}"
puts "Email: #{account.email}"
# List transactions
transactions = account_api.list_transactions(limit: 100, offset: 0)
transactions.transactions.each do |tx|
puts "#{tx.transaction_type}: #{tx.credits} credits"
endintegrations_api = TemplateFox::IntegrationsApi.new
# Save S3 configuration
s3_config = TemplateFox::S3ConfigRequest.new(
endpoint_url: 'https://s3.amazonaws.com',
access_key_id: 'AKIAIOSFODNN7EXAMPLE',
secret_access_key: 'your-secret-key',
bucket_name: 'my-pdf-bucket',
default_prefix: 'generated/pdfs/'
)
integrations_api.save_s3_config(s3_config)
# Test connection
test = integrations_api.test_s3_connection
puts "Connection: #{test.success ? 'OK' : 'Failed'}"TemplateFox.configure do |config|
config.host = 'https://api.pdftemplateapi.com' # Default API URL
config.api_key['ApiKeyAuth'] = ENV['TEMPLATEFOX_API_KEY']
# Optional: Enable debugging
config.debugging = true
endbegin
response = api.create_pdf(request)
rescue TemplateFox::ApiError => e
case e.code
when 402
puts 'Insufficient credits'
when 403
puts 'Access denied - check your API key'
when 404
puts 'Template not found'
else
puts "Error: #{e.message}"
end
end# config/initializers/templatefox.rb
TemplateFox.configure do |config|
config.api_key['ApiKeyAuth'] = Rails.application.credentials.templatefox_api_key
end
# app/services/pdf_generator.rb
class PdfGenerator
def initialize
@api = TemplateFox::PDFApi.new
end
def generate_invoice(invoice)
request = TemplateFox::CreatePdfRequest.new(
template_id: ENV['INVOICE_TEMPLATE_ID'],
data: invoice.to_template_data
)
@api.create_pdf(request)
end
end- Email: support@pdftemplateapi.com
- Issues: GitHub Issues
MIT License - see LICENSE for details.