Skip to content

Instantly share code, notes, and snippets.

@illbzo1
Created June 9, 2015 18:54
Show Gist options
  • Save illbzo1/c0bddf0946714bff00ec to your computer and use it in GitHub Desktop.
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.
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