Class: DynamicPDFApi::Pdf

Inherits:
Endpoint show all
Defined in:
lib/ruby_client/Pdf.rb

Overview

Represents a pdf endpoint.

Instance Attribute Summary collapse

Attributes inherited from Endpoint

#_endpoint_name, #api_key, #base_url

Instance Method Summary collapse

Methods inherited from Endpoint

#init

Constructor Details

#initializePdf

Initializes a new instance of the Pdf class.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ruby_client/Pdf.rb', line 25

def initialize
  super
  @_endpoint_name = "pdf"
  @out_lines = OutlineList.new
  @instructions = PdfInstructions.new
  @author = nil
  @resources = {}
  @title = nil
  @subject = nil
  @creator = ""
  @producer = ""
  @tag = nil
  @keywords = nil
  @security = nil
  @flatten_all_form_fields = nil
  @retain_signature_form_fields = nil
  @inputs = []
  @templates = {}
  @fonts = {}
  @form_fields = []
end

Instance Attribute Details

#authorObject

Gets or sets the author.



90
91
92
# File 'lib/ruby_client/Pdf.rb', line 90

def author
  @author
end

#creatorObject

Gets or sets the creator.



105
106
107
# File 'lib/ruby_client/Pdf.rb', line 105

def creator
  @creator
end

#flatten_all_form_fieldsObject

Gets or sets the value indicating whether to flatten all form fields.



130
131
132
# File 'lib/ruby_client/Pdf.rb', line 130

def flatten_all_form_fields
  @flatten_all_form_fields
end

#fontsObject

Gets the fonts.



258
259
260
# File 'lib/ruby_client/Pdf.rb', line 258

def fonts
  @fonts
end

#form_fieldsObject

Gets the formFields.



263
264
265
# File 'lib/ruby_client/Pdf.rb', line 263

def form_fields
  @form_fields
end

#inputsObject

Gets the inputs.



244
245
246
# File 'lib/ruby_client/Pdf.rb', line 244

def inputs
  @inputs
end

#instructionsObject

Returns the value of attribute instructions.



80
81
82
# File 'lib/ruby_client/Pdf.rb', line 80

def instructions
  @instructions
end

#keywordsObject

Gets or sets the keywords.



120
121
122
# File 'lib/ruby_client/Pdf.rb', line 120

def keywords
  @keywords
end

#out_linesObject

Gets the outlines.



268
269
270
# File 'lib/ruby_client/Pdf.rb', line 268

def out_lines
  @out_lines
end

#producerObject

Gets or sets the PDF producer.



110
111
112
# File 'lib/ruby_client/Pdf.rb', line 110

def producer
  @producer
end

#resourcesObject

Gets or sets the collection of resource.



85
86
87
# File 'lib/ruby_client/Pdf.rb', line 85

def resources
  @resources
end

#retain_signature_form_fieldsObject

Gets or sets the value indicating whether to retain signature form field.



135
136
137
# File 'lib/ruby_client/Pdf.rb', line 135

def retain_signature_form_fields
  @retain_signature_form_fields
end

#securityObject

Gets or sets the security.



125
126
127
# File 'lib/ruby_client/Pdf.rb', line 125

def security
  @security
end

#subjectObject

Gets or sets the subject.



100
101
102
# File 'lib/ruby_client/Pdf.rb', line 100

def subject
  @subject
end

#tagObject

Gets or sets the Tag property.



115
116
117
# File 'lib/ruby_client/Pdf.rb', line 115

def tag
  @tag
end

#templatesObject

Gets the templates.



253
254
255
# File 'lib/ruby_client/Pdf.rb', line 253

def templates
  @templates
end

#titleObject

Gets or sets the title.



95
96
97
# File 'lib/ruby_client/Pdf.rb', line 95

def title
  @title
end

Instance Method Details

#add_additional_resource(resourcePath, resourceName = nil) ⇒ Object

Adds additional resource to the endpoint.

<param name=“resourcePath”>The resource file path. <param name=“resourceName”>The name of the resource.



52
53
54
55
56
57
58
59
# File 'lib/ruby_client/Pdf.rb', line 52

def add_additional_resource(resourcePath, resourceName = nil)
  if (resourceName == nil)
    resourceName = File.basename(resourcePath)
  end
  additional_resource = AdditionalResource.new(resourcePath, resourceName)
  @resources[resourceName] = additional_resource
  # @additionalResources << additional_resource
end

#add_additional_resource_with_resourcedata(resourceData, additionalResourceType, resourceName) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ruby_client/Pdf.rb', line 61

