mirror of
https://github.com/tomru/pdfer.git
synced 2026-03-03 06:27:19 +01:00
initial commit
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
node_modules
|
||||||
28
index.js
Normal file
28
index.js
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
const express = require('express');
|
||||||
|
const bodyParser = require('body-parser');
|
||||||
|
const app = express();
|
||||||
|
const templates = require('./templates');
|
||||||
|
|
||||||
|
app.use(bodyParser.json());
|
||||||
|
|
||||||
|
app.post('/generate/:template', (req, res) => {
|
||||||
|
|
||||||
|
const templateName = req.params.template;
|
||||||
|
const options = req.body;
|
||||||
|
|
||||||
|
templates.get(templateName, options, (err, template) => {
|
||||||
|
if (err) {
|
||||||
|
console.error('Error:', err.code, 'for', req.url);
|
||||||
|
res.sendStatus(500).end('Something went wrong, call Thomas');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let result = template;
|
||||||
|
|
||||||
|
res.send(result);
|
||||||
|
res.end();
|
||||||
|
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
app.listen(5000);
|
||||||
15
package.json
Normal file
15
package.json
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"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"
|
||||||
|
},
|
||||||
|
"author": "Thomas Ruoff <tomru@mail.id0.link>",
|
||||||
|
"license": "ISC",
|
||||||
|
"dependencies": {
|
||||||
|
"body-parser": "^1.16.1",
|
||||||
|
"express": "^4.14.1"
|
||||||
|
}
|
||||||
|
}
|
||||||
30
templates.js
Normal file
30
templates.js
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
function replaceOption(string, key, value) {
|
||||||
|
const re = new RegExp(`\\$\{${key}\}`, 'g');
|
||||||
|
return string.replace(re, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
function cleanUnsetVars(string) {
|
||||||
|
const re = new RegExp('\\$\{[a-zA-Z-_]+\}', 'g');
|
||||||
|
return string.replace(re, '');
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports.get = (templateName, options, callback) => {
|
||||||
|
const filename = path.join(__dirname, 'templates', templateName + '.template');
|
||||||
|
|
||||||
|
fs.readFile(filename, 'utf-8', (err, template) => {
|
||||||
|
if (err) {
|
||||||
|
callback(err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const replaced = Object.keys(options).reduce((memo, key) => {
|
||||||
|
return replaceOption(memo, key, options[key]);
|
||||||
|
}, template);
|
||||||
|
|
||||||
|
const result = cleanUnsetVars(replaced);
|
||||||
|
|
||||||
|
callback(null, result);
|
||||||
|
});
|
||||||
|
};
|
||||||
35
templates/brief.template
Normal file
35
templates/brief.template
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
\documentclass{scrlttr2}
|
||||||
|
\LoadLetterOption{${LETTER_OPTION}}
|
||||||
|
|
||||||
|
\setkomavar{subject}{${SUBJECT}}
|
||||||
|
|
||||||
|
\setkomavar{yourref}{${YOURREF}}
|
||||||
|
\setkomavar{yourmail}{${YOURMAIL}}
|
||||||
|
\setkomavar{myref}{${MYREF}}
|
||||||
|
\setkomavar{customer}{${CUSTOMER}}
|
||||||
|
\setkomavar{invoice}{${INVOICE}}
|
||||||
|
% \setkomavar{yourref}[alternative name]{value}
|
||||||
|
|
||||||
|
% custom date
|
||||||
|
%\setkomavar{date}{}
|
||||||
|
|
||||||
|
%\setkomavar{signature}{\electronicsignature}
|
||||||
|
|
||||||
|
\setkomavar{specialmail}{${SPECIALMAIL}}
|
||||||
|
|
||||||
|
\begin{document}
|
||||||
|
\begin{letter}{${ADDRESS}}
|
||||||
|
|
||||||
|
\opening{${OPENING}}
|
||||||
|
|
||||||
|
${BODY}
|
||||||
|
|
||||||
|
\closing{${CLOSING}}
|
||||||
|
|
||||||
|
\ps{${PS}}
|
||||||
|
|
||||||
|
\encl{${ENCL}}
|
||||||
|
|
||||||
|
\end{letter}
|
||||||
|
\end{document}
|
||||||
|
|
||||||
Reference in New Issue
Block a user