Class: Pdfcrowd::ImageToPdfClient

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

Overview

Conversion from an image to PDF.

Instance Method Summary collapse

Constructor Details

#initialize(user_name, api_key) ⇒ ImageToPdfClient

Constructor for the Pdfcrowd API client.

  • user_name - Your username at Pdfcrowd.

  • api_key - Your API key.



3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
# File 'lib/pdfcrowd.rb', line 3341

def initialize(user_name, api_key)
    @helper = ConnectionHelper.new(user_name, api_key)
    @fields = {
        'input_format'=>'image',
        'output_format'=>'pdf'
    }
    @file_id = 1
    @files = {}
    @raw_data = {}
end

Instance Method Details

#convertFile(file) ⇒ Object

Convert a local file.

  • file - The path to a local file to convert. The file can be either a single file or an archive (.tar.gz, .tar.bz2, or .zip). The file must exist and not be empty.

  • Returns - Byte array containing the conversion output.



3402
3403
3404
3405
3406
3407
3408
3409
# File 'lib/pdfcrowd.rb', line 3402

def convertFile(file)
    if (!(File.file?(file) && !File.zero?(file)))
        raise Error.new(Pdfcrowd.create_invalid_value_message(file, "file", "image-to-pdf", "The file must exist and not be empty.", "convert_file"), 470);
    end
    
    @files['file'] = file
    @helper.post(@fields, @files, @raw_data)
end

#convertFileToFile(file, file_path) ⇒ Object

Convert a local file and write the result to a local file.

  • file - The path to a local file to convert. The file can be either a single file or an archive (.tar.gz, .tar.bz2, or .zip). The file must exist and not be empty.

  • file_path - The output file path. The string must not be empty.



3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
# File 'lib/pdfcrowd.rb', line 3428

def convertFileToFile(file, file_path)
    if (!(!file_path.nil? && !file_path.empty?))
        raise Error.new(Pdfcrowd.create_invalid_value_message(file_path, "file_path", "image-to-pdf", "The string must not be empty.", "convert_file_to_file"), 470);
    end
    
    output_file = open(file_path, "wb")
    begin
        convertFileToStream(file, output_file)
        output_file.close()
    rescue Error => why
        output_file.close()
        FileUtils.rm(file_path)
        raise
    end
end

#convertFileToStream(file, out_stream) ⇒ Object

Convert a local file and write the result to an output stream.

  • file - The path to a local file to convert. The file can be either a single file or an archive (.tar.gz, .tar.bz2, or .zip). The file must exist and not be empty.

  • out_stream - The output stream that will contain the conversion output.



3415
3416
3417
3418
3419
3420
3421
3422
# File 'lib/pdfcrowd.rb', line 3415

def convertFileToStream(file, out_stream)
    if (!(File.file?(file) && !File.zero?(file)))
        raise Error.new(Pdfcrowd.create_invalid_value_message(file, "file", "image-to-pdf", "The file must exist and not be empty.", "convert_file_to_stream"), 470);
    end
    
    @files['file'] = file
    @helper.post(@fields, @files, @raw_data, out_stream)
end

#convertRawData(data) ⇒ Object

Convert raw data.

  • data - The raw content to be converted.

  • Returns - Byte array with the output.



3448
3449
3450
3451
# File 'lib/pdfcrowd.rb', line 3448

def convertRawData(data)
    @raw_data['file'] = data
    @helper.post(@fields, @files, @raw_data)
end

#convertRawDataToFile(data, file_path) ⇒ Object

Convert raw data to a file.

  • data - The raw content to be converted.

  • file_path - The output file path. The string must not be empty.



3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
# File 'lib/pdfcrowd.rb', line 3466

def convertRawDataToFile(data, file_path)
    if (!(!file_path.nil? && !file_path.empty?))
        raise Error.new(Pdfcrowd.create_invalid_value_message(file_path, "file_path", "image-to-pdf", "The string must not be empty.", "convert_raw_data_to_file"), 470);
    end
    
    output_file = open(file_path, "wb")
    begin
        convertRawDataToStream(data, output_file)
        output_file.close()
    rescue Error => why
        output_file.close()
        FileUtils.rm(file_path)
        raise
    end
end

#convertRawDataToStream(data, out_stream) ⇒ Object

Convert raw data and write the result to an output stream.

  • data - The raw content to be converted.

  • out_stream - The output stream that will contain the conversion output.