def add_additional_resource_with_resourcedata(resourceData, additionalResourceType, resourceName)
  type = ResourceType::PDF
  case (additionalResourceType)

  when AdditionalResourceType::FONT
    type = ResourceType::FONT
  when AdditionalResourceType::IMAGE
    type = ResourceType::IMAGE
  when AdditionalResourceType::PDF
    type = ResourceType::PDF
  when AdditionalResourceType::HTML
    type = ResourceType::HTML
  else
    raise 'This type of resource not allowed'
  end
  additional_resource = AdditionalResource.new(resourceData, resourceName, type)
  @additionalResources << additional_resource
end

#add_dlex(dlex, layout) ⇒ Object

Returns a DlexInput object containing the input pdf.

Parameters:

  • dlex (String)

    |[DlexResource] The resource path in cloud resource manager or the dlex resource of type DlexResource.

  • layout_data (String)

    |[Layout_dataResource] The layout data resource path in cloud resource manager or the layout data resource of type Layout_dataResource.

Returns:

  • DlexInput DlexInput object.



217
218
219
220
221
# File 'lib/ruby_client/Pdf.rb', line 217

def add_dlex(dlex, layout)
  input = DlexInput.new(dlex, layout)
  @inputs << input
  input
end

#add_excel(resource, size = nil, orientation = nil, margins = nil) ⇒ Object

Returns a ExcelInput object containing the input pdf.

Parameters:

  • resource (ExcelResource)

    The resource of type ExcelResource.

Returns:

  • ExcelInput ExcelInput object.



203
204
205
206
207
# File 'lib/ruby_client/Pdf.rb', line 203

def add_excel(resource, size = nil, orientation = nil, margins = nil)
  input = ExcelInput.new(resource, size, orientation, margins)
  @inputs << input
  input
end

#add_html(html, basepath = nil, size = nil, orientation = nil, margins = nil) ⇒ Object

Returns an HtmlInput object containing the input pdf.

Parameters:

  • resource (String)

    |[HtmlResource] The resource of type HtmlResource.

Returns:

  • HtmlInput HtmlInput object.



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/ruby_client/Pdf.rb', line 171

def add_html(html, basepath = nil, size = nil, orientation = nil, margins = nil)
  input = nil
  if file.is_a?(String)
    input = HtmlInput.new(HtmlResource.new(html), basepath, size, orientation, margins)
  else
    input = HtmlInput.new(resource, basepath, size, orientation, margins)
  end

  @inputs << input
  input
end

#add_image(resource) ⇒ Object

Returns an ImageInput object containing the input pdf.

Parameters:

  • resource (String)

    |[ImageResource] The resource path in cloud resource manager or the resource of type ImageResource.

Returns:

  • ImageInput ImageInput object.



158
159
160
161
162
# File 'lib/ruby_client/Pdf.rb', line 158

def add_image(resource)
  input = ImageInput.new(resource)
  @inputs << input
  input
end

#add_page(page_width = nil, page_height = nil) ⇒ Object

Returns a page_input object containing the input pdf.

Parameters:

  • page_width (float) (defaults to: nil)

    The width of the page.

  • page_height (float) (defaults to: nil)

    The height of the page.

Returns:

  • page_input page_input object.



231
232
233
234
235
236
237
238
239
# File 'lib/ruby_client/Pdf.rb', line 231

def add_page(page_width = nil, page_height = nil)
  input = if !page_width.nil? && !page_height.nil?
      PageInput.new(page_width, page_height)
    else
      PageInput.new
    end
  @inputs << input
  input
end

#add_pdf(resource, options = nil) ⇒ Object

Returns a PdfInput object containing the input pdf.

Parameters:

  • resource (String)

    |[PdfResource] The resource path in cloud resource manager or the resource of type PdfResource.

  • options (MergeOptions) (defaults to: nil)

    The merge options for the pdf.

Returns:

  • PdfInput PdfInput object.



145
146
147
148
149
# File 'lib/ruby_client/Pdf.rb', line 145

def add_pdf(resource, options = nil)
  input = PdfInput.new(resource, options)
  @inputs << input
  input
end

#add_word(resource, size = nil, orientation = nil, margins = nil) ⇒ Object

Returns a WordInput object containing the input pdf.

Parameters:

  • resource (WordResource)

    The resource of type WordResource.

Returns:

  • WordInput WordInput object.



190
191
192
193
194
# File 'lib/ruby_client/Pdf.rb', line 190

def add_word(resource, size = nil, orientation = nil, margins = nil)
  input = WordInput.new(resource, size, orientation, margins)
  @inputs << input
  input
end

#get_instructions_json(indented = false) ⇒ Object



382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/ruby_client/Pdf.rb', line 382

