Class: AsposeHtml::HtmlApi

Inherits:
Object
  • Object
show all
Defined in:
lib/aspose_html_cloud/api/html_api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ HtmlApi

Returns a new instance of HtmlApi.



36
37
38
39
# File 'lib/aspose_html_cloud/api/html_api.rb', line 36

def initialize(args)
  @api_client = AsposeHtml::ApiClient.default(args)
  @storage_api = AsposeHtml::StorageApi.new args
end

Instance Attribute Details

#api_clientObject

Returns the value of attribute api_client.



34
35
36
# File 'lib/aspose_html_cloud/api/html_api.rb', line 34

def api_client
  @api_client
end

Instance Method Details

#convert(src, dst, src_in_local, dst_in_local, is_url, options = nil, storage_name = nil) ⇒ OperationResult

Converting a document to various formats

Parameters:

  • src

    Source file or URL.

  • dst

    Full path to a result file.

  • src_in_local

    Flag, true if source in the local file.

  • dst_in_local

    Flag, true if result in the local file.

  • is_url

    Flag, true if source is website.

  • options (Hash, nil) (defaults to: nil)

    the optional parameters

  • storage_name (defaults to: nil)

    Storage name. Default storage is nil.

Options Hash (options):

  • :width (Float)

    Resulting width. For images in pixels, for PDF, XPS, DOCX in inches.

  • :height (Float)

    Resulting height. For images in pixels, for PDF, XPS, DOCX in inches.

  • :left_margin (Float)

    Left resulting margin. For images in pixels, for PDF, XPS, DOCX in inches.

  • :right_margin (Float)

    Right resulting margin. For images in pixels, for PDF, XPS, DOCX in inches.

  • :top_margin (Float)

    Top resulting margin. For images in pixels, for PDF, XPS, DOCX in inches.

  • :bottom_margin (Float)

    Bottom resulting margin. For images in pixels, for PDF, XPS, DOCX in inches.

Returns:



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
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
# File 'lib/aspose_html_cloud/api/html_api.rb', line 239

def convert(src, dst, src_in_local, dst_in_local, is_url, options=nil, storage_name=nil)
  if src_in_local
    res = @storage_api.upload_file("/", src)
    if res.uploaded.length != 1 || res.errors.length != 0
      fail ApiError.new('Unable to upload file')
    end
    file_in_storage = res.uploaded[0]
  else
    file_in_storage = src
  end

  out_file = File.basename(dst)
  out_file = dst unless dst_in_local

  input_format = 'html'
  input_format = get_input_format(src) unless is_url

  output_format = File.extname(dst).strip.downcase[1..-1]

  if output_format == 'jpg'
    output_format = 'jpeg'
  elsif output_format == 'mht'
    output_format = 'mhtml'
  elsif output_format == 'tif'
    output_format = 'tiff'
  end

  local_var_path = "/html/conversion/{from}-{to}".sub('{' + 'from' + '}', input_format.to_s).sub('{' + 'to' + '}', output_format.to_s)

  post_body = {}
  post_body[:'inputPath'] = file_in_storage.to_s
  post_body[:'outputFile'] = out_file.to_s
  post_body[:'storageName'] = storage_name.to_s

  unless options.nil?
    post_body[:'options'] = {}
    post_body[:'options'][:'width'] = options[:'width'] unless options[:'width'].nil?
    post_body[:'options'][:'height'] = options[:'height'] unless options[:'height'].nil?
    post_body[:'options'][:'leftMargin'] = options[:'left_margin'] unless options[:'left_margin'].nil?
    post_body[:'options'][:'rightMargin'] = options[:'right_margin'] unless options[:'right_margin'].nil?
    post_body[:'options'][:'topMargin'] = options[:'top_margin'] unless options[:'top_margin'].nil?
    post_body[:'options'][:'bottomMargin'] = options[:'bottom_margin'] unless options[:'bottom_margin'].nil?
    post_body[:'options'][:'resolution'] = options[:'resolution'] unless options[:'resolution'].nil?
    post_body[:'options'][:'background'] = options[:'background'] unless options[:'background'].nil?
    post_body[:'options'][:'jpegquality'] = options[:'jpeg_quality'] unless options[:'jpeg_quality'].nil?
    post_body[:'options'][:'usegit'] = options[:'use_git'] unless options[:'use_git'].nil?
    post_body[:'options'][:'error_threshold'] = options[:'error_threshold'] unless options[:'error_threshold'].nil?
    post_body[:'options'][:'max_iterations'] = options[:'max_iterations'] unless options[:'max_iterations'].nil?
    post_body[:'options'][:'colors_limit'] = options[:'colors_limit'] unless options[:'colors_limit'].nil?
    post_body[:'options'][:'line_width'] = options[:'line_width'] unless options[:'line_width'].nil?
  end

  query_params = {}

  # header parameters

  header_params = {}
  # HTTP header 'Accept' (if needed)

  header_params['Accept'] = @api_client.select_header_accept(['application/json'])
  # HTTP header 'Content-Type'

  header_params['Content-Type'] = @api_client.select_header_content_type(['application/json'])

  # form parameters

  form_params = {}

  data, status_code, headers = @api_client.call_api(:POST, local_var_path,
                                                    :header_params => header_params,
                                                    :query_params => query_params,
                                                    :form_params => form_params,
                                                    :body => post_body,
                                                    :return_type => 'OperationResult')
  if @api_client.config.debug
    @api_client.config.logger.debug "API called: Create task\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
  end

  if status_code != 200
    fail ApiError.new('Conversion failed')
  end

  while true
    data, status_code, headers = get_status(data.id)
    fail ApiError.new('Conversion failed') if data.code != 200 or data.status == 'faulted' or data.status == 'canceled'
    break if data.status == 'completed'
    sleep 3
  end

  if dst_in_local
    out_folder = File.dirname(dst)
    d = @storage_api.download_file(data.file)
    out_name = out_folder + '/' + File.basename(data.file)
    File.rename(d, out_name)
    data.file = out_name
  end

  data
