make pdfs

This commit is contained in:
Thomas Ruoff
2017-02-16 00:47:52 +01:00
parent 7668fc3837
commit d806327bf9
5 changed files with 131 additions and 68 deletions

49
renderer.js Normal file
View File

@@ -0,0 +1,49 @@
const fs = require('fs');
const path = require('path');
const spawn = require('child_process').spawn;
function copyToTemp(texDocument, callback) {
fs.mkdtemp('/tmp/pdfer-', (err, tmpPath) => {
if (err) {
callback(err);
return;
}
const docPath = path.join(tmpPath, 'doc.tex');
fs.writeFile(docPath, texDocument, (err) => {
if (err) {
callback(err);
}
callback(null, docPath);
});
});
}
function generateDoc(docPath, callback) {
const pdflatex = spawn('pdflatex', [docPath, '-halt-on-error'], {cwd: path.dirname(docPath)});
pdflatex.stderr.on('data', (data) => {
console.error('onData', data);
});
pdflatex.on('close', (code) => {
if (code > 0) {
callback(`pdflatex returned with code ${code}`);
return;
}
console.log('PDF generated');
const pdfFilePath = docPath.replace('.tex', '.pdf');
fs.readFile(pdfFilePath, (err, data) => {
if (err) {
callback(err);
return;
}
callback(null, data);
});
});
}
module.exports = (texDocument, callback) => {
copyToTemp(texDocument, (err, docPath) => {
generateDoc(docPath, callback);
});
};