def get_instructions_json(indented = false)
  @tembInstructions = PdfInstructions.new
  @tembInstructions._author = @author
  @tembInstructions._title = @title
  @tembInstructions._subject = @subject
  @tembInstructions._creator = @creator
  @tembInstructions._producer = @producer
  @tembInstructions._tag = @tag
  @tembInstructions._keywords = @keywords
  @tembInstructions._security = @security
  @tembInstructions._flatten_all_form_fields = @flatten_all_form_fields
  @tembInstructions._retain_signature_form_fields = @retain_signature_form_fields
  @tembInstructions._inputs = @inputs
  @tembInstructions._templates = @templates
  @tembInstructions._fonts = @fonts
  @tembInstructions._form_fields = @form_fields
  @tembInstructions._out_lines = @out_lines
  # @tembInstructions.form_fields = @form_fields

  @tembInstructions._inputs.each do |input|
    if input._type == InputType::PAGE
      page_input = input
      page_input.elements.each do |element|
        @resources[element._resource.resource_name] = element._resource unless element._resource.nil?
        if !element._text_font.nil?
          font_serialized_array = element._text_font
          @tembInstructions._fonts[element._text_font._name] = font_serialized_array
        end
      end
    end

    input._resources.each do |resource|
      @resources[resource.resource_name] = resource unless resource.nil?
    end

    if !input.get_template.nil?
      template = input.get_template
      @tembInstructions._templates[template.id] = template
      if !input.get_template.elements.nil? && !input.get_template.elements.empty?
        input.get_template.elements.each do |element|
          @resources[element._resource.resource_name] = element._resource unless element._resource.nil?
          if !element._text_font.nil?
            font_serialized_array = element._text_font

            @tembInstructions._fonts[element._text_font._name] = font_serialized_array
          end
        end
      end
    end
  end
  #data_string = @instructions.to_json
  @tembInstructions.to_json
end

#processObject

Process to create pdf.

Returns:

  • PdfResponse Returns collection of PdfResponse.



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/ruby_client/Pdf.rb', line 275

def process
  @instructions._author = @author
  @instructions._title = @title
  @instructions._subject = @subject
  @instructions._creator = @creator
  @instructions._producer = @producer
  @instructions._tag = @tag
  @instructions._keywords = @keywords
  @instructions._security = @security
  @instructions._flatten_all_form_fields = @flatten_all_form_fields
  @instructions._retain_signature_form_fields = @retain_signature_form_fields
  @instructions._inputs = @inputs

  @instructions._templates = @templates
  @instructions._fonts = @fonts
  @instructions._form_fields = @form_fields
  @instructions._out_lines = @out_lines
  # @Instructions.form_fields = @form_fields

  @instructions._inputs.each do |input|
    if input._type == InputType::PAGE
      page_input = input
      page_input.elements.each do |element|
        @resources[element._resource.resource_name] = element._resource unless element._resource.nil?
        if !element._text_font.nil?
          font_serialized_array = element._text_font
          @instructions._fonts[element._text_font._name] = font_serialized_array
        end
      end
    end

    input._resources.each do |resource|
      @resources[resource.resource_name] = resource unless resource.nil?
    end

    if !input.get_template.nil?
      template = input.get_template
      @instructions._templates[template.id] = template
      if !input.get_template.elements.nil? && !input.get_template.elements.empty?
        input.get_template.elements.each do |element|
          @resources[element._resource.resource_name] = element._resource unless element._resource.nil?
          if !element._text_font.nil?
            font_serialized_array = element._text_font

            @instructions._fonts[element._text_font._name] = font_serialized_array
          end
        end
      end
    end
  end

  data_string = @instructions.to_json

  header = {
    'authorization': "Bearer #{@api_key}",
    'Expect': "100-continue",
  }
  uri = URI.parse("#{@base_url}/v1.0/#{@_endpoint_name}")
  request = Net::HTTP::Post.new(uri.request_uri, header)

  req_options = {
    use_ssl: uri.scheme == "https",
    verify_mode: OpenSSL::SSL::VERIFY_NONE,

  }

  resource_array = []
  resource_array << ["Instructions", data_string,
                     { content_type: "application/octet-stream", filename: "Instructions.json" }]
  @resources.each_value do |field|
    data = if !field._file_path.nil?
        File.binread(field._file_path)
      else
        field.data
      end

    resource_array << ["Resource", data,
                       { content_type: "application/octet-stream", filename: field.resource_name }]
  end
  request.set_form(resource_array, "multipart/form-data")

  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    http.request(request)
  end

  ret_object = PdfResponse.new
  ret_object.is_successful = false
  ret_object.status_code = response.code
  out_data = response.body

  if !ret_object.nil? && ret_object.status_code == "200" && out_data[0, 4].eql?("%PDF") == true
    ret_object = PdfResponse.new(out_data)
    ret_object.is_successful = true
  else
    out_data_json = JSON.parse(out_data)
    ret_object.error_json = out_data
    ret_object.error_message = if !out_data_json["message"].nil?
        out_data_json["message"]
      else
        "status_code : #{Net::HTTPResponse::CODE_TO_OBJ[ret_object.status_code]}"
      end
    ret_object.error_id = out_data_json["id"]
  end

  ret_object
end