end

#convert_local_to_local(src, dst, options = nil) ⇒ OperationResult

Converting a document from the local file and putting the result in the local file

Parameters:

  • src

    Full path to source file.

  • dst

    Full path to a result file.

  • options (Hash, nil) (defaults to: nil)

    the optional parameters

Options Hash (options):

  • :width (Float)

    Resulting width. For images in pixels, for PDF, XPS, DOCX in inches.

  • :height (Float)

    Resulting height. For images in pixels, for PDF, XPS, DOCX in inches.

  • :left_margin (Float)

    Left resulting margin. For images in pixels, for PDF, XPS, DOCX in inches.

  • :right_margin (Float)

    Right resulting margin. For images in pixels, for PDF, XPS, DOCX in inches.

  • :top_margin (Float)

    Top resulting margin. For images in pixels, for PDF, XPS, DOCX in inches.

  • :bottom_margin (Float)

    Bottom resulting margin. For images in pixels, for PDF, XPS, DOCX in inches.

Returns:



54
55
56
# File 'lib/aspose_html_cloud/api/html_api.rb', line 54

def convert_local_to_local(src, dst, options = nil)
  convert(src, dst, true, true, false, options)
end

#convert_local_to_storage(src, dst, storage, options = nil) ⇒ OperationResult

Converting a document from the local file and putting the result in the storage

Parameters:

  • src

    Full path to source file.

  • dst

    Full path to a result file.

  • storage

    Storage name. Default storage is nil.

  • options (Hash, nil) (defaults to: nil)

    the optional parameters

Options Hash (options):

  • :width (Float)

    Resulting width. For images in pixels, for PDF, XPS, DOCX in inches.

  • :height (Float)

    Resulting height. For images in pixels, for PDF, XPS, DOCX in inches.

  • :left_margin (Float)

    Left resulting margin. For images in pixels, for PDF, XPS, DOCX in inches.

  • :right_margin (Float)

    Right resulting margin. For images in pixels, for PDF, XPS, DOCX in inches.

  • :top_margin (Float)

    Top resulting margin. For images in pixels, for PDF, XPS, DOCX in inches.

  • :bottom_margin (Float)

    Bottom resulting margin. For images in pixels, for PDF, XPS, DOCX in inches.

