Class: Pdfcrowd::Client

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

Overview

Pdfcrowd API client.

Instance Method Summary collapse

Constructor Details

#initialize(username, apikey, hostname = nil) ⇒ Client

Client constructor.

username – your username at Pdfcrowd apikey – your API key



80
81
82
83
84
85
86
87
88
89
# 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

Instance Method Details

#convertFile(fpath, outstream = nil) ⇒ Object

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.


120
121
122
# File 'lib/pdfcrowd.rb', line 120

def convertFile(fpath, outstream=nil)
    return post_multipart(fpath, outstream)
end

#convertHtml(content, outstream = nil) ⇒ Object

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.


109
110
111
# 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) ⇒ Object

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.


98
99
100
# 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) ⇒ Object



212
213
214
# File 'lib/pdfcrowd.rb', line 212

def enableBackgrounds(value=true)
    @fields['no_backgrounds'] = (not value)
end


224
225
226
# File 'lib/pdfcrowd.rb', line 224

def enableHyperlinks(value=true)
    @fields['no_hyperlinks'] = (not value)
end

#enableImages(value = true) ⇒ Object



208
209
210
# File 'lib/pdfcrowd.rb', line 208

def enableImages(value=true)
    @fields['no_images'] = (not value)
end

#enableJavaScript(value = true) ⇒ Object



220
221
222
# File 'lib/pdfcrowd.rb', line 220

def enableJavaScript(value=true)
    @fields['no_javascript'] = (not value)
end

#enablePdfcrowdLogo(value = true) ⇒ Object



240
241
242
# File 'lib/pdfcrowd.rb', line 240

def (value=true)
    @fields['pdfcrowd_logo'] = value
end

#numTokensObject

Returns the number of available conversion tokens.



127
128
129
130
# File 'lib/pdfcrowd.rb', line 127

def numTokens()
  uri = '/api/user/%s/tokens/' % @fields['username']
  return Integer(call_api_urlencoded(uri))
end

#setApiKey(key) ⇒ Object



141
142
143
# File 'lib/pdfcrowd.rb', line 141

def setApiKey(key)
    @fields['key'] = key
end

#setAuthor(value) ⇒ Object



254
255
256
# File 'lib/pdfcrowd.rb', line 254

def setAuthor(value)
    @fields['author'] = value
end

#setDefaultTextEncoding(value) ⇒ Object



228
229
230
# File 'lib/pdfcrowd.rb', line 228

def setDefaultTextEncoding(value)
    @fields['text_encoding'] = value
end

#setEncrypted(val = true) ⇒ Object



169
170
171
# File 'lib/pdfcrowd.rb', line 169

def setEncrypted(val=true)
    @fields['encrypted'] = val
end

#setFailOnNon200(value) ⇒ Object



258
259
260
# File 'lib/pdfcrowd.rb', line 258

def setFailOnNon200(value)
    @fields['fail_on_non200'] = value
end

#setFooterHtml(value) ⇒ Object



266
267
268
# File 'lib/pdfcrowd.rb', line 266

def setFooterHtml(value)
    @fields['footer_html'] = value
end

#setFooterText(value) ⇒ Object



204
205
206
# File 'lib/pdfcrowd.rb', line 204

def setFooterText(value)
    @fields['footer_text'] = value
end

#setFooterUrl(value) ⇒ Object



270
271
272
# File 'lib/pdfcrowd.rb', line 270

def setFooterUrl(value)
    @fields['footer_url'] = value
end

#setHeaderFooterPageExcludeList(value) ⇒ Object



294
295
296
# File 'lib/pdfcrowd.rb', line 294

def setHeaderFooterPageExcludeList(value)
    @fields['header_footer_page_exclude_list'] = value
end

#setHeaderHtml(value) ⇒ Object



274
275
276
# File 'lib/pdfcrowd.rb', line 274

def setHeaderHtml(value)
    @fields['header_html'] = value
end

#setHeaderUrl(value) ⇒ Object



278
279
280
# File 'lib/pdfcrowd.rb', line 278

def setHeaderUrl(value)
    @fields['header_url'] = value
end

#setHorizontalMargin(value) ⇒ Object



153
154
155
# File 'lib/pdfcrowd.rb', line 153

def setHorizontalMargin(value)
    @fields['margin_right'] = @fields['margin_left'] = value.to_s()
end

#setHtmlZoom(value) ⇒ Object



216
217
218
# File 'lib/pdfcrowd.rb', line 216

def setHtmlZoom(value)
    @fields['html_zoom'] = value
