mirror of
https://github.com/tomru/pdfer.git
synced 2026-03-03 06:27:19 +01:00
49 lines
1.2 KiB
JavaScript
49 lines
1.2 KiB
JavaScript
const fs = require('fs');
|
|
const spawn = require('child_process').spawn;
|
|
const uuid = require('uuid');
|
|
|
|
const {getDirPath, getDocPath} = require('./utils');
|
|
|
|
|
|
function copyToTemp(texDocument, callback) {
|
|
const id = uuid.v1();
|
|
const dirPath = getDirPath(id);
|
|
|
|
fs.mkdir(dirPath, (err) => {
|
|
if (err) {
|
|
callback(err);
|
|
return;
|
|
}
|
|
|
|
const docPath = getDocPath(id);
|
|
fs.writeFile(docPath, texDocument, (err) => {
|
|
if (err) {
|
|
callback(err);
|
|
}
|
|
callback(null, id);
|
|
});
|
|
});
|
|
}
|
|
|
|
function generateDoc(id, callback) {
|
|
const pdflatex = spawn('pdflatex', [getDocPath(id), '-interaction', 'nonstopmode'], {cwd: getDirPath(id)});
|
|
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');
|
|
callback(null, id);
|
|
});
|
|
}
|
|
|
|
module.exports = (texDocument, callback) => {
|
|
copyToTemp(texDocument, (err, id) => {
|
|
generateDoc(id, callback);
|
|
});
|
|
};
|