3457
3458
3459
3460
# File 'lib/pdfcrowd.rb', line 3457

def convertRawDataToStream(data, out_stream)
    @raw_data['file'] = data
    @helper.post(@fields, @files, @raw_data, out_stream)
end

#convertUrl(url) ⇒ Object

Convert an image.

  • url - The address of the image to convert. The supported protocols are http:// and https://.

  • Returns - Byte array containing the conversion output.



3356
3357
3358
3359
3360
3361
3362
3363
# File 'lib/pdfcrowd.rb', line 3356

def convertUrl(url)
    unless /(?i)^https?:\/\/.*$/.match(url)
        raise Error.new(Pdfcrowd.create_invalid_value_message(url, "url", "image-to-pdf", "The supported protocols are http:// and https://.", "convert_url"), 470);
    end
    
    @fields['url'] = url
    @helper.post(@fields, @files, @raw_data)
end

#convertUrlToFile(url, file_path) ⇒ Object

Convert an image and write the result to a local file.

  • url - The address of the image to convert. The supported protocols are http:// and https://.

  • file_path - The output file path. The string must not be empty.



3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
# File 'lib/pdfcrowd.rb', line 3382

def convertUrlToFile(url, file_path)
    if (!(!file_path.nil? && !file_path.empty?))
        raise Error.new(Pdfcrowd.create_invalid_value_message(file_path, "file_path", "image-to-pdf", "The string must not be empty.", "convert_url_to_file"), 470);
    end
    
    output_file = open(file_path, "wb")
    begin
        convertUrlToStream(url, output_file)
        output_file.close()
    rescue Error => why
        output_file.close()
        FileUtils.rm(file_path)
        raise
    end
end

#convertUrlToStream(url, out_stream) ⇒ Object

Convert an image and write the result to an output stream.

  • url - The address of the image to convert. The supported protocols are http:// and https://.

  • out_stream - The output stream that will contain the conversion output.



3369
3370
3371
3372
3373
3374
3375
3376
# File 'lib/pdfcrowd.rb', line 3369

def convertUrlToStream(url, out_stream)
    unless /(?i)^https?:\/\/.*$/.match(url)
        raise Error.new(Pdfcrowd.create_invalid_value_message(url, "url", "image-to-pdf", "The supported protocols are http:// and https://.", "convert_url_to_stream"), 470);
    end
    
    @fields['url'] = url
    @helper.post(@fields, @files, @raw_data, out_stream)
end

#getConsumedCreditCountObject

Get the number of credits consumed by the last conversion.

  • Returns - The number of credits.



3526
3527
3528
# File 'lib/pdfcrowd.rb', line 3526

def getConsumedCreditCount()
    return @helper.getConsumedCreditCount()
end

#getDebugLogUrlObject

Get the URL of the debug log for the last conversion.

  • Returns - The link to the debug log.



3511
3512
3513
# File 'lib/pdfcrowd.rb', line 3511

def getDebugLogUrl()
    return @helper.getDebugLogUrl()
end

#getJobIdObject

Get the job id.

  • Returns - The unique job identifier.



3532
3533
3534
# File 'lib/pdfcrowd.rb', line 3532

def getJobId()
    return @helper.getJobId()
end

#getOutputSizeObject

Get the size of the output in bytes.

  • Returns - The count of bytes.



3538
3539
3540
# File 'lib/pdfcrowd.rb', line 3538

def getOutputSize()
    return @helper.getOutputSize()
end

#getRemainingCreditCountObject

Get the number of conversion credits available in your account. This method can only be called after a call to one of the convertXYZ methods. The returned value can differ from the actual count if you run parallel conversions. The special value 999999 is returned if the information is not available.

  • Returns - The number of credits.



3520
3521
3522
# File 'lib/pdfcrowd.rb', line 3520

def getRemainingCreditCount()
    return @helper.getRemainingCreditCount()
end

#setDebugLog(debug_log) ⇒ Object

Turn on the debug logging. Details about the conversion are stored in the debug log. The URL of the log can be obtained from the getDebugLogUrl method or available in conversion statistics.

  • debug_log - Set to true to enable the debug logging.

  • Returns - The converter object.



3504
3505
3506
3507
# File 'lib/pdfcrowd.rb', line 3504

def setDebugLog(debug_log)
    @fields['debug_log'] = debug_log
    self
end

#setHttpProxy(http_proxy) ⇒ Object