end

#setInitialPdfExactZoom(value) ⇒ Object



249
250
251
252
# File 'lib/pdfcrowd.rb', line 249

def setInitialPdfExactZoom(value)
    @fields['initial_pdf_zoom_type'] = 4
    @fields['initial_pdf_zoom'] = value
end

#setInitialPdfZoomType(value) ⇒ Object



244
245
246
247
# File 'lib/pdfcrowd.rb', line 244

def setInitialPdfZoomType(value)
    assert_pdfcrowd { value>0 and value<=3 }
    @fields['initial_pdf_zoom_type'] = value
end

#setMaxPages(value) ⇒ Object



236
237
238
# File 'lib/pdfcrowd.rb', line 236

def setMaxPages(value)
    @fields['max_pages'] = value
end

#setNoCopy(val = true) ⇒ Object



189
190
191
# File 'lib/pdfcrowd.rb', line 189

def setNoCopy(val=true)
    @fields['no_copy'] = val
end

#setNoModify(val = true) ⇒ Object



185
186
187
# File 'lib/pdfcrowd.rb', line 185

def setNoModify(val=true)
    @fields['no_modify'] = val
end

#setNoPrint(val = true) ⇒ Object



181
182
183
# File 'lib/pdfcrowd.rb', line 181

def setNoPrint(val=true)
    @fields['no_print'] = val
end

#setOwnerPassword(pwd) ⇒ Object



177
178
179
# File 'lib/pdfcrowd.rb', line 177

def setOwnerPassword(pwd)
    @fields['owner_pwd'] = pwd
end

#setPageBackgroundColor(value) ⇒ Object



282
283
284
# File 'lib/pdfcrowd.rb', line 282

def setPageBackgroundColor(value)
    @fields['page_background_color'] = value
end

#setPageHeight(value) ⇒ Object



149
150
151
# File 'lib/pdfcrowd.rb', line 149

def setPageHeight(value)
    @fields['height'] = value
end

#setPageLayout(value) ⇒ Object



193
194
195
196
# File 'lib/pdfcrowd.rb', line 193

def setPageLayout(value)
    assert_pdfcrowd { value > 0 and value <= 3 }
    @fields['page_layout'] = value
end

#setPageMargins(top, right, bottom, left) ⇒ Object



161
162
163
164
165
166
# 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) ⇒ Object



198
199
200
201
# File 'lib/pdfcrowd.rb', line 198

def setPageMode(value)
    assert_pdfcrowd { value > 0 and value <= 3 }
    @fields['page_mode'] = value
end

#setPageNumberingOffset(value) ⇒ Object



290
291
292
# File 'lib/pdfcrowd.rb', line 290

def setPageNumberingOffset(value)
    @fields['page_numbering_offset'] = value
end

#setPageWidth(value) ⇒ Object



145
146
147
# File 'lib/pdfcrowd.rb', line 145

def setPageWidth(value)
    @fields['width'] = value
end

#setPdfScalingFactor(value) ⇒ Object



262
263
264
# File 'lib/pdfcrowd.rb', line 262

def setPdfScalingFactor(value)
    @fields['pdf_scaling_factor'] = value
end

#setTransparentBackground(value = true) ⇒ Object



286
287
288
# File 'lib/pdfcrowd.rb', line 286

def setTransparentBackground(value=true)
    @fields['transparent_background'] = value
end

#setUsername(username) ⇒ Object



137
138
139
# File 'lib/pdfcrowd.rb', line 137

def setUsername(username)
    @fields['username'] = username
end

#setUserPassword(pwd) ⇒ Object



173
174
175
# File 'lib/pdfcrowd.rb', line 173

def setUserPassword(pwd)
    @fields['user_pwd'] = pwd
end

#setVerticalMargin(value) ⇒ Object



157
158
159
# 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) ⇒ Object



298
299
300
301
302
# 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) ⇒ Object



308
309
310
# File 'lib/pdfcrowd.rb', line 308

def setWatermarkInBackground(val=True)
    @fields["watermark_in_background"] = val
end

#setWatermarkRotation(angle) ⇒ Object



304
305
306
# File 'lib/pdfcrowd.rb', line 304

def setWatermarkRotation(angle)
    @fields["watermark_rotation"] = angle
end

#usePrintMedia(value = true) ⇒ Object



232
233
234
# File 'lib/pdfcrowd.rb', line 232

def usePrintMedia(value=true)
    @fields['use_print_media'] = value
end

#useSSL(use_ssl) ⇒ Object



132
133
134
135
# 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