mirror of
https://github.com/tomru/pdfer.git
synced 2026-03-03 14:37:21 +01:00
52 lines
1.3 KiB
JavaScript
52 lines
1.3 KiB
JavaScript
const fs = require('fs');
|
|
const spawn = require('child_process').spawn;
|
|
const uuid = require('uuid');
|
|
|
|
const {getDirPath, getDocPath} = require('./utils');
|
|
|
|
|
|
function copyToTemp(id, texDocument) {
|
|
return new Promise((resolve, reject) => {
|
|
const dirPath = getDirPath(id);
|
|
|
|
fs.mkdir(dirPath, (err) => {
|
|
if (err) {
|
|
reject(err);
|
|
return;
|
|
}
|
|
|
|
const docPath = getDocPath(id);
|
|
fs.writeFile(docPath, texDocument, (err) => {
|
|
if (err) {
|
|
reject(err);
|
|
}
|
|
resolve(id);
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
function generateDoc(id) {
|
|
return new Promise((resolve, reject) => {
|
|
const pdflatex = spawn('pdflatex', ['-interaction', 'nonstopmode', getDocPath(id)], {cwd: getDirPath(id)});
|
|
pdflatex.stderr.on('data', (data) => {
|
|
console.error('onData', data);
|
|
});
|
|
|
|
pdflatex.on('close', (code) => {
|
|
if (code > 0) {
|
|
reject(`pdflatex returned with code ${code}`);
|
|
return;
|
|
}
|
|
console.log(`PDF ${id} generated`);
|
|
resolve(id);
|
|
});
|
|
});
|
|
}
|
|
|
|
module.exports = function(texDocument, callback) {
|
|
const id = uuid.v1();
|
|
return copyToTemp(id, texDocument)
|
|
.then(generateDoc);
|
|
};
|