Class: Pdfx::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfx/client.rb

Overview

Main client class for interacting with the pdfx.fr API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, base_url: nil) ⇒ Client

Initialize a new Pdfx client

Parameters:

  • api_key (String) (defaults to: nil) —

    Your pdfx.fr API key (optional if configured globally)

  • base_url (String) (defaults to: nil) —

    The base URL for the API (defaults to https://pdfx.fr)

Raises:

  • (ArgumentError)


16
17
18
19
20
21
# File 'lib/pdfx/client.rb', line 16

def initialize(api_key: nil, base_url: nil)
  @api_key = api_key || Pdfx.api_key
  @base_url = base_url || Pdfx.base_url

  raise ArgumentError, "API key is required" if @api_key.nil? || @api_key.empty?
end

Instance Attribute Details

#api_key ⇒ Object (readonly)

Returns the value of attribute api_key.



10
11
12
# File 'lib/pdfx/client.rb', line 10

def api_key
  @api_key
end

#base_url ⇒ Object (readonly)

Returns the value of attribute base_url.



10
11
12
# File 'lib/pdfx/client.rb', line 10

def base_url
  @base_url
end

Instance Method Details

#generate_pdf(template_uuid:, data: {}) ⇒ String

Generate a PDF using a template and data

Examples:

Generate a PDF

client = Pdfx::Client.new(api_key: "your-api-key")
pdf_data = client.generate_pdf(
  template_uuid: "ea99e8b9-...",
  data: { invoice: "001", customer: "Acme", amount: 1299 }
)
File.write("invoice.pdf", pdf_data)

Parameters:

  • template_uuid (String) —

    The UUID of the template to use

  • data (Hash) (defaults to: {}) —

    The data to interpolate into the template

Returns:

  • (String) —

    The PDF binary data

Raises:



40
41
42
43
44
45
46
47
48
49
# File 'lib/pdfx/client.rb', line 40

def generate_pdf(template_uuid:, data: {})
  raise ArgumentError, "template_uuid is required" if template_uuid.nil? || template_uuid.empty?

  payload = {
    template_uuid: template_uuid,
    data: data
  }

  make_request(payload)
end