Class: Pdfcrowd::PdfToImageClient

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

Overview

Conversion from PDF to image.

Instance Method Summary collapse

Constructor Details

#initialize(user_name, api_key) ⇒ PdfToImageClient

Returns a new instance of PdfToImageClient.



5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
# File 'lib/pdfcrowd.rb', line 5066

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



5115
5116
5117
5118
5119
5120
5121
5122
# File 'lib/pdfcrowd.rb', line 5115

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



5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
# File 'lib/pdfcrowd.rb', line 5135

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-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



5125
5126
5127
5128
5129
5130
5131
5132
# File 'lib/pdfcrowd.rb', line 5125

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-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



5152
5153
5154
5155
# File 'lib/pdfcrowd.rb', line 5152

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

#convertRawDataToFile(data, file_path) ⇒ Object



5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
# File 'lib/pdfcrowd.rb', line 5164

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-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



5158
5159
5160
5161
# File 'lib/pdfcrowd.rb', line 5158

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

#convertStream(in_stream) ⇒ Object



5181
5182
5183
5184
# File 'lib/pdfcrowd.rb', line 5181

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

#convertStreamToFile(in_stream, file_path) ⇒ Object



5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
# File 'lib/pdfcrowd.rb', line 5193

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-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



5187
5188
5189
5190
# File 'lib/pdfcrowd.rb', line 5187

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

#convertUrl(url) ⇒ Object



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

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



5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
# File 'lib/pdfcrowd.rb', line 5098

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-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



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

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



5330
5331
5332
# File 'lib/pdfcrowd.rb', line 5330

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

#getDebugLogUrlObject



5320
5321
5322
# File 'lib/pdfcrowd.rb', line 5320

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

#getJobIdObject



5335
5336
5337
# File 'lib/pdfcrowd.rb', line 5335

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

#getOutputSizeObject



5345
5346
5347
# File 'lib/pdfcrowd.rb', line 5345

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

#getPageCountObject



5340
5341
5342
# File 'lib/pdfcrowd.rb', line 5340

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

#getRemainingCreditCountObject



5325
5326
5327
# File 'lib/pdfcrowd.rb', line 5325

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

#getVersionObject



5350
5351
5352
# File 'lib/pdfcrowd.rb', line 5350

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

#isZippedOutputObject



5242
5243
5244
# File 'lib/pdfcrowd.rb', line 5242

def isZippedOutput()
    @fields.fetch('force_zip', false) == true || getPageCount() > 1
end

#setClientUserAgent(agent) ⇒ Object



5387
5388
5389
5390
# File 'lib/pdfcrowd.rb', line 5387

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

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



5299
5300
5301
5302
5303
5304
5305
# File 'lib/pdfcrowd.rb', line 5299

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

#setCropAreaHeight(height) ⇒ Object



5289
5290
5291
5292
5293
5294
5295
5296
# File 'lib/pdfcrowd.rb', line 5289

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

#setCropAreaWidth(width) ⇒ Object



5279
5280
5281
5282
5283
5284
5285
5286
# File 'lib/pdfcrowd.rb', line 5279

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

#setCropAreaX(x) ⇒ Object



5259
5260
5261
5262
5263
5264
5265
5266
# File 'lib/pdfcrowd.rb', line 5259

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

#setCropAreaY(y) ⇒ Object



5269
5270
5271
5272
5273
5274
5275
5276
# File 'lib/pdfcrowd.rb', line 5269

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

#setDebugLog(value) ⇒ Object



5314
5315
5316
5317
# File 'lib/pdfcrowd.rb', line 5314

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

#setDpi(dpi) ⇒ Object



5236
5237
5238
5239
# File 'lib/pdfcrowd.rb', line 5236

def setDpi(dpi)
    @fields['dpi'] = dpi
    self
end

#setForceZip(value) ⇒ Object



5247
5248
5249
5250
# File 'lib/pdfcrowd.rb', line 5247

def setForceZip(value)
    @fields['force_zip'] = value
    self
end

#setHttpProxy(proxy) ⇒ Object



5361
5362
5363
5364
5365
5366
5367
5368
# File 'lib/pdfcrowd.rb', line 5361

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-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



5371
5372
5373
5374
5375
5376
5377
5378
# File 'lib/pdfcrowd.rb', line 5371

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-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



5210
5211
5212
5213
5214
5215
5216
5217
# File 'lib/pdfcrowd.rb', line 5210

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.create_invalid_value_message(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



5220
5221
5222
5223
# File 'lib/pdfcrowd.rb', line 5220

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

#setPrintPageRange(pages) ⇒ Object



5226
5227
5228
5229
5230
5231
5232
5233
# File 'lib/pdfcrowd.rb', line 5226

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-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



5399
5400
5401
5402
# File 'lib/pdfcrowd.rb', line 5399

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

#setRetryCount(count) ⇒ Object



5405
5406
5407
5408
# File 'lib/pdfcrowd.rb', line 5405

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

#setTag(tag) ⇒ Object



5355
5356
5357
5358
# File 'lib/pdfcrowd.rb', line 5355

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

#setUseCropbox(value) ⇒ Object



5253
5254
5255
5256
# File 'lib/pdfcrowd.rb', line 5253

def setUseCropbox(value)
    @fields['use_cropbox'] = value
    self
end

#setUseGrayscale(value) ⇒ Object



5308
5309
5310
5311
# File 'lib/pdfcrowd.rb', line 5308

def setUseGrayscale(value)
    @fields['use_grayscale'] = value
    self
end

#setUseHttp(value) ⇒ Object



5381
5382
5383
5384
# File 'lib/pdfcrowd.rb', line 5381

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

#setUserAgent(agent) ⇒ Object



5393
5394
5395
5396
# File 'lib/pdfcrowd.rb', line 5393

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