Compare commits

..

No commits in common. "15f16415c0ac5012df8126c8df6bd4da93ad0a20" and "b5e79e51ef5d751a67e7a1763809ece91a7627f2" have entirely different histories.

6 changed files with 2 additions and 125 deletions

1
.gitignore vendored
View File

@ -137,4 +137,3 @@ build
lib lib
/oss-attribution /oss-attribution
*.tmp *.tmp
*.pdf

View File

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

View File

@ -1,56 +0,0 @@
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

@ -1,20 +0,0 @@
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();
}
}

View File

View File

@ -1,45 +0,0 @@
<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>