Class: Pdfcrowd::PdfToTextClient

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

Overview

Conversion from PDF to text.

Instance Method Summary collapse

Constructor Details

#initialize(user_name, api_key) ⇒ PdfToTextClient

Returns a new instance of PdfToTextClient.



4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
# File 'lib/pdfcrowd.rb', line 4759

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

Instance Method Details

#convertFile(file) ⇒ Object



4808
4809
4810
4811
4812
4813
4814
4815
# File 'lib/pdfcrowd.rb', line 4808

def convertFile(file)
    if (!(File.file?(file) && !File.zero?(file)))
        raise Error.new(Pdfcrowd.create_invalid_value_message(file, "convertFile", "pdf-to-text", "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



4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
# File 'lib/pdfcrowd.rb', line 4828

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", "pdf-to-text", "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



4818
4819
4820
4821
4822
4823
4824
4825
# File 'lib/pdfcrowd.rb', line 4818

def convertFileToStream(file, out_stream)
    if (!(File.file?(file) && !File.zero?(file)))
        raise Error.new(Pdfcrowd.create_invalid_value_message(file, "convertFileToStream::file", "pdf-to-text", "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



4845
4846
4847
4848
# File 'lib/pdfcrowd.rb', line 4845

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

#convertRawDataToFile(data, file_path) ⇒ Object



4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
# File 'lib/pdfcrowd.rb', line 4857

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", "pdf-to-text", "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



4851
4852
4853
4854
# File 'lib/pdfcrowd.rb', line 4851

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

#convertStream(in_stream) ⇒ Object



4874
4875
4876
4877
# File 'lib/pdfcrowd.rb', line 4874

def convertStream(in_stream)
    @raw_data['stream'] = in_stream.read
    @helper.post(@fields, @files, @raw_data)
end

#convertStreamToFile(in_stream, file_path) ⇒ Object



4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
# File 'lib/pdfcrowd.rb', line 4886

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", "pdf-to-text", "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



4880
4881
4882
4883
# File 'lib/pdfcrowd.rb', line 4880

def convertStreamToStream(in_stream, out_stream)
    @raw_data['stream'] = in_stream.read
    @helper.post(@fields, @files, @raw_data, out_stream)
end

#convertUrl(url) ⇒ Object



4771
4772
4773
4774
4775
4776
4777
4778
# File 'lib/pdfcrowd.rb', line 4771

def convertUrl(url)
    unless /(?i)^https?:\/\/.*$/.match(url)
        raise Error.new(Pdfcrowd.create_invalid_value_message(url, "convertUrl", "pdf-to-text", "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



4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
# File 'lib/pdfcrowd.rb', line 4791

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", "pdf-to-text", "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



4781
4782
4783
4784
4785
4786
4787
4788
# File 'lib/pdfcrowd.rb', line 4781

def convertUrlToStream(url, out_stream)
    unless /(?i)^https?:\/\/.*$/.match(url)
        raise Error.new(Pdfcrowd.create_invalid_value_message(url, "convertUrlToStream::url", "pdf-to-text", "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



5048
5049
5050
# File 'lib/pdfcrowd.rb', line 5048

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

#getDebugLogUrlObject



5038
5039
5040
# File 'lib/pdfcrowd.rb', line 5038

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

#getJobIdObject



5053
5054
5055
# File 'lib/pdfcrowd.rb', line 5053

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

#getOutputSizeObject



5063
5064
5065
# File 'lib/pdfcrowd.rb', line 5063

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

#getPageCountObject



5058
5059
5060
# File 'lib/pdfcrowd.rb', line 5058

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

#getRemainingCreditCountObject



5043
5044
5045
# File 'lib/pdfcrowd.rb', line 5043

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

#getVersionObject



5068
5069
5070
# File 'lib/pdfcrowd.rb', line 5068

def getVersion()
    return "client " + CLIENT_VERSION + ", API v2, converter " + @helper.getConverterVersion()
end

#setClientUserAgent(agent) ⇒ Object



5105
5106
5107
5108
# File 'lib/pdfcrowd.rb', line 5105

def setClientUserAgent(agent)
    @helper.setUserAgent(agent)
    self
end

#setCropArea(x, y, width, height) ⇒ Object



5023
5024
5025
5026
5027
5028
5029
# File 'lib/pdfcrowd.rb', line 5023

def setCropArea(x, y, width, height)
    setCropAreaX(x)
    setCropAreaY(y)
    setCropAreaWidth(width)
    setCropAreaHeight(height)
    self
end

#setCropAreaHeight(height) ⇒ Object



5013
5014
5015
5016
5017
5018
5019
5020
# File 'lib/pdfcrowd.rb', line 5013

def setCropAreaHeight(height)
    if (!(Integer(height) >= 0))
        raise Error.new(Pdfcrowd.create_invalid_value_message(height, "setCropAreaHeight", "pdf-to-text", "Must be a positive integer or 0.", "set_crop_area_height"), 470);
    end
    
    @fields['crop_area_height'] = height
    self
end

#setCropAreaWidth(width) ⇒ Object



5003
5004
5005
5006
5007
5008
5009
5010
# File 'lib/pdfcrowd.rb', line 5003

def setCropAreaWidth(width)
    if (!(Integer(width) >= 0))
        raise Error.new(Pdfcrowd.create_invalid_value_message(width, "setCropAreaWidth", "pdf-to-text", "Must be a positive integer or 0.", "set_crop_area_width"), 470);
    end
    
    @fields['crop_area_width'] = width
    self
end

#setCropAreaX(x) ⇒ Object



4983
4984
4985
4986
4987
4988
4989
4990
# File 'lib/pdfcrowd.rb', line 4983

def setCropAreaX(x)
    if (!(Integer(x) >= 0))
        raise Error.new(Pdfcrowd.create_invalid_value_message(x, "setCropAreaX", "pdf-to-text", "Must be a positive integer or 0.", "set_crop_area_x"), 470);
    end
    
    @fields['crop_area_x'] = x
    self
end

#setCropAreaY(y) ⇒ Object



4993
4994
4995
4996
4997
4998
4999
5000
# File 'lib/pdfcrowd.rb', line 4993

def setCropAreaY(y)
    if (!(Integer(y) >= 0))
        raise Error.new(Pdfcrowd.create_invalid_value_message(y, "setCropAreaY", "pdf-to-text", "Must be a positive integer or 0.", "set_crop_area_y"), 470);
    end
    
    @fields['crop_area_y'] = y
    self
end

#setCustomPageBreak(page_break) ⇒ Object



4945
4946
4947
4948
# File 'lib/pdfcrowd.rb', line 4945

def setCustomPageBreak(page_break)
    @fields['custom_page_break'] = page_break
    self
end

#setDebugLog(value) ⇒ Object



5032
5033
5034
5035
# File 'lib/pdfcrowd.rb', line 5032

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

#setEol(eol) ⇒ Object



4925
4926
4927
4928
4929
4930
4931
4932
# File 'lib/pdfcrowd.rb', line 4925

def setEol(eol)
    unless /(?i)^(unix|dos|mac)$/.match(eol)
        raise Error.new(Pdfcrowd.create_invalid_value_message(eol, "setEol", "pdf-to-text", "Allowed values are unix, dos, mac.", "set_eol"), 470);
    end
    
    @fields['eol'] = eol
    self
end

#setHttpProxy(proxy) ⇒ Object



5079
5080
5081
5082
5083
5084
5085
5086
# File 'lib/pdfcrowd.rb', line 5079

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", "pdf-to-text", "The value must have format DOMAIN_OR_IP_ADDRESS:PORT.", "set_http_proxy"), 470);
    end
    
    @fields['http_proxy'] = proxy
    self
end

#setHttpsProxy(proxy) ⇒ Object



5089
5090
5091
5092
5093
5094
5095
5096
# File 'lib/pdfcrowd.rb', line 5089

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", "pdf-to-text", "The value must have format DOMAIN_OR_IP_ADDRESS:PORT.", "set_https_proxy"), 470);
    end
    
    @fields['https_proxy'] = proxy
    self
end

#setLineSpacingThreshold(threshold) ⇒ Object



4961
4962
4963
4964
4965
4966
4967
4968
# File 'lib/pdfcrowd.rb', line 4961

def setLineSpacingThreshold(threshold)
    unless /(?i)^0$|^[0-9]+%$/.match(threshold)
        raise Error.new(Pdfcrowd.create_invalid_value_message(threshold, "setLineSpacingThreshold", "pdf-to-text", "The value must be a positive integer percentage.", "set_line_spacing_threshold"), 470);
    end
    
    @fields['line_spacing_threshold'] = threshold
    self
end

#setNoLayout(value) ⇒ Object



4919
4920
4921
4922
# File 'lib/pdfcrowd.rb', line 4919

def setNoLayout(value)
    @fields['no_layout'] = value
    self
end

#setPageBreakMode(mode) ⇒ Object



4935
4936
4937
4938
4939
4940
4941
4942
# File 'lib/pdfcrowd.rb', line 4935

def setPageBreakMode(mode)
    unless /(?i)^(none|default|custom)$/.match(mode)
        raise Error.new(Pdfcrowd.create_invalid_value_message(mode, "setPageBreakMode", "pdf-to-text", "Allowed values are none, default, custom.", "set_page_break_mode"), 470);
    end
    
    @fields['page_break_mode'] = mode
    self
end

#setParagraphMode(mode) ⇒ Object



4951
4952
4953
4954
4955
4956
4957
4958
# File 'lib/pdfcrowd.rb', line 4951

def setParagraphMode(mode)
    unless /(?i)^(none|bounding-box|characters)$/.match(mode)
        raise Error.new(Pdfcrowd.create_invalid_value_message(mode, "setParagraphMode", "pdf-to-text", "Allowed values are none, bounding-box, characters.", "set_paragraph_mode"), 470);
    end
    
    @fields['paragraph_mode'] = mode
    self
end

#setPdfPassword(password) ⇒ Object



4903
4904
4905
4906
# File 'lib/pdfcrowd.rb', line 4903

def setPdfPassword(password)
    @fields['pdf_password'] = password
    self
end

#setPrintPageRange(pages) ⇒ Object



4909
4910
4911
4912
4913
4914
4915
4916
# File 'lib/pdfcrowd.rb', line 4909

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.create_invalid_value_message(pages, "setPrintPageRange", "pdf-to-text", "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



5117
5118
5119
5120
# File 'lib/pdfcrowd.rb', line 5117

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

#setRemoveEmptyLines(value) ⇒ Object



4977
4978
4979
4980
# File 'lib/pdfcrowd.rb', line 4977

def setRemoveEmptyLines(value)
    @fields['remove_empty_lines'] = value
    self
end

#setRemoveHyphenation(value) ⇒ Object



4971
4972
4973
4974
# File 'lib/pdfcrowd.rb', line 4971

def setRemoveHyphenation(value)
    @fields['remove_hyphenation'] = value
    self
end

#setRetryCount(count) ⇒ Object



5123
5124
5125
5126
# File 'lib/pdfcrowd.rb', line 5123

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

#setTag(tag) ⇒ Object



5073
5074
5075
5076
# File 'lib/pdfcrowd.rb', line 5073

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

#setUseHttp(value) ⇒ Object



5099
5100
5101
5102
# File 'lib/pdfcrowd.rb', line 5099

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

#setUserAgent(agent) ⇒ Object



5111
5112
5113
5114
# File 'lib/pdfcrowd.rb', line 5111

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