working prototype

This commit is contained in:
Thomas Ruoff
2017-02-19 00:23:16 +01:00
parent 09aca6778c
commit 8542c0bf2d
9 changed files with 55087 additions and 33 deletions

View File

@@ -1,26 +1,32 @@
const fs = require('fs');
const path = require('path');
const spawn = require('child_process').spawn;
const uuid = require('uuid');
const {getDirPath, getDocPath} = require('./utils');
function copyToTemp(texDocument, callback) {
fs.mkdtemp('/tmp/pdfer-', (err, tmpPath) => {
const id = uuid.v1();
const dirPath = getDirPath(id);
fs.mkdir(dirPath, (err) => {
if (err) {
callback(err);
return;
}
const docPath = path.join(tmpPath, 'doc.tex');
const docPath = getDocPath(id);
fs.writeFile(docPath, texDocument, (err) => {
if (err) {
callback(err);
}
callback(null, docPath);
callback(null, id);
});
});
}
function generateDoc(docPath, callback) {
const pdflatex = spawn('pdflatex', [docPath, '-interaction', 'nonstopmode'], {cwd: path.dirname(docPath)});
function generateDoc(id, callback) {
const pdflatex = spawn('pdflatex', [getDocPath(id), '-interaction', 'nonstopmode'], {cwd: getDirPath(id)});
pdflatex.stderr.on('data', (data) => {
console.error('onData', data);
});
@@ -31,19 +37,12 @@ function generateDoc(docPath, callback) {
return;
}
console.log('PDF generated');
const pdfFilePath = docPath.replace('.tex', '.pdf');
fs.readFile(pdfFilePath, (err, data) => {
if (err) {
callback(err);
return;
}
callback(null, data);
});
callback(null, id);
});
}
module.exports = (texDocument, callback) => {
copyToTemp(texDocument, (err, docPath) => {
generateDoc(docPath, callback);
copyToTemp(texDocument, (err, id) => {
generateDoc(id, callback);
});
};