mirror of
https://github.com/tomru/pdfer.git
synced 2026-03-03 06:27:19 +01:00
make pdfs
This commit is contained in:
22
index.js
22
index.js
@@ -2,26 +2,32 @@ const express = require('express');
|
|||||||
const bodyParser = require('body-parser');
|
const bodyParser = require('body-parser');
|
||||||
const app = express();
|
const app = express();
|
||||||
const templates = require('./templates');
|
const templates = require('./templates');
|
||||||
|
const renderer = require('./renderer');
|
||||||
|
|
||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json());
|
||||||
|
|
||||||
app.post('/generate/:template', (req, res) => {
|
app.post('/generate/pdf/:template', (req, res) => {
|
||||||
|
|
||||||
const templateName = req.params.template;
|
const templateName = req.params.template;
|
||||||
const options = req.body;
|
const options = req.body;
|
||||||
|
|
||||||
templates.get(templateName, options, (err, template) => {
|
templates.get(templateName, options, (err, texDocument) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error('Error:', err.code, 'for', req.url);
|
console.error('Error:', err.code, 'for', req.url);
|
||||||
res.sendStatus(500).end('Something went wrong, call Thomas');
|
res.sendStatus(500).end('Something went wrong while generating Tex source');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let result = template;
|
renderer(texDocument, (err, pdf) => {
|
||||||
|
if (err) {
|
||||||
res.send(result);
|
console.error('Error:', err.code, 'for', req.url);
|
||||||
res.end();
|
res.sendStatus(500).end('Something went wrong while baking the PDF');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
res.set('Content-Type', 'application/pdf');
|
||||||
|
res.send(pdf);
|
||||||
|
res.end();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
49
renderer.js
Normal file
49
renderer.js
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
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);
|
||||||
|
});
|
||||||
|
};
|
||||||
33
templates.js
33
templates.js
@@ -1,30 +1,13 @@
|
|||||||
const fs = require('fs');
|
|
||||||
const path = require('path');
|
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) => {
|
module.exports.get = (templateName, options, callback) => {
|
||||||
const filename = path.join(__dirname, 'templates', templateName + '.template');
|
const modulePath = path.join(__dirname, 'templates', templateName);
|
||||||
|
let template;
|
||||||
|
|
||||||
fs.readFile(filename, 'utf-8', (err, template) => {
|
try {
|
||||||
if (err) {
|
template = require(modulePath);
|
||||||
callback(err);
|
callback(null, template(options));
|
||||||
return;
|
} catch (e) {
|
||||||
}
|
callback(`${templateName} not found!`);
|
||||||
const replaced = Object.keys(options).reduce((memo, key) => {
|
}
|
||||||
return replaceOption(memo, key, options[key]);
|
|
||||||
}, template);
|
|
||||||
|
|
||||||
const result = cleanUnsetVars(replaced);
|
|
||||||
|
|
||||||
callback(null, result);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|||||||
60
templates/brief.js
Normal file
60
templates/brief.js
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
function convertLineBreaks(string){
|
||||||
|
return string.replace('\n', '////');
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = (options) => {
|
||||||
|
|
||||||
|
var {
|
||||||
|
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}`;
|
||||||
|
};
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
\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