Returns:



71
72
73
# File 'lib/aspose_html_cloud/api/html_api.rb', line 71

def convert_local_to_storage(src, dst, storage, options=nil)
  convert(src, dst, true, false, false, options, storage)
end

#convert_storage_to_local(src, dst, storage, options = nil) ⇒ OperationResult

Converting a document from the storage and putting the result in the local file

Parameters:

  • src

    Full path to source file.

  • dst

    Full path to a result file.

  • storage

    Storage name. Default storage is nil.

  • options (Hash, nil) (defaults to: nil)

    the optional parameters

Options Hash (options):

  • :width (Float)

    Resulting width. For images in pixels, for PDF, XPS, DOCX in inches.

  • :height (Float)

    Resulting height. For images in pixels, for PDF, XPS, DOCX in inches.

  • :left_margin (Float)

    Left resulting margin. For images in pixels, for PDF, XPS, DOCX in inches.

  • :right_margin (Float)

    Right resulting margin. For images in pixels, for PDF, XPS, DOCX in inches.

  • :top_margin (Float)

    Top resulting margin. For images in pixels, for PDF, XPS, DOCX in inches.

  • :bottom_margin (Float)

    Bottom resulting margin. For images in pixels, for PDF, XPS, DOCX in inches.

Returns:



88
89
90
# File 'lib/aspose_html_cloud/api/html_api.rb', line 88

def convert_storage_to_local(src, dst, storage, options=nil)
  convert(src, dst, false, true, false, options, storage)
end

#convert_storage_to_storage(src, dst, storage, options = nil) ⇒ OperationResult

Converting a document from the storage and putting the result in the storage

Parameters:

  • src

    Full path to source file.

  • dst

    Full path to a result file.

  • storage

    Storage name. Default storage is nil.

  • options (Hash, nil) (defaults to: nil)

    the optional parameters

Options Hash (options):

  • :width (Float)

    Resulting width. For images in pixels, for PDF, XPS, DOCX in inches.

  • :height (Float)

    Resulting height. For images in pixels, for PDF, XPS, DOCX in inches.

  • :left_margin (Float)

    Left resulting margin. For images in pixels, for PDF, XPS, DOCX in inches.

  • :right_margin (Float)

    Right resulting margin. For images in pixels, for PDF, XPS, DOCX in inches.

  • :top_margin (Float)

    Top resulting margin. For images in pixels, for PDF, XPS, DOCX in inches.

  • :bottom_margin (Float)

    Bottom resulting margin. For images in pixels, for PDF, XPS, DOCX in inches.

Returns:



105
106
107
# File 'lib/aspose_html_cloud/api/html_api.rb', line 105

def convert_storage_to_storage(src, dst, storage, options=nil)
  convert(src, dst, false, false, false, options, storage)
end

#convert_url_to_local(src, dst, options = nil) ⇒ OperationResult

Converting the HTML page from URL and putting the result in the local file

Parameters:

  • src

    URI of the website.

  • dst

    Full path to a result file.

  • options (Hash, nil) (defaults to: nil)

    the optional parameters

Options Hash (options):

  • :width (Float)

    Resulting width. For images in pixels, for PDF, XPS, DOCX in inches.

  • :height (Float)

    Resulting height. For images in pixels, for PDF, XPS, DOCX in inches.

  • :left_margin (Float)

    Left resulting margin. For images in pixels, for PDF, XPS, DOCX in inches.

  • :right_margin (Float)

    Right resulting margin. For images in pixels, for PDF, XPS, DOCX in inches.

  • :top_margin (Float)

    Top resulting margin. For images in pixels, for PDF, XPS, DOCX in inches.

  • :bottom_margin (Float)

    Bottom resulting margin. For images in pixels, for PDF, XPS, DOCX in inches.

Returns:



121
122
123
# File 'lib/aspose_html_cloud/api/html_api.rb', line 121

def convert_url_to_local(src, dst, options=nil)
  convert(src, dst, false, true, true, options)
end

#convert_url_to_storage(src, dst, storage, options = nil) ⇒ OperationResult

