Permalink
Browse files

initial code

  • Loading branch information...
1 parent 9b0c9aa commit ee5029f1b4070b199296c4d02f97337e831386fd @pdfcrowd committed Apr 27, 2011
Showing with 101 additions and 0 deletions.
  1. +80 −0 lib/pdfcrowd.js
  2. +21 −0 package.json
View
@@ -0,0 +1,80 @@
+var http = require('http');
+var querystring = require('querystring');
+
+var Pdfcrowd = function(username, apikey) {
+ this.username = username;
+ this.apikey = apikey;
+
+ this.httpOptions = {
+ host: 'pdfcrowd.com',
+ port: 80,
+ // path: '/api/pdf/convert/html/',
+ method: 'POST',
+ // headers: { 'content-length': postData.length,
+ // 'content-type': 'application/x-www-form-urlencoded' }
+ };
+};
+
+var clone_object = function(obj) {
+ var prop, cloned = {};
+ for (prop in obj) {
+ if (typeof obj !== 'function') {
+ cloned[prop] = obj[prop];
+ }
+ }
+ return cloned;
+};
+
+
+Pdfcrowd.prototype.convertHtml = function(server_res, html, options) {
+
+ // form POST data
+ var postData = clone_object(options);
+ postData['src'] = html;
+ postData['username'] = this.username;
+ postData['key'] = this.apikey;
+ postData = querystring.stringify(postData);
+ console.log("%d - %s", postData.length, postData);
+
+ // http options
+ var httpOptions = clone_object(this.httpOptions);
+ httpOptions['path'] = '/api/pdf/convert/html/';
+ httpOptions['headers'] = { 'content-length': postData.length,
+ 'content-type': 'application/x-www-form-urlencoded' };
+
+
+ var req = http.request(httpOptions, function(res) {
+ console.log('STATUS: ' + res.statusCode);
+ console.log('HEADERS: ' + JSON.stringify(res.headers));
+
+ res.on('end', function () {
+ server_res.end();
+ });
+
+ if (res.statusCode < 299) {
+ server_res.setHeader("Content-Type", "application/pdf");
+ server_res.setHeader("cache-control", "no-cache");
+ server_res.setHeader("accept-ranges", "none");
+ server_res.setHeader("content-disposition", "attachment; filename=\"generated.pdf\"");
+ res.on('data', function (chunk) {
+ server_res.write(chunk);
+ });
+ }
+ else {
+ server_res.write("some error");
+ server_res.end();
+ }
+
+ });
+
+ req.on('error', function(res) {
+ console.log('STATUS: ' + res.statusCode);
+ console.log('** ' + res);
+ server_res.end('FATALek!\n');
+ });
+
+ req.write(postData);
+ req.end();
+}
+
+module.exports = Pdfcrowd;
View
@@ -0,0 +1,21 @@
+{
+ "name": "pdfcrowd",
+ "version": "1.0.0b",
+ "repository": {
+ "type": "git",
+ "url": "git@github.com:pdfcrowd/node-pdfcrowd.git"
+ },
+ "directories": {
+ "lib": "lib"
+ },
+ "engines": {
+ "node": "*"
+ },
+ "files": [
+ ""
+ ],
+ "description": "A wrapper for the Pdfcrowd API. It allows you to convert web pages or raw HTML code to PDF.",
+ "homepage": "https://github.com/pdfcrowd/node-pdfcrowd",
+ "author": "Pdfcrowd Dev <info+github@pdfcrowd.com> (http://pdfcrowd.com/html-to-pdf-api/)",
+ "main": "./lib/pdfcrowd"
+}

0 comments on commit ee5029f

Please sign in to comment.