Created
March 4, 2015 04:40
-
-
Save jiggak/21f02f888aabc6b87c47 to your computer and use it in GitHub Desktop.
Use markdowntopdf.com to create pdf from markdown
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 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