Created
June 9, 2015 18:54
-
-
Save illbzo1/c0bddf0946714bff00ec to your computer and use it in GitHub Desktop.
An example Node.js implementation for DocRaptor, that does not rely on any external dependencies.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var https = require("https"), | |
fs = require("fs"); | |
var payload = JSON.stringify({ | |
user_credentials: "YOUR_API_KEY_HERE", | |
doc: { | |
document_content: "<!DOCTYPE html><html><head><title>Javascript PDF Sample</title></head><body>Hello from Javascript!</body></html>", | |
name: "test.pdf", | |
document_type: "pdf", | |
test: true | |
} | |
}); | |
var req = https.request({ | |
hostname: "docraptor.com", | |
port: 443, | |
path: "/docs", | |
method: "POST", | |
headers: { | |
"Content-Type": "application/json" , | |
"Content-Length": payload.length | |
}, | |
}, function onResult(res){ | |
if(res.statusCode == 200){ | |
res.setEncoding("binary"); | |
var pdf = ""; | |
res.on("data", function onData(chunk){ | |
pdf += chunk; | |
}); | |
res.on("end", function onClose(){ | |
fs.writeFile("javascript_sample.pdf", pdf, "binary", function onComplete(err){ | |
if(err){ | |
throw err; | |
} | |
}); | |
}); | |
} else { | |
throw new Error("Document creation failed: " + res.statusCode); | |
} | |
}); | |
req.end(payload, "utf8"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment