add server dir

This commit is contained in:
Thomas Ruoff
2017-02-23 21:00:35 +01:00
parent 2441268fb8
commit db2c45342f
6 changed files with 0 additions and 0 deletions

46
server/index.js Normal file
View File

@@ -0,0 +1,46 @@
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
const templates = require('./templates');
const renderer = require('./renderer');
const {getPdfPath} = require('./utils');
app.use(bodyParser.json());
app.options('/api/pdf/generate/:template', cors());
app.post('/api/pdf/generate/:template', cors(), (req, res) => {
const templateName = req.params.template;
const options = req.body;
templates.get(templateName, options, (err, texDocument) => {
if (err) {
console.error('Error:', err.code, 'for', req.url);
res.sendStatus(500).end('Something went wrong while generating Tex source');
return;
}
renderer(texDocument, (err, pdfFilePath) => {
if (err) {
console.error('Error:', err.code, 'for', req.url);
res.sendStatus(500).end('Something went wrong while baking the PDF');
return;
}
const id = pdfFilePath.replace('/tmp/', '');
res.send({id: id});
res.end();
});
});
});
app.options('/api/pdf/:id', cors());
app.get('/api/pdf/:id', cors(), (req, res) => {
const {id} = req.params;
res.sendFile(getPdfPath(id));
});
app.use(express.static('./client/build'));
app.listen(5000);

23
server/package.json Normal file
View File

@@ -0,0 +1,23 @@
{
"name": "pdfer",
"version": "1.0.0",
"description": "Generate pdfs based on a template",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js",
"watch": "nodemon index.js"
},
"author": "Thomas Ruoff <tomru@mail.id0.link>",
"license": "ISC",
"dependencies": {
"body-parser": "^1.16.1",
"cors": "^2.8.1",
"express": "^4.14.1",
"uuid": "^3.0.1"
},
"devDependencies": {
"eslint": "^3.16.0",
"nodemon": "^1.11.0"
}
}

52
server/renderer.js Normal file
View File

@@ -0,0 +1,52 @@
const fs = require('fs');
const spawn = require('child_process').spawn;
const uuid = require('uuid');
const {getDirPath, getDocPath} = require('./utils');
function copyToTemp(id, texDocument, callback) {
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);
});
});
}
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 ${id} generated`);
callback(null, id);
});
}
module.exports = (texDocument, callback) => {
const id = uuid.v1();
copyToTemp(id, texDocument, (err) => {
if (err) {
callback(err);
return;
}
generateDoc(id, callback);
});
};

13
server/templates.js Normal file
View File

@@ -0,0 +1,13 @@
const path = require('path');
module.exports.get = (templateName, options, callback) => {
const modulePath = path.join(__dirname, 'templates', templateName);
let template;
try {
template = require(modulePath);
callback(null, template(options));
} catch (e) {
callback(`${templateName} not found!`);
}
};

60
server/templates/brief.js Normal file
View File

@@ -0,0 +1,60 @@
function convertLineBreaks(string){
return string.replace(/\n/g, '\\\\');
};
module.exports = (options) => {
const {
template = 'brief-fam',
subject = 'Betreff',
yourRef = '',
yourRefName = 'Ihr Zeichen',
yourMail = '',
myRef = '',
customer = '',
invoice = '',
date = '\\today',
signature = '',
specialMail = '',
address = 'Max Mustermann\\\\Musterstrasse\\\\12345 Musterstadt',
opening = 'Sehr geehrte Damen und Herren',
body = 'Inhalt des Briefes',
closing = 'Mit freundlichen Grüßen',
ps = '',
enclosing = '',
} = options;
return `% brief document
\\documentclass{scrlttr2}
\\LoadLetterOption{${template}}
\\setkomavar{subject}{${subject}}
\\setkomavar{yourref}[${yourRefName}]{${yourRef}}
\\setkomavar{yourmail}{${yourMail}}
\\setkomavar{myref}{${myRef}}
\\setkomavar{customer}{${customer}}
\\setkomavar{invoice}{${invoice}}
\\setkomavar{date}{${date}}
%\\setkomavar{signature}{${signature}}
\\setkomavar{specialmail}{${specialMail}}
\\begin{document}
\\begin{letter}{${convertLineBreaks(address)}}
\\opening{${opening},}
${convertLineBreaks(body)}
\\closing{${closing}}
\\ps{${ps}}
%\\encl{${enclosing}}
\\end{letter}
\\end{document}`;
};

19
server/utils.js Normal file
View File

@@ -0,0 +1,19 @@
const path = require('path');
function getDirPath(id) {
return `/tmp/pdfer-${id}`;
}
function getDocPath(id) {
return path.join(getDirPath(id), 'doc.tex');
}
function getPdfPath(id) {
return path.join(getDirPath(id), 'doc.pdf');
}
module.exports = {
getDirPath,
getDocPath,
getPdfPath
};