Last active
August 30, 2017 13:52
-
-
Save colinappnovation/ab13906ac1b1919f3032fe9da6663954 to your computer and use it in GitHub Desktop.
Docraptor Node implementation command line helper
This file contains hidden or 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
#!/usr/bin/env node | |
// npm install restler | |
var sys = require("util"), | |
fs = require("fs"), | |
rest = require("restler"), | |
p = console.log, | |
chalk = require("chalk"), | |
program = require("commander") | |
mdls = require("mdls"), | |
exec = require('child_process').exec | |
wordcount = require("html-word-count"); | |
program | |
.version('0.1.0') | |
.option('-f, --file [file]', 'File source of HTML') | |
.option('-o, --output [output]', 'Output filename') | |
.parse(process.argv); | |
if (typeof program.file !== 'undefined' || typeof program.output !== 'undefined') { | |
var contents = fs.readFileSync(program.file.toString(), 'utf8'); | |
p(chalk.white.bgBlue('Read the contents of HTML file.')); | |
p(chalk.white.bgBlue('No. of words in HTML content: ' + wordcount(contents))); | |
rest.postJson("https://docraptor.com/docs", { | |
user_credentials: "YOUR_API_KEY_HERE", | |
doc: { | |
document_content: contents, | |
type: "pdf", | |
test: true | |
} | |
}).on("success", function(data, response) { | |
// Write the file out to the filesystem. | |
fs.writeFile(program.output, response.raw, "binary", function(err) { | |
if (err) throw err; | |
}); | |
// Grab the PDF pages from the file which has been generated. | |
exec("mdls -name kMDItemNumberOfPages ./" + program.output, function(error, stdout, stderr) { | |
if (stdout.match("kMDItemNumberOfPages")) { | |
p(chalk.green(stdout)); | |
} | |
}); | |
// Inform the user of successful creation of document. | |
p(chalk.white.bgGreen("Success Creating Document")); | |
}).on("fail", function(data, response) { | |
p(chalk.red("Failure Creating Document")); | |
p(data); | |
}).on("error", function(err, response) { | |
p(chalk.blue.bgRed.bold("Error Creating Document")); | |
p(err); | |
}); | |
} else { | |
p(chalk.blue.bgRed.bold('Please provide params for input file and output filename')); | |
process.exit(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment