Merge pull request 'Implemented the basics for templateing feature/3-pdf_templateing' (#4) from feature/3-pdf_templateing into dev
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
Reviewed-on: #4
This commit is contained in:
commit
15f16415c0
1
.gitignore
vendored
1
.gitignore
vendored
@ -137,3 +137,4 @@ build
|
|||||||
lib
|
lib
|
||||||
/oss-attribution
|
/oss-attribution
|
||||||
*.tmp
|
*.tmp
|
||||||
|
*.pdf
|
@ -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",
|
||||||
|
"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"
|
||||||
|
56
src/PdfCreator.ts
Normal file
56
src/PdfCreator.ts
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
20
src/controllers/PdfController.ts
Normal file
20
src/controllers/PdfController.ts
Normal 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
0
src/templates/.gitkeep
Normal file
45
src/templates/sponsoring_contract.html
Normal file
45
src/templates/sponsoring_contract.html
Normal 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>
|
Loading…
x
Reference in New Issue
Block a user