class Pdfcrowd::Client
Pdfcrowd
API client.
Public Class Methods
new(username, apikey, hostname=nil)
click to toggle source
Client
constructor.
username – your username at Pdfcrowd
apikey – your API key
# File lib/pdfcrowd.rb, line 80 def initialize(username, apikey, hostname=nil) useSSL(false) @fields = { 'username' => username, 'key' => apikey, 'html_zoom' => 200, 'pdf_scaling_factor' => 1 } @hostname = hostname || $api_hostname; end
Public Instance Methods
convertFile(fpath, outstream=nil)
click to toggle source
Converts an html file.
fpath – a path to an html file outstream – an object having method 'write(data)'; if nil then the
return value is a string containing the PDF.
# File lib/pdfcrowd.rb, line 120 def convertFile(fpath, outstream=nil) return post_multipart(fpath, outstream) end
convertHtml(content, outstream=nil)
click to toggle source
Converts an in-memory html document.
content – a string containing an html document outstream – an object having method 'write(data)'; if nil then the
return value is a string containing the PDF.
# File lib/pdfcrowd.rb, line 109 def convertHtml(content, outstream=nil) return call_api_urlencoded('/api/pdf/convert/html/', content, outstream) end
convertURI(uri, outstream=nil)
click to toggle source
Converts a web page.
uri – a web page URL outstream – an object having method 'write(data)'; if nil then the
return value is a string containing the PDF.
# File lib/pdfcrowd.rb, line 98 def convertURI(uri, outstream=nil) return call_api_urlencoded('/api/pdf/convert/uri/', uri, outstream) end
enableBackgrounds(value=true)
click to toggle source
# File lib/pdfcrowd.rb, line 212 def enableBackgrounds(value=true) @fields['no_backgrounds'] = (not value) end
enableHyperlinks(value=true)
click to toggle source
# File lib/pdfcrowd.rb, line 224 def enableHyperlinks(value=true) @fields['no_hyperlinks'] = (not value) end
enableImages(value=true)
click to toggle source
# File lib/pdfcrowd.rb, line 208 def enableImages(value=true) @fields['no_images'] = (not value) end
enableJavaScript(value=true)
click to toggle source
# File lib/pdfcrowd.rb, line 220 def enableJavaScript(value=true) @fields['no_javascript'] = (not value) end
enablePdfcrowdLogo(value=true)
click to toggle source
# File lib/pdfcrowd.rb, line 240 def enablePdfcrowdLogo(value=true) @fields['pdfcrowd_logo'] = value end
numTokens()
click to toggle source
Returns the number of available conversion tokens.
# File lib/pdfcrowd.rb, line 127 def numTokens() uri = '/api/user/%s/tokens/' % @fields['username'] return Integer(call_api_urlencoded(uri)) end
setApiKey(key)
click to toggle source
# File lib/pdfcrowd.rb, line 141 def setApiKey(key) @fields['key'] = key end
setAuthor(value)
click to toggle source
# File lib/pdfcrowd.rb, line 254 def setAuthor(value) @fields['author'] = value end
setDefaultTextEncoding(value)
click to toggle source
# File lib/pdfcrowd.rb, line 228 def setDefaultTextEncoding(value) @fields['text_encoding'] = value end
setEncrypted(val=true)
click to toggle source
# File lib/pdfcrowd.rb, line 169 def setEncrypted(val=true) @fields['encrypted'] = val end
setFailOnNon200(value)
click to toggle source
# File lib/pdfcrowd.rb, line 258 def setFailOnNon200(value) @fields['fail_on_non200'] = value end
setHeaderHtml(value)
click to toggle source
# File lib/pdfcrowd.rb, line 274 def setHeaderHtml(value) @fields['header_html'] = value end
setHeaderUrl(value)
click to toggle source
# File lib/pdfcrowd.rb, line 278 def setHeaderUrl(value) @fields['header_url'] = value end
setHorizontalMargin(value)
click to toggle source
# File lib/pdfcrowd.rb, line 153 def setHorizontalMargin(value) @fields['margin_right'] = @fields['margin_left'] = value.to_s() end
setHtmlZoom(value)
click to toggle source
# File lib/pdfcrowd.rb, line 216 def setHtmlZoom(value) @fields['html_zoom'] = value end
setInitialPdfExactZoom(value)
click to toggle source
# File lib/pdfcrowd.rb, line 249 def setInitialPdfExactZoom(value) @fields['initial_pdf_zoom_type'] = 4 @fields['initial_pdf_zoom'] = value end
setInitialPdfZoomType(value)
click to toggle source
# File lib/pdfcrowd.rb, line 244 def setInitialPdfZoomType(value) assert { value>0 and value<=3 } @fields['initial_pdf_zoom_type'] = value end
setMaxPages(value)
click to toggle source
# File lib/pdfcrowd.rb, line 236 def setMaxPages(value) @fields['max_pages'] = value end
setNoCopy(val=true)
click to toggle source
# File lib/pdfcrowd.rb, line 189 def setNoCopy(val=true) @fields['no_copy'] = val end
setNoModify(val=true)
click to toggle source
# File lib/pdfcrowd.rb, line 185 def setNoModify(val=true) @fields['no_modify'] = val end
setNoPrint(val=true)
click to toggle source
# File lib/pdfcrowd.rb, line 181 def setNoPrint(val=true) @fields['no_print'] = val end
setOwnerPassword(pwd)
click to toggle source
# File lib/pdfcrowd.rb, line 177 def setOwnerPassword(pwd) @fields['owner_pwd'] = pwd end
setPageBackgroundColor(value)
click to toggle source
# File lib/pdfcrowd.rb, line 282 def setPageBackgroundColor(value) @fields['page_background_color'] = value end
setPageHeight(value)
click to toggle source
# File lib/pdfcrowd.rb, line 149 def setPageHeight(value) @fields['height'] = value end
setPageLayout(value)
click to toggle source
# File lib/pdfcrowd.rb, line 193 def setPageLayout(value) assert { value > 0 and value <= 3 } @fields['page_layout'] = value end
setPageMargins(top, right, bottom, left)
click to toggle source
# File lib/pdfcrowd.rb, line 161 def setPageMargins(top, right, bottom, left) @fields['margin_top'] = top.to_s() @fields['margin_right'] = right.to_s() @fields['margin_bottom'] = bottom.to_s() @fields['margin_left'] = left.to_s() end
setPageMode(value)
click to toggle source
# File lib/pdfcrowd.rb, line 198 def setPageMode(value) assert { value > 0 and value <= 3 } @fields['page_mode'] = value end
setPageNumberingOffset(value)
click to toggle source
# File lib/pdfcrowd.rb, line 290 def setPageNumberingOffset(value) @fields['page_numbering_offset'] = value end
setPageWidth(value)
click to toggle source
# File lib/pdfcrowd.rb, line 145 def setPageWidth(value) @fields['width'] = value end
setPdfScalingFactor(value)
click to toggle source
# File lib/pdfcrowd.rb, line 262 def setPdfScalingFactor(value) @fields['pdf_scaling_factor'] = value end
setTransparentBackground(value=true)
click to toggle source
# File lib/pdfcrowd.rb, line 286 def setTransparentBackground(value=true) @fields['transparent_background'] = value end
setUserPassword(pwd)
click to toggle source
# File lib/pdfcrowd.rb, line 173 def setUserPassword(pwd) @fields['user_pwd'] = pwd end
setUsername(username)
click to toggle source
# File lib/pdfcrowd.rb, line 137 def setUsername(username) @fields['username'] = username end
setVerticalMargin(value)
click to toggle source
# File lib/pdfcrowd.rb, line 157 def setVerticalMargin(value) @fields['margin_top'] = @fields['margin_bottom'] = value.to_s() end
setWatermark(url, offset_x=0, offset_y=0)
click to toggle source
# File lib/pdfcrowd.rb, line 298 def setWatermark(url, offset_x=0, offset_y=0) @fields["watermark_url"] = url @fields["watermark_offset_x"] = offset_x @fields["watermark_offset_y"] = offset_y end
setWatermarkInBackground(val=True)
click to toggle source
# File lib/pdfcrowd.rb, line 308 def setWatermarkInBackground(val=True) @fields["watermark_in_background"] = val end
setWatermarkRotation(angle)
click to toggle source
# File lib/pdfcrowd.rb, line 304 def setWatermarkRotation(angle) @fields["watermark_rotation"] = angle end
usePrintMedia(value=true)
click to toggle source
# File lib/pdfcrowd.rb, line 232 def usePrintMedia(value=true) @fields['use_print_media'] = value end
useSSL(use_ssl)
click to toggle source
# File lib/pdfcrowd.rb, line 132 def useSSL(use_ssl) @use_ssl = use_ssl @api_uri = use_ssl ? HTTPS_API_URI : HTTP_API_URI end
Private Instance Methods
call_api(request, out_stream)
click to toggle source
# File lib/pdfcrowd.rb, line 342 def call_api(request, out_stream) http = create_http_obj() begin http.start {|conn| conn.request(request) {|response| case response when Net::HTTPSuccess if out_stream response.read_body do |chunk| out_stream.write(chunk) end else return response.body end else raise Error.new(response.body, response.code) end } } rescue SystemCallError => why raise Error.new("#{why}\n") end end
call_api_urlencoded(path, src=nil, out_stream=nil)
click to toggle source
# File lib/pdfcrowd.rb, line 335 def call_api_urlencoded(path, src=nil, out_stream=nil) request = Net::HTTP::Post.new(path) request.set_form_data(rename_post_data({'src' => src})) return call_api(request, out_stream) end
create_http_obj()
click to toggle source
Private stuff
# File lib/pdfcrowd.rb, line 321 def create_http_obj() if @use_ssl require 'net/https' #apt-get install libopenssl-ruby http = Net::HTTP.new(@hostname, $api_https_port) # OpenSSL::SSL::VERIFY_PEER fails here: # ... certificate verify failed ... http.verify_mode = OpenSSL::SSL::VERIFY_NONE http.use_ssl = @use_ssl else http = Net::HTTP.new(@hostname, $api_http_port) end return http end
encode_multipart_post_data(filename)
click to toggle source
# File lib/pdfcrowd.rb, line 373 def encode_multipart_post_data(filename) boundary = '----------ThIs_Is_tHe_bOUnDary_$' body = [] for field, value in @fields body << '--' + boundary << 'Content-Disposition: form-data; name="%s"' % field << '' << value.to_s if value end # filename body << '--' + boundary body << 'Content-Disposition: form-data; name="src"; filename="%s"' % filename mime_type = 'application/octet-stream' body << 'Content-Type: ' + mime_type body << '' body << open(filename).read() # finalize body << '--' + boundary + '--' body << '' body = body.join("\r\n") content_type = 'multipart/form-data; boundary=%s' % boundary return content_type, body end
post_multipart(fpath, out_stream)
click to toggle source
# File lib/pdfcrowd.rb, line 394 def post_multipart(fpath, out_stream) req = Net::HTTP::Post.new('/api/pdf/convert/html/') req.content_type, req.body = encode_multipart_post_data(fpath) return call_api(req, out_stream) end
rename_post_data(extra_data={})
click to toggle source
# File lib/pdfcrowd.rb, line 366 def rename_post_data(extra_data={}) result = {} extra_data.each { |key, val| result[key] = val if val } @fields.each { |key, val| result[key] = val if val } result end