A proxy server used by Pdfcrowd conversion process for accessing the source URLs with HTTP scheme. It can help to circumvent regional restrictions or provide limited access to your intranet.

  • http_proxy - The value must have format DOMAIN_OR_IP_ADDRESS:PORT.

  • Returns - The converter object.



3555
3556
3557
3558
3559
3560
3561
3562
# File 'lib/pdfcrowd.rb', line 3555

def setHttpProxy(http_proxy)
    unless /(?i)^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z0-9]{1,}:\d+$/.match(http_proxy)
        raise Error.new(Pdfcrowd.create_invalid_value_message(http_proxy, "http_proxy", "image-to-pdf", "The value must have format DOMAIN_OR_IP_ADDRESS:PORT.", "set_http_proxy"), 470);
    end
    
    @fields['http_proxy'] = http_proxy
    self
end

#setHttpsProxy(https_proxy) ⇒ Object

A proxy server used by Pdfcrowd conversion process for accessing the source URLs with HTTPS scheme. It can help to circumvent regional restrictions or provide limited access to your intranet.

  • https_proxy - The value must have format DOMAIN_OR_IP_ADDRESS:PORT.

  • Returns - The converter object.



3568
3569
3570
3571
3572
3573
3574
3575
# File 'lib/pdfcrowd.rb', line 3568

def setHttpsProxy(https_proxy)
    unless /(?i)^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z0-9]{1,}:\d+$/.match(https_proxy)
        raise Error.new(Pdfcrowd.create_invalid_value_message(https_proxy, "https_proxy", "image-to-pdf", "The value must have format DOMAIN_OR_IP_ADDRESS:PORT.", "set_https_proxy"), 470);
    end
    
    @fields['https_proxy'] = https_proxy
    self
end

#setProxy(host, port, user_name, password) ⇒ Object

Specifies an HTTP proxy that the API client library will use to connect to the internet.

  • host - The proxy hostname.

  • port - The proxy port.

  • user_name - The username.

  • password - The password.

  • Returns - The converter object.



3602
3603
3604
3605
# File 'lib/pdfcrowd.rb', line 3602

def setProxy(host, port, user_name, password)
    @helper.setProxy(host, port, user_name, password)
    self
end

#setResize(resize) ⇒ Object

Resize the image.

  • resize - The resize percentage or new image dimensions.

  • Returns - The converter object.



3486
3487
3488
3489
# File 'lib/pdfcrowd.rb', line 3486

def setResize(resize)
    @fields['resize'] = resize
    self
end

#setRetryCount(retry_count) ⇒ Object

Specifies the number of retries when the 502 HTTP status code is received. The 502 status code indicates a temporary network issue. This feature can be disabled by setting to 0.

  • retry_count - Number of retries wanted.

  • Returns - The converter object.



3611
3612
3613
3614
# File 'lib/pdfcrowd.rb', line 3611

def setRetryCount(retry_count)
    @helper.setRetryCount(retry_count)
    self
end

#setRotate(rotate) ⇒ Object

Rotate the image.

  • rotate - The rotation specified in degrees.

  • Returns - The converter object.



3495
3496
3497
3498
# File 'lib/pdfcrowd.rb', line 3495

def setRotate(rotate)
    @fields['rotate'] = rotate
    self
end

#setTag(tag) ⇒ Object

Tag the conversion with a custom value. The tag is used in conversion statistics. A value longer than 32 characters is cut off.

  • tag - A string with the custom tag.

  • Returns - The converter object.



3546
3547
3548
3549
# File 'lib/pdfcrowd.rb', line 3546

def setTag(tag)
    @fields['tag'] = tag
    self
end

#setUseHttp(use_http) ⇒ Object

Specifies if the client communicates over HTTP or HTTPS with Pdfcrowd API.

  • use_http - Set to true to use HTTP.

  • Returns - The converter object.



3581
3582
3583
3584
# File 'lib/pdfcrowd.rb', line 3581

def setUseHttp(use_http)
    @helper.setUseHttp(use_http)
    self
end

#setUserAgent(user_agent) ⇒ Object

Set a custom user agent HTTP header. It can be usefull if you are behind some proxy or firewall.

  • user_agent - The user agent string.

  • Returns - The converter object.



3590
3591
3592
3593
# File 'lib/pdfcrowd.rb', line 3590

def setUserAgent(user_agent)
    @helper.setUserAgent(user_agent)
    self
end