mirror of
https://github.com/tomru/pdfer.git
synced 2026-03-03 06:27:19 +01:00
working prototype
This commit is contained in:
31
renderer.js
31
renderer.js
@@ -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);
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user