Class: Pdfcrowd::PdfToImageClient
- Inherits:
-
Object
- Object
- Pdfcrowd::PdfToImageClient
- Defined in:
- lib/pdfcrowd.rb
Overview
Conversion from PDF to image.
Instance Method Summary collapse
-
#convertFile(file) ⇒ Object
Convert a local file.
-
#convertFileToFile(file, file_path) ⇒ Object
Convert a local file and write the result to a local file.
-
#convertFileToStream(file, out_stream) ⇒ Object
Convert a local file and write the result to an output stream.
-
#convertRawData(data) ⇒ Object
Convert raw data.
-
#convertRawDataToFile(data, file_path) ⇒ Object
Convert raw data to a file.
-
#convertRawDataToStream(data, out_stream) ⇒ Object
Convert raw data and write the result to an output stream.
-
#convertStream(in_stream) ⇒ Object
Convert the contents of an input stream.
-
#convertStreamToFile(in_stream, file_path) ⇒ Object
Convert the contents of an input stream and write the result to a local file.
-
#convertStreamToStream(in_stream, out_stream) ⇒ Object
Convert the contents of an input stream and write the result to an output stream.
-
#convertUrl(url) ⇒ Object
Convert an image.
-
#convertUrlToFile(url, file_path) ⇒ Object
Convert an image and write the result to a local file.
-
#convertUrlToStream(url, out_stream) ⇒ Object
Convert an image and write the result to an output stream.
-
#getConsumedCreditCount ⇒ Object
Get the number of credits consumed by the last conversion.
-
#getDebugLogUrl ⇒ Object
Get the URL of the debug log for the last conversion.
-
#getJobId ⇒ Object
Get the job id.
-
#getOutputSize ⇒ Object
Get the size of the output in bytes.
-
#getPageCount ⇒ Object
Get the number of pages in the output document.
-
#getRemainingCreditCount ⇒ Object
Get the number of conversion credits available in your account.
-
#getVersion ⇒ Object
Get the version details.
-
#initialize(user_name, api_key) ⇒ PdfToImageClient
constructor
Constructor for the Pdfcrowd API client.
-
#isZippedOutput ⇒ Object
A helper method to determine if the output file from a conversion process is a zip archive.
-
#setCropArea(x, y, width, height) ⇒ Object
Set the crop area.
-
#setCropAreaHeight(height) ⇒ Object
Set the height of the crop area in points.
-
#setCropAreaWidth(width) ⇒ Object
Set the width of the crop area in points.
-
#setCropAreaX(x) ⇒ Object
Set the top left X coordinate of the crop area in points.
-
#setCropAreaY(y) ⇒ Object
Set the top left Y coordinate of the crop area in points.
-
#setDebugLog(value) ⇒ Object
Turn on the debug logging.
-
#setDpi(dpi) ⇒ Object
Set the output graphics DPI.
-
#setForceZip(value) ⇒ Object
Enforces the zip output format.
-
#setHttpProxy(proxy) ⇒ Object
A proxy server used by Pdfcrowd conversion process for accessing the source URLs with HTTP scheme.
-
#setHttpsProxy(proxy) ⇒ Object
A proxy server used by Pdfcrowd conversion process for accessing the source URLs with HTTPS scheme.
-
#setOutputFormat(output_format) ⇒ Object
The format of the output file.
-
#setPdfPassword(password) ⇒ Object
Password to open the encrypted PDF file.
-
#setPrintPageRange(pages) ⇒ Object
Set the page range to print.
-
#setProxy(host, port, user_name, password) ⇒ Object
Specifies an HTTP proxy that the API client library will use to connect to the internet.
-
#setRetryCount(count) ⇒ Object
Specifies the number of automatic retries when the 502 or 503 HTTP status code is received.
-
#setTag(tag) ⇒ Object
Tag the conversion with a custom value.
-
#setUseCropbox(value) ⇒ Object
Use the crop box rather than media box.
-
#setUseGrayscale(value) ⇒ Object
Generate a grayscale image.
-
#setUseHttp(value) ⇒ Object
Specifies if the client communicates over HTTP or HTTPS with Pdfcrowd API.
-
#setUserAgent(agent) ⇒ Object
Set a custom user agent HTTP header.
Constructor Details
#initialize(user_name, api_key) ⇒ PdfToImageClient
Constructor for the Pdfcrowd API client.
-
user_name- Your username at Pdfcrowd. -
api_key- Your API key.
6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 |
# File 'lib/pdfcrowd.rb', line 6341 def initialize(user_name, api_key) @helper = ConnectionHelper.new(user_name, api_key) @fields = { 'input_format'=>'pdf', 'output_format'=>'png' } @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 must exist and not be empty. -
Returns - Byte array containing the conversion output.
6402 6403 6404 6405 6406 6407 6408 6409 |
# File 'lib/pdfcrowd.rb', line 6402 def convertFile(file) if (!(File.file?(file) && !File.zero?(file))) raise Error.new(Pdfcrowd.(file, "convertFile", "pdf-to-image", "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 must exist and not be empty. -
file_path- The output file path. The string must not be empty.
6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 |
# File 'lib/pdfcrowd.rb', line 6428 def convertFileToFile(file, file_path) if (!(!file_path.nil? && !file_path.empty?)) raise Error.new(Pdfcrowd.(file_path, "convertFileToFile::file_path", "pdf-to-image", "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 must exist and not be empty. -
out_stream- The output stream that will contain the conversion output.
6415 6416 6417 6418 6419 6420 6421 6422 |
# File 'lib/pdfcrowd.rb', line 6415 def convertFileToStream(file, out_stream) if (!(File.file?(file) && !File.zero?(file))) raise Error.new(Pdfcrowd.(file, "convertFileToStream::file", "pdf-to-image", "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.
6448 6449 6450 6451 |
# File 'lib/pdfcrowd.rb', line 6448 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.
6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 |
# File 'lib/pdfcrowd.rb', line 6466 def convertRawDataToFile(data, file_path) if (!(!file_path.nil? && !file_path.empty?)) raise Error.new(Pdfcrowd.(file_path, "convertRawDataToFile::file_path", "pdf-to-image", "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.
6457 6458 6459 6460 |
# File 'lib/pdfcrowd.rb', line 6457 def convertRawDataToStream(data, out_stream) @raw_data['file'] = data @helper.post(@fields, @files, @raw_data, out_stream) end |
#convertStream(in_stream) ⇒ Object
Convert the contents of an input stream.
-
in_stream- The input stream with source data. -
Returns - Byte array containing the conversion output.
6486 6487 6488 6489 |
# File 'lib/pdfcrowd.rb', line 6486 def convertStream(in_stream) @raw_data['stream'] = in_stream.read @helper.post(@fields, @files, @raw_data) end |
#convertStreamToFile(in_stream, file_path) ⇒ Object
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.
6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 |
# File 'lib/pdfcrowd.rb', line 6504 def convertStreamToFile(in_stream, file_path) if (!(!file_path.nil? && !file_path.empty?)) raise Error.new(Pdfcrowd.(file_path, "convertStreamToFile::file_path", "pdf-to-image", "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 |
#convertStreamToStream(in_stream, out_stream) ⇒ Object
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.
6495 6496 6497 6498 |
# File 'lib/pdfcrowd.rb', line 6495 def convertStreamToStream(in_stream, out_stream) @raw_data['stream'] = in_stream.read @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.
6356 6357 6358 6359 6360 6361 6362 6363 |
# File 'lib/pdfcrowd.rb', line 6356 def convertUrl(url) unless /(?i)^https?:\/\/.*$/.match(url) raise Error.new(Pdfcrowd.(url, "convertUrl", "pdf-to-image", "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.
6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 |
# File 'lib/pdfcrowd.rb', line 6382 def convertUrlToFile(url, file_path) if (!(!file_path.nil? && !file_path.empty?)) raise Error.new(Pdfcrowd.(file_path, "convertUrlToFile::file_path", "pdf-to-image", "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.
6369 6370 6371 6372 6373 6374 6375 6376 |
# File 'lib/pdfcrowd.rb', line 6369 def convertUrlToStream(url, out_stream) unless /(?i)^https?:\/\/.*$/.match(url) raise Error.new(Pdfcrowd.(url, "convertUrlToStream::url", "pdf-to-image", "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 |
#getConsumedCreditCount ⇒ Object
Get the number of credits consumed by the last conversion.
-
Returns - The number of credits.
6690 6691 6692 |
# File 'lib/pdfcrowd.rb', line 6690 def getConsumedCreditCount() return @helper.getConsumedCreditCount() end |
#getDebugLogUrl ⇒ Object
Get the URL of the debug log for the last conversion.
-
Returns - The link to the debug log.
6675 6676 6677 |
# File 'lib/pdfcrowd.rb', line 6675 def getDebugLogUrl() return @helper.getDebugLogUrl() end |
#getJobId ⇒ Object
Get the job id.
-
Returns - The unique job identifier.
6696 6697 6698 |
# File 'lib/pdfcrowd.rb', line 6696 def getJobId() return @helper.getJobId() end |
#getOutputSize ⇒ Object
Get the size of the output in bytes.
-
Returns - The count of bytes.
6708 6709 6710 |
# File 'lib/pdfcrowd.rb', line 6708 def getOutputSize() return @helper.getOutputSize() end |
#getPageCount ⇒ Object
Get the number of pages in the output document.
-
Returns - The page count.
6702 6703 6704 |
# File 'lib/pdfcrowd.rb', line 6702 def getPageCount() return @helper.getPageCount() end |
#getRemainingCreditCount ⇒ Object
Get the number of conversion credits available in your account. This method can only be called after a call to one of the convertXtoY 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.
6684 6685 6686 |
# File 'lib/pdfcrowd.rb', line 6684 def getRemainingCreditCount() return @helper.getRemainingCreditCount() end |
#getVersion ⇒ Object
Get the version details.
-
Returns - API version, converter version, and client version.
6714 6715 6716 |
# File 'lib/pdfcrowd.rb', line 6714 def getVersion() return "client " + CLIENT_VERSION + ", API v2, converter " + @helper.getConverterVersion() end |
#isZippedOutput ⇒ Object
A helper method to determine if the output file from a conversion process is a zip archive. The conversion output can be either a single image file or a zip file containing one or more image files. This method should be called after the conversion has been successfully completed.
-
Returns - True if the conversion output is a zip archive, otherwise False.
6566 6567 6568 |
# File 'lib/pdfcrowd.rb', line 6566 def isZippedOutput() @fields.fetch('force_zip', false) == true || getPageCount() > 1 end |
#setCropArea(x, y, width, height) ⇒ Object
Set the crop area. It allows to extract just a part of a PDF page.
-
x- Set the top left X coordinate of the crop area in points. Must be a positive integer number or 0. -
y- Set the top left Y coordinate of the crop area in points. Must be a positive integer number or 0. -
width- Set the width of the crop area in points. Must be a positive integer number or 0. -
height- Set the height of the crop area in points. Must be a positive integer number or 0. -
Returns - The converter object.
6647 6648 6649 6650 6651 6652 6653 |
# File 'lib/pdfcrowd.rb', line 6647 def setCropArea(x, y, width, height) setCropAreaX(x) setCropAreaY(y) setCropAreaWidth(width) setCropAreaHeight(height) self end |
#setCropAreaHeight(height) ⇒ Object
Set the height of the crop area in points.
-
height- Must be a positive integer number or 0. -
Returns - The converter object.
6631 6632 6633 6634 6635 6636 6637 6638 |
# File 'lib/pdfcrowd.rb', line 6631 def setCropAreaHeight(height) if (!(Integer(height) >= 0)) raise Error.new(Pdfcrowd.(height, "setCropAreaHeight", "pdf-to-image", "Must be a positive integer number or 0.", "set_crop_area_height"), 470); end @fields['crop_area_height'] = height self end |
#setCropAreaWidth(width) ⇒ Object
Set the width of the crop area in points.
-
width- Must be a positive integer number or 0. -
Returns - The converter object.
6618 6619 6620 6621 6622 6623 6624 6625 |
# File 'lib/pdfcrowd.rb', line 6618 def setCropAreaWidth(width) if (!(Integer(width) >= 0)) raise Error.new(Pdfcrowd.(width, "setCropAreaWidth", "pdf-to-image", "Must be a positive integer number or 0.", "set_crop_area_width"), 470); end @fields['crop_area_width'] = width self end |
#setCropAreaX(x) ⇒ Object
Set the top left X coordinate of the crop area in points.
-
x- Must be a positive integer number or 0. -
Returns - The converter object.
6592 6593 6594 6595 6596 6597 6598 6599 |
# File 'lib/pdfcrowd.rb', line 6592 def setCropAreaX(x) if (!(Integer(x) >= 0)) raise Error.new(Pdfcrowd.(x, "setCropAreaX", "pdf-to-image", "Must be a positive integer number or 0.", "set_crop_area_x"), 470); end @fields['crop_area_x'] = x self end |
#setCropAreaY(y) ⇒ Object
Set the top left Y coordinate of the crop area in points.
-
y- Must be a positive integer number or 0. -
Returns - The converter object.
6605 6606 6607 6608 6609 6610 6611 6612 |
# File 'lib/pdfcrowd.rb', line 6605 def setCropAreaY(y) if (!(Integer(y) >= 0)) raise Error.new(Pdfcrowd.(y, "setCropAreaY", "pdf-to-image", "Must be a positive integer number or 0.", "set_crop_area_y"), 470); end @fields['crop_area_y'] = y self end |
#setDebugLog(value) ⇒ 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.
-
value- Set to true to enable the debug logging. -
Returns - The converter object.
6668 6669 6670 6671 |
# File 'lib/pdfcrowd.rb', line 6668 def setDebugLog(value) @fields['debug_log'] = value self end |
#setDpi(dpi) ⇒ Object
Set the output graphics DPI.
-
dpi- The DPI value. -
Returns - The converter object.
6559 6560 6561 6562 |
# File 'lib/pdfcrowd.rb', line 6559 def setDpi(dpi) @fields['dpi'] = dpi self end |
#setForceZip(value) ⇒ Object
Enforces the zip output format.
-
value- Set to true to get the output as a zip archive. -
Returns - The converter object.
6574 6575 6576 6577 |
# File 'lib/pdfcrowd.rb', line 6574 def setForceZip(value) @fields['force_zip'] = value self end |
#setHttpProxy(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.
-
proxy- The value must have format DOMAIN_OR_IP_ADDRESS:PORT. -
Returns - The converter object.
6731 6732 6733 6734 6735 6736 6737 6738 |
# File 'lib/pdfcrowd.rb', line 6731 def setHttpProxy(proxy) unless /(?i)^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z0-9]{1,}:\d+$/.match(proxy) raise Error.new(Pdfcrowd.(proxy, "setHttpProxy", "pdf-to-image", "The value must have format DOMAIN_OR_IP_ADDRESS:PORT.", "set_http_proxy"), 470); end @fields['http_proxy'] = proxy self end |
#setHttpsProxy(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.
-
proxy- The value must have format DOMAIN_OR_IP_ADDRESS:PORT. -
Returns - The converter object.
6744 6745 6746 6747 6748 6749 6750 6751 |
# File 'lib/pdfcrowd.rb', line 6744 def setHttpsProxy(proxy) unless /(?i)^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z0-9]{1,}:\d+$/.match(proxy) raise Error.new(Pdfcrowd.(proxy, "setHttpsProxy", "pdf-to-image", "The value must have format DOMAIN_OR_IP_ADDRESS:PORT.", "set_https_proxy"), 470); end @fields['https_proxy'] = proxy self end |
#setOutputFormat(output_format) ⇒ Object
The format of the output file.
-
output_format- Allowed values are png, jpg, gif, tiff, bmp, ico, ppm, pgm, pbm, pnm, psb, pct, ras, tga, sgi, sun, webp. -
Returns - The converter object.
6524 6525 6526 6527 6528 6529 6530 6531 |
# File 'lib/pdfcrowd.rb', line 6524 def setOutputFormat(output_format) unless /(?i)^(png|jpg|gif|tiff|bmp|ico|ppm|pgm|pbm|pnm|psb|pct|ras|tga|sgi|sun|webp)$/.match(output_format) raise Error.new(Pdfcrowd.(output_format, "setOutputFormat", "pdf-to-image", "Allowed values are png, jpg, gif, tiff, bmp, ico, ppm, pgm, pbm, pnm, psb, pct, ras, tga, sgi, sun, webp.", "set_output_format"), 470); end @fields['output_format'] = output_format self end |
#setPdfPassword(password) ⇒ Object
Password to open the encrypted PDF file.
-
password- The input PDF password. -
Returns - The converter object.
6537 6538 6539 6540 |
# File 'lib/pdfcrowd.rb', line 6537 def setPdfPassword(password) @fields['pdf_password'] = password self end |
#setPrintPageRange(pages) ⇒ Object
Set the page range to print.
-
pages- A comma separated list of page numbers or ranges. -
Returns - The converter object.
6546 6547 6548 6549 6550 6551 6552 6553 |
# File 'lib/pdfcrowd.rb', line 6546 def setPrintPageRange(pages) unless /^(?:\s*(?:\d+|(?:\d*\s*\-\s*\d+)|(?:\d+\s*\-\s*\d*))\s*,\s*)*\s*(?:\d+|(?:\d*\s*\-\s*\d+)|(?:\d+\s*\-\s*\d*))\s*$/.match(pages) raise Error.new(Pdfcrowd.(pages, "setPrintPageRange", "pdf-to-image", "A comma separated list of page numbers or ranges.", "set_print_page_range"), 470); end @fields['print_page_range'] = pages 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.
6779 6780 6781 6782 |
# File 'lib/pdfcrowd.rb', line 6779 def setProxy(host, port, user_name, password) @helper.setProxy(host, port, user_name, password) self end |
#setRetryCount(count) ⇒ Object
Specifies the number of automatic retries when the 502 or 503 HTTP status code is received. The status code indicates a temporary network issue. This feature can be disabled by setting to 0.
-
count- Number of retries. -
Returns - The converter object.
6788 6789 6790 6791 |
# File 'lib/pdfcrowd.rb', line 6788 def setRetryCount(count) @helper.setRetryCount(count) 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.
6722 6723 6724 6725 |
# File 'lib/pdfcrowd.rb', line 6722 def setTag(tag) @fields['tag'] = tag self end |
#setUseCropbox(value) ⇒ Object
Use the crop box rather than media box.
-
value- Set to true to use crop box. -
Returns - The converter object.
6583 6584 6585 6586 |
# File 'lib/pdfcrowd.rb', line 6583 def setUseCropbox(value) @fields['use_cropbox'] = value self end |
#setUseGrayscale(value) ⇒ Object
Generate a grayscale image.
-
value- Set to true to generate a grayscale image. -
Returns - The converter object.
6659 6660 6661 6662 |
# File 'lib/pdfcrowd.rb', line 6659 def setUseGrayscale(value) @fields['use_grayscale'] = value self end |
#setUseHttp(value) ⇒ Object
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.
6758 6759 6760 6761 |
# File 'lib/pdfcrowd.rb', line 6758 def setUseHttp(value) @helper.setUseHttp(value) self end |
#setUserAgent(agent) ⇒ Object
Set a custom user agent HTTP header. It can be useful if you are behind a proxy or a firewall.
-
agent- The user agent string. -
Returns - The converter object.
6767 6768 6769 6770 |
# File 'lib/pdfcrowd.rb', line 6767 def setUserAgent(agent) @helper.setUserAgent(agent) self end |