Converting the HTML page from URL and putting the result in the storage

Parameters:

  • src

    URI of the website.

  • dst

    Full path to a result file.

  • storage

    Storage name. Default storage is nil.

  • options (Hash, nil) (defaults to: nil)

    the optional parameters

Options Hash (options):

  • :width (Float)

    Resulting width. For images in pixels, for PDF, XPS, DOCX in inches.

  • :height (Float)

    Resulting height. For images in pixels, for PDF, XPS, DOCX in inches.

  • :left_margin (Float)

    Left resulting margin. For images in pixels, for PDF, XPS, DOCX in inches.

  • :right_margin (Float)

    Right resulting margin. For images in pixels, for PDF, XPS, DOCX in inches.

  • :top_margin (Float)

    Top resulting margin. For images in pixels, for PDF, XPS, DOCX in inches.

  • :bottom_margin (Float)

    Bottom resulting margin. For images in pixels, for PDF, XPS, DOCX in inches.

Returns:



138
139
140
# File 'lib/aspose_html_cloud/api/html_api.rb', line 138

def convert_url_to_storage(src, dst, storage, options=nil)
  convert(src, dst, false, false, true, options, storage)
end

#get_input_format(path) ⇒ Object



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/aspose_html_cloud/api/html_api.rb', line 358

def get_input_format(path)
  ext = File.extname(path).downcase[1..-1]
  case ext
    when 'htm'
      return 'html'
    when 'mht'
      return 'mhtml'
    when 'xml'
      return 'xhtml'
    when 'jpg'
      return 'jpeg'
    when 'tif'
      return 'tiff'
    else
      ext
  end
end

#get_status(id) ⇒ Object



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/aspose_html_cloud/api/html_api.rb', line 335

def get_status(id)

  local_var_path = "/html/conversion/{id}".sub('{' + 'id' + '}', id.to_s)
  # header parameters

  header_params = {}
  # HTTP header 'Accept' (if needed)

  header_params['Accept'] = @api_client.select_header_accept(['application/json'])
  # HTTP header 'Content-Type'

  header_params['Content-Type'] = @api_client.select_header_content_type(['application/json'])

  query_params = {}
  form_params = {}
  post_body = nil

  data, status_code, headers = @api_client.call_api(:GET, local_var_path,
                                                    :header_params => header_params,
                                                    :query_params => query_params,
                                                    :form_params => form_params,
                                                    :body => post_body,
                                                    :return_type => 'OperationResult')

end

#vectorize(src, dst, src_in_local, dst_in_local, options = nil, storage_name = nil) ⇒ OperationResult

Vectorization an image from to SVG format

Parameters:

  • src

    Full path to source file(bmp, png, gif, tiff, jpeg).

  • dst

    Full path to a result file (svg).

  • src_in_local

    Flag, true if source in the local file.

  • dst_in_local

    Flag, true if result in the local file.

  • options (Hash, nil) (defaults to: nil)

    the optional parameters

  • storage_name (defaults to: nil)

    Storage name. Default storage is nil.

Options Hash (options):

  • :error_threshold (Float)

    Defines maximum deviation of points to fitted curve. By default it is 30.

  • :max_iterations (Integer)

    Defines number of iteration for least-squares approximation method. By default, it is 30.

  • :colors_limit (Integer)

    The maximum number of colors used to quantize an image. Default value is 25.

  • :line_width (Float)

    The value of this parameter is affected by the graphics scale. Default value is 1.

Returns:



214
215
216
217
218
219
220
# File 'lib/aspose_html_cloud/api/html_api.rb', line 214

def vectorize(src, dst, src_in_local, dst_in_local, options=nil, storage_name=nil)
  input_format = get_input_format(src)
  output_format = File.extname(dst).strip.downcase[1..-1]
  fail ApiError.new('Input format must be bmp, jpeg, gif, tiff, png') unless %w[bmp jpeg gif tiff png].include? input_format
  fail ApiError.new('Output format must be SVG') if output_format != 'svg'
  convert(src, dst, src_in_local, dst_in_local, false, options, storage_name)
end

#vectorize_local_to_local(src, dst, options = nil) ⇒ OperationResult

