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) ⇒ ConversionResult

Converting the HTML document to various formats

Parameters:

  • src

    Source file or URL.

  • dst

    Full path to a result file (pdf, xps, docx, md, mhtml, jpeg, png, bmp, tiff, gif).

  • 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 (Double)

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

  • :height (Double)

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

  • :left_margin (Double)

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

  • :right_margin (Double)

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

  • :top_margin (Double)

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

  • :bottom_margin (Double)

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

Returns:



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/aspose_html_cloud/api/html_api.rb', line 158

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?
  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 => 'ConversionResult')
  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) ⇒ ConversionResult

Converting the HTML, EPUB 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 (pdf, xps, docx, md, mhtml, jpeg, png, bmp, tiff, gif).

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

    the optional parameters

Options Hash (options):

  • :width (Double)

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

  • :height (Double)

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

  • :left_margin (Double)

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

  • :right_margin (Double)

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

  • :top_margin (Double)

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

  • :bottom_margin (Double)

    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) ⇒ ConversionResult

Converting the HTML, EPUB 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 (pdf, xps, docx, md, mhtml, jpeg, png, bmp, tiff, gif).

  • storage

    Storage name. Default storage is nil.

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

    the optional parameters

Options Hash (options):

  • :width (Double)

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

  • :height (Double)

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

  • :left_margin (Double)

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

  • :right_margin (Double)

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

  • :top_margin (Double)

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

  • :bottom_margin (Double)

    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) ⇒ ConversionResult

Converting the HTML, EPUB 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 (pdf, xps, docx, md, mhtml, jpeg, png, bmp, tiff, gif).

  • storage

    Storage name. Default storage is nil.

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

    the optional parameters

Options Hash (options):

  • :width (Double)

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

  • :height (Double)

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

  • :left_margin (Double)

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

  • :right_margin (Double)

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

  • :top_margin (Double)

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

  • :bottom_margin (Double)

    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) ⇒ ConversionResult

Converting the HTML, EPUB 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 (pdf, xps, docx, md, mhtml, jpeg, png, bmp, tiff, gif).

  • storage

    Storage name. Default storage is nil.

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

    the optional parameters

Options Hash (options):

  • :width (Double)

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

  • :height (Double)

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

  • :left_margin (Double)

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

  • :right_margin (Double)

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

  • :top_margin (Double)

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

  • :bottom_margin (Double)

    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) ⇒ ConversionResult

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 (pdf, xps, docx, md, mhtml, jpeg, png, bmp, tiff, gif).

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

    the optional parameters

Options Hash (options):

  • :width (Double)

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

  • :height (Double)

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

  • :left_margin (Double)

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

  • :right_margin (Double)

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

  • :top_margin (Double)

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

  • :bottom_margin (Double)

    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) ⇒ ConversionResult

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 (pdf, xps, docx, md, mhtml, jpeg, png, bmp, tiff, gif).

  • storage

    Storage name. Default storage is nil.

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

    the optional parameters

Options Hash (options):

  • :width (Double)

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

  • :height (Double)

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

  • :left_margin (Double)

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

  • :right_margin (Double)

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

  • :top_margin (Double)

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

  • :bottom_margin (Double)

    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



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/aspose_html_cloud/api/html_api.rb', line 270

def get_input_format(path)
  ext = File.extname(path).upcase
  case ext
    when '.HTML', '.HTM'
      return 'html'
    when '.MHT', '.MHTML'
      return 'mhtml'
    when '.XHTML', '.XML'
      return 'xhtml'
    when '.EPUB'
      return 'epub'
    when '.SVG'
      return 'svg'
    when '.MD'
      return 'md'
    else
      return nil
  end
end

#get_status(id) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/aspose_html_cloud/api/html_api.rb', line 247

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 => 'ConversionResult')

end