class Pdfcrowd::ImageToPdfClient
Conversion from an image to PDF.
Public Class Methods
Constructor for the Pdfcrowd
API client.
-
user_name
- Your username atPdfcrowd
. -
api_key
- Your API key.
# File lib/pdfcrowd.rb, line 4017 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
Public Instance Methods
Convert a local file.
-
file
- The path to a local file to convert. The file must exist and not be empty. -
Returns - Byte array containing the conversion output.
# File lib/pdfcrowd.rb, line 4078 def convertFile(file) if (!(File.file?(file) && !File.zero?(file))) raise Error.new(Pdfcrowd.create_invalid_value_message(file, "convertFile", "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
Convert a local file and write the result to a local file.
-
file
- The path to a local file to convert. The file must exist and not be empty. -
file_path
- The output file path. The string must not be empty.
# File lib/pdfcrowd.rb, line 4104 def convertFileToFile(file, file_path) if (!(!file_path.nil? && !file_path.empty?)) raise Error.new(Pdfcrowd.create_invalid_value_message(file_path, "convertFileToFile::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
Convert a local file and write the result to an output stream.
-
file
- The path to a local file to convert. The file must exist and not be empty. -
out_stream
- The output stream that will contain the conversion output.
# File lib/pdfcrowd.rb, line 4091 def convertFileToStream(file, out_stream) if (!(File.file?(file) && !File.zero?(file))) raise Error.new(Pdfcrowd.create_invalid_value_message(file, "convertFileToStream::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
Convert raw data.
-
data
- The raw content to be converted. -
Returns - Byte array with the output.
# File lib/pdfcrowd.rb, line 4124 def convertRawData(data) @raw_data['file'] = data @helper.post(@fields, @files, @raw_data) end
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.
# File lib/pdfcrowd.rb, line 4142 def convertRawDataToFile(data, file_path) if (!(!file_path.nil? && !file_path.empty?)) raise Error.new(Pdfcrowd.create_invalid_value_message(file_path, "convertRawDataToFile::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
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.
# File lib/pdfcrowd.rb, line 4133 def convertRawDataToStream(data, out_stream) @raw_data['file'] = data @helper.post(@fields, @files, @raw_data, out_stream) end
Convert the contents of an input stream.
-
in_stream
- The input stream with source data. -
Returns - Byte array containing the conversion output.
# File lib/pdfcrowd.rb, line 4162 def convertStream(in_stream) @raw_data['stream'] = in_stream.read @helper.post(@fields, @files, @raw_data) end
Convert the contents of an input stream and write the result to a local file.
-
in_stream
- The input stream with source data. -
file_path
- The output file path. The string must not be empty.
# File lib/pdfcrowd.rb, line 4180 def convertStreamToFile(in_stream, file_path) if (!(!file_path.nil? && !file_path.empty?)) raise Error.new(Pdfcrowd.create_invalid_value_message(file_path, "convertStreamToFile::file_path", "image-to-pdf", "The string must not be empty.", "convert_stream_to_file"), 470); end output_file = open(file_path, "wb") begin convertStreamToStream(in_stream, output_file) output_file.close() rescue Error => why output_file.close() FileUtils.rm(file_path) raise end end
Convert the contents of an input stream and write the result to an output stream.
-
in_stream
- The input stream with source data. -
out_stream
- The output stream that will contain the conversion output.
# File lib/pdfcrowd.rb, line 4171 def convertStreamToStream(in_stream, out_stream) @raw_data['stream'] = in_stream.read @helper.post(@fields, @files, @raw_data, out_stream) end
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.
# File lib/pdfcrowd.rb, line 4032 def convertUrl(url) unless /(?i)^https?:\/\/.*$/.match(url) raise Error.new(Pdfcrowd.create_invalid_value_message(url, "convertUrl", "image-to-pdf", "The supported protocols are http:// and https://.", "convert_url"), 470); end @fields['url'] = url @helper.post(@fields, @files, @raw_data) end
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.
# File lib/pdfcrowd.rb, line 4058 def convertUrlToFile(url, file_path) if (!(!file_path.nil? && !file_path.empty?)) raise Error.new(Pdfcrowd.create_invalid_value_message(file_path, "convertUrlToFile::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
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.
# File lib/pdfcrowd.rb, line 4045 def convertUrlToStream(url, out_stream) unless /(?i)^https?:\/\/.*$/.match(url) raise Error.new(Pdfcrowd.create_invalid_value_message(url, "convertUrlToStream::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
Get the number of credits consumed by the last conversion.
-
Returns - The number of credits.
# File lib/pdfcrowd.rb, line 4240 def getConsumedCreditCount() return @helper.getConsumedCreditCount() end
Get the URL of the debug log for the last conversion.
-
Returns - The link to the debug log.
# File lib/pdfcrowd.rb, line 4225 def getDebugLogUrl() return @helper.getDebugLogUrl() end
Get the job id.
-
Returns - The unique job identifier.
# File lib/pdfcrowd.rb, line 4246 def getJobId() return @helper.getJobId() end
Get the size of the output in bytes.
-
Returns - The count of bytes.
# File lib/pdfcrowd.rb, line 4252 def getOutputSize() return @helper.getOutputSize() end
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.
# File lib/pdfcrowd.rb, line 4234 def getRemainingCreditCount() return @helper.getRemainingCreditCount() end
Get the version details.
-
Returns - API version, converter version, and client version.
# File lib/pdfcrowd.rb, line 4258 def getVersion() return "client " + CLIENT_VERSION + ", API v2, converter " + @helper.getConverterVersion() end
Set the converter version. Different versions may produce different output. Choose which one provides the best output for your case.
-
version
- The version identifier. Allowed values are latest, 20.10, 18.10. -
Returns - The converter object.
# File lib/pdfcrowd.rb, line 4301 def setConverterVersion(version) unless /(?i)^(latest|20.10|18.10)$/.match(version) raise Error.new(Pdfcrowd.create_invalid_value_message(version, "setConverterVersion", "image-to-pdf", "Allowed values are latest, 20.10, 18.10.", "set_converter_version"), 470); end @helper.setConverterVersion(version) self end
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.
-
value
- Set to true to enable the debug logging. -
Returns - The converter object.
# File lib/pdfcrowd.rb, line 4218 def setDebugLog(value) @fields['debug_log'] = value self end
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.
-
proxy
- The value must have format DOMAIN_OR_IP_ADDRESS:PORT. -
Returns - The converter object.
# File lib/pdfcrowd.rb, line 4275 def setHttpProxy(proxy) unless /(?i)^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z0-9]{1,}:\d+$/.match(proxy) raise Error.new(Pdfcrowd.create_invalid_value_message(proxy, "setHttpProxy", "image-to-pdf", "The value must have format DOMAIN_OR_IP_ADDRESS:PORT.", "set_http_proxy"), 470); end @fields['http_proxy'] = proxy self end
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.
-
proxy
- The value must have format DOMAIN_OR_IP_ADDRESS:PORT. -
Returns - The converter object.
# File lib/pdfcrowd.rb, line 4288 def setHttpsProxy(proxy) unless /(?i)^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z0-9]{1,}:\d+$/.match(proxy) raise Error.new(Pdfcrowd.create_invalid_value_message(proxy, "setHttpsProxy", "image-to-pdf", "The value must have format DOMAIN_OR_IP_ADDRESS:PORT.", "set_https_proxy"), 470); end @fields['https_proxy'] = proxy self end
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.
# File lib/pdfcrowd.rb, line 4336 def setProxy(host, port, user_name, password) @helper.setProxy(host, port, user_name, password) self end
Resize the image.
-
resize
- The resize percentage or new image dimensions. -
Returns - The converter object.
# File lib/pdfcrowd.rb, line 4200 def setResize(resize) @fields['resize'] = resize self end
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.
-
count
- Number of retries wanted. -
Returns - The converter object.
# File lib/pdfcrowd.rb, line 4345 def setRetryCount(count) @helper.setRetryCount(count) self end
Rotate the image.
-
rotate
- The rotation specified in degrees. -
Returns - The converter object.
# File lib/pdfcrowd.rb, line 4209 def setRotate(rotate) @fields['rotate'] = rotate self end
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.
# File lib/pdfcrowd.rb, line 4266 def setTag(tag) @fields['tag'] = tag self end
Specifies if the client communicates over HTTP or HTTPS with Pdfcrowd
API. Warning: Using HTTP is insecure as data sent over HTTP is not encrypted. Enable this option only if you know what you are doing.
-
value
- Set to true to use HTTP. -
Returns - The converter object.
# File lib/pdfcrowd.rb, line 4315 def setUseHttp(value) @helper.setUseHttp(value) self end
Set a custom user agent HTTP header. It can be useful if you are behind some proxy or firewall.
-
agent
- The user agent string. -
Returns - The converter object.
# File lib/pdfcrowd.rb, line 4324 def setUserAgent(agent) @helper.setUserAgent(agent) self end