Pdfx Ruby Gem
A Ruby wrapper for the pdfx.fr API - the fastest, most eco-friendly PDF generation service for developers.
Installation
Add this line to your application's Gemfile:
gem 'pdfx'And then execute:
bundle installOr install it yourself as:
gem install pdfxConfiguration
Configure the gem with your API key:
require 'pdfx'
Pdfx.configure do |config|
config.api_key = "your-api-key-here"
endYou can also set a custom base URL (useful for testing):
Pdfx.configure do |config|
config.api_key = "your-api-key-here"
config.base_url = "https://custom-pdfx-instance.com"
endUsage
Basic Usage
require 'pdfx'
# Configure globally
Pdfx.configure do |config|
config.api_key = "your-api-key"
end
# Create a client
client = Pdfx::Client.new
# Generate a PDF
pdf_data = client.generate_pdf(
template_uuid: "ea99e8b9-1234-5678-90ab-cdef12345678",
data: {
invoice: "001",
customer: "Acme Corp",
amount: 1299
}
)
# Save to file
File.write("invoice.pdf", pdf_data)Using Client-Specific API Key
You can override the global API key for specific clients:
client = Pdfx::Client.new(api_key: "different-api-key")
pdf_data = client.generate_pdf(
template_uuid: "ea99e8b9-1234-5678-90ab-cdef12345678",
data: { invoice: "002", customer: "Beta Inc", amount: 2500 }
)Error Handling
The gem provides specific error classes for different scenarios:
begin
pdf_data = client.generate_pdf(
template_uuid: "ea99e8b9-1234-5678-90ab-cdef12345678",
data: { invoice: "003" }
)
File.write("output.pdf", pdf_data)
rescue Pdfx::AuthenticationError => e
puts "Invalid API key: #{e.message}"
rescue Pdfx::RequestError => e
puts "Bad request: #{e.message}"
rescue Pdfx::ServerError => e
puts "Server error: #{e.message}"
rescue Pdfx::NetworkError => e
puts "Network error: #{e.message}"
rescue Pdfx::Error => e
puts "General error: #{e.message}"
endAPI Reference
Pdfx.configure
Configure the gem globally.
Parameters:
-
api_key(String): Your pdfx.fr API key -
base_url(String): Base URL for the API (default: "https://pdfx.fr")
Pdfx::Client.new
Create a new client instance.
Parameters:
-
api_key(String, optional): Override the global API key -
base_url(String, optional): Override the global base URL
client.generate_pdf
Generate a PDF using a template.
Parameters:
-
template_uuid(String, required): The UUID of your template -
data(Hash, optional): Data to interpolate into the template
Returns: String (PDF binary data)
Raises:
-
Pdfx::AuthenticationError: Invalid API key -
Pdfx::RequestError: Invalid request parameters -
Pdfx::ServerError: Server error (5xx) -
Pdfx::NetworkError: Network connectivity issues
Error Classes
-
Pdfx::Error- Base error class -
Pdfx::AuthenticationError- Invalid API key (401) -
Pdfx::RequestError- Bad request (400) -
Pdfx::ServerError- Server error (5xx) -
Pdfx::NetworkError- Network connectivity issues
Development
After checking out the repo, run bundle install to install dependencies.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/solisoft/pdfx-gem.
License
The gem is available as open source under the terms of the MIT License.
About pdfx.fr
pdfx.fr is a blazing-fast PDF generation API that:
- Generates PDFs in sub-100ms
- Supports Factur-X/ZUGFeRD e-invoicing
- Stores zero data (privacy by design)
- Runs on 100% renewable energy
- Offers 200 free PDFs per month
Visit pdfx.fr to create your account and get your API key.