Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Node server to take Gingko Tex output into PDF
var TreeId = "your_tree_id_here";
var Filename = "output_filename";
var http =require('http');
var fs = require('fs');
var request = require('request');
var exec = require('child_process').exec;
var header = fs.readFileSync('parts/header.tex', 'utf8');
var footer = fs.readFileSync('parts/footer.tex', 'utf8');
var replacements = { '\\\\begin\\{center\\}\\\\rule\\{3in\\}\\{0\\.4pt\}\\\\end\\{center\\}': '\\hr', '\\\\section\{([^\}]*)\}\\\\label\{([^\}]*)\}\\n\\n([^\.]*[\\.\\\'\\\'\\?\\!]*)\\s': '\\finishchapter\n\\section{$1}\\label{$2}\n\\setupchapter\n\\newthought{$3} ' };
http.createServer(function (req, res) { request.get({ url:""+TreeId+".tex?column=3", encoding: 'utf-8', headers: { 'User-Agent': 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11', 'Cookie': "connect.sid="+LoginToken, 'Accept': '/', 'Connection': 'keep-alive' } }, function(error, response, body){ var keys = Object.keys(replacements); for(var k in keys) { var regex = new RegExp(keys[k], 'g'); body = body.replace(regex, replacements[keys[k]]); } var tex = header + body + footer; fs.writeFile("parts/"+Filename+".tex", tex, function(err) { exec('cd parts; pdflatex '+Filename+'.tex', function (error, stdout, stderr) { fs.readFile('parts/'+Filename+'.pdf',function(error,data){ res.writeHead(200, {"Content-Type": "application/pdf"}); res.write(data); res.end(); }); }); }); }) }).listen(80, function(){ console.log('ready'); });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment