Switched to handlebars for templateing

ref #5
This commit is contained in:
Nicolai Ort 2021-02-05 20:08:47 +01:00
parent 3af76a53e3
commit 4acf3e39ce
3 changed files with 11 additions and 13 deletions

View File

@ -45,6 +45,7 @@
"cors": "^2.8.5", "cors": "^2.8.5",
"dotenv": "^8.2.0", "dotenv": "^8.2.0",
"express": "^4.17.1", "express": "^4.17.1",
"handlebars": "^4.7.6",
"puppeteer": "^7.0.1", "puppeteer": "^7.0.1",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
"routing-controllers": "^0.9.0-alpha.6", "routing-controllers": "^0.9.0-alpha.6",

View File

@ -2,7 +2,7 @@ import fs from "fs";
import path from 'path'; import path from 'path';
import puppeteer from "puppeteer"; import puppeteer from "puppeteer";
import { Runner } from './models/Runner'; import { Runner } from './models/Runner';
import Handlebars = require('handlebars');
/** /**
* This class is responsible for all things pdf creation. * This class is responsible for all things pdf creation.
* This uses the html templates from src/templates. * This uses the html templates from src/templates.
@ -19,13 +19,10 @@ export class PdfCreator {
//TODO: Accept the runner class //TODO: Accept the runner class
public async generateSponsoringContract(runner: Runner): Promise<any> { public async generateSponsoringContract(runner: Runner): Promise<any> {
let template = fs.readFileSync(`${this.templateDir}/sponsoring_contract.html`, 'utf8'); const template_source = fs.readFileSync(`${this.templateDir}/sponsoring_contract.html`, 'utf8');
template = template const template = Handlebars.compile(template_source);
.replace("{{runner_id}}", runner.id.toString()) const result = template({ runner: runner })
.replace("{{runner_firstname}}", runner.firstname) return await this.renderPdf(result, { format: "A5", landscape: true });
.replace("{{runner_lastname}}", runner.lastname)
.replace("{{runner_groupname}}", runner.group.name);
return await this.renderPdf(template, { format: "A5", landscape: true });
} }
/** /**

View File

@ -6,10 +6,10 @@
</head> </head>
<body> <body>
<div class="page"> <div class="page">
<p>ID: {{runner_id}}</p> <p>ID: {{runner.id}}</p>
<p>FIRSTNAME: {{runner_firstname}}</p> <p>FIRSTNAME: {{runner.firstname}}</p>
<p>LASTNAME: {{runner_lastname}}</p> <p>LASTNAME: {{runner.lastname}}</p>
<p>GROUP: {{runner_groupname}}</p> <p>GROUP: {{runner.group.name}}</p>
</div> </div>
</body> </body>
</html> </html>