Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added saveToFile and sendHttpResponse helpers

  • Loading branch information...
commit db553e8316ad4e752db6ee2914e761d47243b83c 1 parent be70d40
@pdfcrowd authored
Showing with 55 additions and 18 deletions.
  1. +38 −2 lib/pdfcrowd.js
  2. +17 −16 test/client_test.js
View
40 lib/pdfcrowd.js
@@ -4,7 +4,6 @@ var fs = require('fs');
var Pdfcrowd = function(username, apikey) {
-
if (!username)
throw new Error('Missing username.');
@@ -36,7 +35,44 @@ Pdfcrowd.prototype.convertFile = function(fname, callbacks, options) {
}
-module.exports = Pdfcrowd;
+var saveToFile = function(fname) {
+ return {
+ pdf: function(rstream) {
+ wstream = fs.createWriteStream(fname);
+ rstream.pipe(wstream);
+ },
+ error: function(errMessage, statusCode) { console.log("ERROR: " + errMessage); },
+ end: function() {},
+ };
+}
+
+
+var sendHttpResponse = function(response, disposition) {
+ disposition = disposition || "attachment";
+ return {
+ pdf: function(rstream) {
+ response.setHeader("Content-Type", "application/pdf");
+ response.setHeader("Cache-Control", "no-cache");
+ response.setHeader("Accept-Ranges", "none");
+ response.setHeader("Content-Disposition", disposition + "; filename=\"generated.pdf\"");
+ rstream.pipe(response);
+ },
+ error: function(errMessage, statusCode) {
+ response.setHeader("Content-Type", "text/plain");
+ response.end('ERROR: ' + errMessage);
+ },
+ end: function() {},
+ };
+}
+
+
+
+
+module.exports = {
+ Pdfcrowd: Pdfcrowd,
+ saveToFile: saveToFile,
+ sendHttpResponse: sendHttpResponse,
+}
View
33 test/client_test.js
@@ -1,23 +1,24 @@
-var Pdfcrowd = require('../lib/pdfcrowd');
+var pdf = require('../lib/pdfcrowd');
var fs = require('fs');
credentials = require('./config').Credentials;
-function saveToFile(fname) {
- return {
- pdf: function(rstream) {
- console.log("start");
- wstream = fs.createWriteStream(fname);
- rstream.pipe(wstream);
- },
- end: function() { console.log("end"); },
- error: function(errMessage, statusCode) { console.log("ERROR: " + errMessage); },
- };
-}
-myPdfcrowd = new Pdfcrowd(credentials.username,
+myPdfcrowd = new pdf.Pdfcrowd(credentials.username,
credentials.apikey);
-myPdfcrowd.convertHtml("raw code", saveToFile("html.pdf"))
-myPdfcrowd.convertURI("http://example.com", saveToFile("url.pdf"))
-myPdfcrowd.convertFile("sample.html", saveToFile("file.pdf"))
+myPdfcrowd.convertHtml("raw code", pdf.saveToFile("html.pdf"));
+myPdfcrowd.convertURI("http://example.com", pdf.saveToFile("url.pdf"));
+myPdfcrowd.convertFile("sample.html", pdf.saveToFile("file.pdf"));
+myPdfcrowd.convertHtml(
+ 'footer example',
+ pdf.saveToFile("footer.pdf"),
+ {
+ width: "11in",
+ height: "8.5in",
+ vmargin: ".4in",
+ footer_html: '<div style=text-align:center;font-size:smaller;color:maroon;">\
+ Page %p out of %n\
+ </div>'
+ });
+
Please sign in to comment.
Something went wrong with that request. Please try again.