mirror of
https://github.com/tomru/pdfer.git
synced 2026-03-03 14:37:21 +01:00
50 lines
1.3 KiB
JavaScript
50 lines
1.3 KiB
JavaScript
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);
|
|
});
|
|
};
|