Skip to content

Instantly share code, notes, and snippets.

@jiggak
Created March 4, 2015 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jiggak/21f02f888aabc6b87c47 to your computer and use it in GitHub Desktop.
Save jiggak/21f02f888aabc6b87c47 to your computer and use it in GitHub Desktop.
Use markdowntopdf.com to create pdf from markdown
var request = require('request');
var path = require('path');
var fs = require('fs');
var args = process.argv.slice(2);
var input = args.shift();
if (input === undefined) {
console.log('missing input file argument');
process.exit(1);
}
if (!fs.existsSync(input)) {
console.log(input, 'not found');
process.exit(1);
}
var output = args.shift();
if (output === undefined) {
output = path.basename(input).replace(path.extname(input), '.pdf');
}
var url = 'http://www.markdowntopdf.com/';
var form = {
file: fs.createReadStream(input)
};
request.post({ url: url, formData: form}, function (error, response, body) {
if (response.statusCode === 200) {
var match = /\/(download\/[^"]+)"/.exec(body);
if (match) {
request.get(url + match[1]).pipe(fs.createWriteStream(output));
} else {
console.log('unable to find download link in response');
}
} else {
console.log('invalid response code', response.statusCode);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment