mirror of
https://github.com/tomru/pdfer.git
synced 2026-03-03 06:27:19 +01:00
start over with nextjs
This commit is contained in:
51
lib/renderer.ts
Normal file
51
lib/renderer.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { mkdir, writeFile } from 'fs'
|
||||
import { spawn } from 'child_process'
|
||||
import { v1 as uuidv1 } from 'uuid'
|
||||
|
||||
import { getDirPath, getDocPath } from './utils';
|
||||
|
||||
|
||||
function copyToTemp(id: string, texDocument: string): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const dirPath = getDirPath(id);
|
||||
|
||||
mkdir(dirPath, (err) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
|
||||
const docPath = getDocPath(id);
|
||||
writeFile(docPath, texDocument, (err) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
resolve(id);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function generateDoc(id: string) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const pdflatex = spawn('pdflatex', ['-interaction', 'nonstopmode', getDocPath(id)], { cwd: getDirPath(id) });
|
||||
pdflatex.stderr.on('data', (data) => {
|
||||
console.error('onData', data);
|
||||
});
|
||||
|
||||
pdflatex.on('close', (code) => {
|
||||
if (code > 0) {
|
||||
reject(`pdflatex returned with code ${code}`);
|
||||
return;
|
||||
}
|
||||
console.log(`PDF ${id} generated`);
|
||||
resolve(id);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export default function(texDocument: string) {
|
||||
const id = uuidv1();
|
||||
return copyToTemp(id, texDocument)
|
||||
.then(generateDoc);
|
||||
};
|
||||
14
lib/store.ts
Normal file
14
lib/store.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
const storeDir = process.env.JSON_STORE || '/tmp/pdfer-store/';
|
||||
|
||||
import { promisify } from 'util';
|
||||
|
||||
import JsonStore from 'json-fs-store'
|
||||
|
||||
const store = JsonStore(storeDir);
|
||||
|
||||
console.log(`using json-store at ${storeDir}`);
|
||||
|
||||
export const list = promisify(store.list);
|
||||
export const load = promisify(store.load);
|
||||
export const add = promisify(store.add);
|
||||
export const remove = promisify(store.remove);
|
||||
60
lib/templates.ts
Normal file
60
lib/templates.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
function convertLineBreaks(string) {
|
||||
return string.replace(/\n/g, '\\\\');
|
||||
};
|
||||
|
||||
export function brief(options) {
|
||||
|
||||
const {
|
||||
template = 'brief-fam',
|
||||
subject = '',
|
||||
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 = '',
|
||||
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}`;
|
||||
};
|
||||
13
lib/utils.ts
Normal file
13
lib/utils.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import path from 'path';
|
||||
|
||||
export function getDirPath(id) {
|
||||
return `/tmp/pdfer-${id}`;
|
||||
}
|
||||
|
||||
export function getDocPath(id) {
|
||||
return path.join(getDirPath(id), 'doc.tex');
|
||||
}
|
||||
|
||||
export function getPdfPath(id) {
|
||||
return path.join(getDirPath(id), 'doc.pdf');
|
||||
}
|
||||
Reference in New Issue
Block a user