Vectorization an image from the local file and putting the result in the local file

Parameters:

  • src

    Full path to source file(bmp, png, gif, tiff, jpeg).

  • dst

    Full path to a result file (svg).

  • options (Hash, nil) (defaults to: nil)

    the optional parameters

Options Hash (options):

  • :error_threshold (Float)

    Defines maximum deviation of points to fitted curve. By default it is 30.

  • :max_iterations (Integer)

    Defines number of iteration for least-squares approximation method. By default, it is 30.

  • :colors_limit (Integer)

    The maximum number of colors used to quantize an image. Default value is 25.

  • :line_width (Float)

    The value of this parameter is affected by the graphics scale. Default value is 1.

Returns:



152
153
154
# File 'lib/aspose_html_cloud/api/html_api.rb', line 152

def vectorize_local_to_local(src, dst, options = nil)
  vectorize(src, dst, true, true, options)
end

#vectorize_local_to_storage(src, dst, storage, options = nil) ⇒ OperationResult

Vectorization an image from the local file and putting the result in the storage

Parameters:

  • src

    Full path to source file(bmp, png, gif, tiff, jpeg).

  • dst

    Full path to a result file (svg).

  • storage

    Storage name. Default storage is nil.

  • options (Hash, nil) (defaults to: nil)

    the optional parameters

Options Hash (options):

  • :error_threshold (Float)

    Defines maximum deviation of points to fitted curve. By default it is 30.

  • :max_iterations (Integer)

    Defines number of iteration for least-squares approximation method. By default, it is 30.

  • :colors_limit (Integer)

    The maximum number of colors used to quantize an image. Default value is 25.

  • :line_width (Float)

    The value of this parameter is affected by the graphics scale. Default value is 1.

Returns:



167
168
169
# File 'lib/aspose_html_cloud/api/html_api.rb', line 167

def vectorize_local_to_storage(src, dst, storage, options=nil)
  vectorize(src, dst, true, false, options, storage)
end

#vectorize_storage_to_local(src, dst, storage, options = nil) ⇒ OperationResult

Vectorization an image from the storage and putting the result in the local file

Parameters:

  • src

    Full path to source file(bmp, png, gif, tiff, jpeg).

  • dst

    Full path to a result file (svg).

  • storage

    Storage name. Default storage is nil.

  • options (Hash, nil) (defaults to: nil)

    the optional parameters

Options Hash (options):

  • :error_threshold (Float)

    Defines maximum deviation of points to fitted curve. By default it is 30.

  • :max_iterations (Integer)

    Defines number of iteration for least-squares approximation method. By default, it is 30.

  • :colors_limit (Integer)

    The maximum number of colors used to quantize an image. Default value is 25.

  • :line_width (Float)

    The value of this parameter is affected by the graphics scale. Default value is 1.

Returns:



182
183
184
# File 'lib/aspose_html_cloud/api/html_api.rb', line 182

def vectorize_storage_to_local(src, dst, storage, options=nil)
  vectorize(src, dst, false, true, options, storage)
end

#vectorize_storage_to_storage(src, dst, storage, options = nil) ⇒ OperationResult

Vectorization an image from the storage and putting the result in the storage

Parameters:

  • src

    Full path to source file(bmp, png, gif, tiff, jpeg).

  • dst

    Full path to a result file (svg).

  • storage

    Storage name. Default storage is nil.

  • options (Hash, nil) (defaults to: nil)

    the optional parameters

Options Hash (options):

  • :error_threshold (Float)

    Defines maximum deviation of points to fitted curve. By default it is 30.

  • :max_iterations (Integer)

    Defines number of iteration for least-squares approximation method. By default, it is 30.

  • :colors_limit (Integer)

    The maximum number of colors used to quantize an image. Default value is 25.

  • :line_width (Float)

    The value of this parameter is affected by the graphics scale. Default value is 1.

Returns:



197
198
199
# File 'lib/aspose_html_cloud/api/html_api.rb', line 197

def vectorize_storage_to_storage(src, dst, storage, options=nil)
  vectorize(src, dst, false, false, options, storage)
end