From 4acf3e39ce55c0ad9e697cf9598569ec1c1dcb44 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Fri, 5 Feb 2021 20:08:47 +0100 Subject: [PATCH] Switched to handlebars for templateing ref #5 --- package.json | 3 ++- src/PdfCreator.ts | 13 +++++-------- src/templates/sponsoring_contract.html | 8 ++++---- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index 1a028db..44f7435 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "cors": "^2.8.5", "dotenv": "^8.2.0", "express": "^4.17.1", + "handlebars": "^4.7.6", "puppeteer": "^7.0.1", "reflect-metadata": "^0.1.13", "routing-controllers": "^0.9.0-alpha.6", @@ -74,4 +75,4 @@ "publish": false } } -} \ No newline at end of file +} diff --git a/src/PdfCreator.ts b/src/PdfCreator.ts index 57f0610..4bba509 100644 --- a/src/PdfCreator.ts +++ b/src/PdfCreator.ts @@ -2,7 +2,7 @@ import fs from "fs"; import path from 'path'; import puppeteer from "puppeteer"; import { Runner } from './models/Runner'; - +import Handlebars = require('handlebars'); /** * This class is responsible for all things pdf creation. * This uses the html templates from src/templates. @@ -19,13 +19,10 @@ export class PdfCreator { //TODO: Accept the runner class public async generateSponsoringContract(runner: Runner): Promise { - let template = fs.readFileSync(`${this.templateDir}/sponsoring_contract.html`, 'utf8'); - template = template - .replace("{{runner_id}}", runner.id.toString()) - .replace("{{runner_firstname}}", runner.firstname) - .replace("{{runner_lastname}}", runner.lastname) - .replace("{{runner_groupname}}", runner.group.name); - return await this.renderPdf(template, { format: "A5", landscape: true }); + const template_source = fs.readFileSync(`${this.templateDir}/sponsoring_contract.html`, 'utf8'); + const template = Handlebars.compile(template_source); + const result = template({ runner: runner }) + return await this.renderPdf(result, { format: "A5", landscape: true }); } /** diff --git a/src/templates/sponsoring_contract.html b/src/templates/sponsoring_contract.html index 9803cf5..c10b4b4 100644 --- a/src/templates/sponsoring_contract.html +++ b/src/templates/sponsoring_contract.html @@ -6,10 +6,10 @@
-

ID: {{runner_id}}

-

FIRSTNAME: {{runner_firstname}}

-

LASTNAME: {{runner_lastname}}

-

GROUP: {{runner_groupname}}

+

ID: {{runner.id}}

+

FIRSTNAME: {{runner.firstname}}

+

LASTNAME: {{runner.lastname}}

+

GROUP: {{runner.group.name}}

\ No newline at end of file