Merge pull request 'Implemented the basics for templateing feature/3-pdf_templateing' (#4) from feature/3-pdf_templateing into dev
continuous-integration/drone/push Build is failing Details

Reviewed-on: #4
This commit is contained in:
Nicolai Ort 2021-02-03 17:01:59 +00:00
commit 15f16415c0
6 changed files with 125 additions and 2 deletions

3
.gitignore vendored
View File

@ -136,4 +136,5 @@ build
/docs
lib
/oss-attribution
*.tmp
*.tmp
*.pdf

View File

@ -45,6 +45,7 @@
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"html-pdf": "^2.2.0",
"reflect-metadata": "^0.1.13",
"routing-controllers": "^0.9.0-alpha.6",
"routing-controllers-openapi": "^2.2.0"
@ -72,4 +73,4 @@
"publish": false
}
}
}
}

56
src/PdfCreator.ts Normal file
View File

@ -0,0 +1,56 @@
import fs from "fs";
import pdf_converter from "html-pdf";
import path from 'path';
import { Stream } from 'stream';
/**
* This class is responsible for all things pdf creation.
* This uses the html templates from src/templates.
*/
export class PdfCreator {
private templateDir = path.join(__dirname, '/templates');
//TODO: Accept the runner class
public async generateSponsoringContract(): Promise<Pdf> {
let template = fs.readFileSync(`${this.templateDir}/sponsoring_contract.html`, 'utf8');
template = template.replace("{{Runner Name}}", "lelele");
return new Pdf(await pdf_converter.create(template, { format: "A5", orientation: "landscape" }));
}
}
/**
* This class is a wrapper for the pdf objects created by html-pdf.
* It offers typed conversion to Buffer and Stream.
*/
export class Pdf {
content: any;
constructor(pdf: any) {
this.content = pdf;
}
/**
* Promise wrapper function that resolves the toBuffer promise for pdf generation.
*/
public async toBuffer(): Promise<Buffer> {
let promise = await new Promise<Buffer>((resolve, reject) => {
this.content.toBuffer(function (err, buffer: Buffer) {
resolve(buffer);
});
});
return await promise;
}
/**
* Promise wrapper function that resolves the toStream promise for pdf generation.
*/
public async toStream(): Promise<Stream> {
let promise = await new Promise<Stream>((resolve, reject) => {
this.content.toStream(function (err, stream: Stream) {
resolve(stream);
});
});
return await promise;
}
}

View File

@ -0,0 +1,20 @@
import { ContentType, Controller, Get } from 'routing-controllers';
import { OpenAPI } from 'routing-controllers-openapi';
import { PdfCreator } from '../PdfCreator';
@Controller()
export class PdfController {
private pdf: PdfCreator;
constructor() {
this.pdf = new PdfCreator();
}
@Get('/contracts')
@ContentType("application/pdf")
@OpenAPI({ description: "Generate Sponsoring contract pdfs from runner objects." })
async generateContracts() {
//TODO: Accept the real classes
const contracts = await this.pdf.generateSponsoringContract();
return await contracts.toBuffer();
}
}

0
src/templates/.gitkeep Normal file
View File

View File

@ -0,0 +1,45 @@
<html>
<head>
<meta charset="utf8">
<title>Sponsoring contract</title>
<style>
html, body {
margin: 0;
padding: 0;
font-family: 'Sackers Gothic Std';
font-weight: 500;
background: rgb(241,241,241);
-webkit-print-color-adjust: exact;
box-sizing: border-box;
}
.page {
position: relative;
height: 148mm;
width: 210mm;
display: block;
background: white;
page-break-after: auto;
margin: 50px;
overflow: hidden;
}
@media print {
body {
background: white;
}
.page {
margin: 0;
height: 100%;
width: 100%;
}
}
</style>
</head>
<body>
<div class="page">
<p style="font-size: 100vw;">{{Runner Name}}</p>
</div>
</body>
</html>