Permalink
Browse files

added convertURI() and convertFile()

  • Loading branch information...
1 parent 245f023 commit 583946c2766806f460c55a7cde264fea3e1dfddd @pdfcrowd committed Apr 28, 2011
Showing with 17 additions and 9 deletions.
  1. +17 −9 lib/pdfcrowd.js
View
@@ -1,5 +1,6 @@
-var http = require('http');
+var http = require('http')
var querystring = require('querystring');
+var fs = require('fs');
var Pdfcrowd = function(username, apikey) {
@@ -33,11 +34,20 @@ Pdfcrowd.prototype.on = function(event, callback) {
};
Pdfcrowd.prototype.convertHtml = function(html, options) {
- convertInternal(this, html, options,
- '/api/pdf/convert/html/',
- 'application/x-www-form-urlencoded');
+ convertInternal(this, html, options, '/api/pdf/convert/html/');
}
+Pdfcrowd.prototype.convertURI = function(uri, options) {
+ convertInternal(this, uri, options, '/api/pdf/convert/uri/');
+}
+
+Pdfcrowd.prototype.convertFile = function(fname, options) {
+ var that = this;
+ fs.readFile(fname, function(err, data) {
+ if (err) throw err;
+ convertInternal(that, data.toString(), options, '/api/pdf/convert/html/');
+ });
+}
@@ -49,25 +59,23 @@ module.exports = Pdfcrowd;
// private
-var convertInternal = function(that, src, options, endpoint, content_type) {
+var convertInternal = function(that, src, options, endpoint) {
// form POST data
var postData = clone_object(options);
postData['src'] = src;
postData['username'] = that.username;
postData['key'] = that.apikey;
postData = querystring.stringify(postData);
- console.log("%d - %s", postData.length, postData);
+ //console.log("%d - %s", postData.length, postData);
// http options
var httpOptions = clone_object(that.httpOptions);
httpOptions['path'] = endpoint;
httpOptions['headers'] = { 'content-length': postData.length,
- 'content-type': content_type };
+ 'content-type': 'application/x-www-form-urlencoded' };
var req = http.request(httpOptions, function(res) {
- console.log('HEADERS: ' + JSON.stringify(res.headers));
-
if (res.statusCode < 300) {
that.callbacks['pdf'](res);
}

0 comments on commit 583946c

Please sign in to comment.