From 9f3758de39ef1d5b591a24868eb683caa742f694 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Tue, 2 Feb 2021 09:59:34 +0100 Subject: [PATCH 01/11] Added PDFs to gitignore ref #3 --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 37345c3..72aa169 100644 --- a/.gitignore +++ b/.gitignore @@ -136,4 +136,5 @@ build /docs lib /oss-attribution -*.tmp \ No newline at end of file +*.tmp +*.pdf \ No newline at end of file -- 2.47.2 From 8a30265fc600dad17f584f95dd51db976945d4c5 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Tue, 2 Feb 2021 09:59:48 +0100 Subject: [PATCH 02/11] Added templates folder ref #3 --- src/templates/.gitkeep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 src/templates/.gitkeep diff --git a/src/templates/.gitkeep b/src/templates/.gitkeep new file mode 100644 index 0000000..e69de29 -- 2.47.2 From 557cc26f281da0b47621dff667435a788c42c103 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Tue, 2 Feb 2021 10:01:31 +0100 Subject: [PATCH 03/11] Added Barebones pdf creator class ref #3 --- src/PdfCreator.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 src/PdfCreator.ts diff --git a/src/PdfCreator.ts b/src/PdfCreator.ts new file mode 100644 index 0000000..1650c79 --- /dev/null +++ b/src/PdfCreator.ts @@ -0,0 +1,14 @@ +import fs from "fs"; +import path from 'path'; + +/** + * 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'); + + public generateSponsoringContract() { + const template = fs.readFileSync(`${this.templateDir}/sponsoring_contract.html`, 'utf8'); + } +} -- 2.47.2 From 4617f2c5bdcb29534b46865ebe21d15e2b1cd72f Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Tue, 2 Feb 2021 11:02:23 +0100 Subject: [PATCH 04/11] Resolved fun issues with promises ref #3 --- src/PdfCreator.ts | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/src/PdfCreator.ts b/src/PdfCreator.ts index 1650c79..3fa95a8 100644 --- a/src/PdfCreator.ts +++ b/src/PdfCreator.ts @@ -1,14 +1,42 @@ import fs from "fs"; +import pdf_converter from "html-pdf"; import path from 'path'; - /** * This class is responsible for all things pdf creation. * This uses the html templates from src/templates. */ -export class PdFCreator { +export class PdfCreator { private templateDir = path.join(__dirname, '/templates'); - public generateSponsoringContract() { + public async generateSponsoringContract(): Promise { const template = fs.readFileSync(`${this.templateDir}/sponsoring_contract.html`, 'utf8'); + let pdf = await pdf_converter.create(template, { format: "A5", orientation: "landscape" }); + return await this.toBuffer(pdf); + } + + /** + * Promise wrapper function that resolves the toBuffer promise for pdf generation. + * @param pdf The pdf object that shall be turned into a buffer. + */ + public async toBuffer(pdf): Promise { + let promise = await new Promise((resolve, reject) => { + pdf.toBuffer(function (err, buffer) { + resolve(buffer); + }); + }); + return await promise; + } + + /** + * Promise wrapper function that resolves the toStream promise for pdf generation. + * @param pdf The pdf object that shall be turned into a stream. + */ + public async toStream(pdf): Promise { + let promise = await new Promise((resolve, reject) => { + pdf.toStream(function (err, stream) { + resolve(stream); + }); + }); + return await promise; } } -- 2.47.2 From a7d4001ad992d60e2af69e9bbed53366bc0a46d3 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Tue, 2 Feb 2021 11:03:06 +0100 Subject: [PATCH 05/11] Added html-pdf package ref #3 --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index f2e080c..eb1477f 100644 --- a/package.json +++ b/package.json @@ -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 } } -} \ No newline at end of file +} -- 2.47.2 From f726a6e699575b839d332c15e61c9008fe996fa9 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Tue, 2 Feb 2021 11:20:47 +0100 Subject: [PATCH 06/11] Added a bs example template ref #3 --- src/templates/sponsoring_contract.html | 46 ++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/templates/sponsoring_contract.html diff --git a/src/templates/sponsoring_contract.html b/src/templates/sponsoring_contract.html new file mode 100644 index 0000000..a1b7707 --- /dev/null +++ b/src/templates/sponsoring_contract.html @@ -0,0 +1,46 @@ + + + + Sponsoring contract + + + +
+

{{Runner Name}}

+
+ + \ No newline at end of file -- 2.47.2 From 2c7b0254ec7e49c31f8f33eed334ab4156052fb6 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Tue, 2 Feb 2021 11:27:14 +0100 Subject: [PATCH 07/11] Adjusted template font styleing ref #3 --- src/templates/sponsoring_contract.html | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/templates/sponsoring_contract.html b/src/templates/sponsoring_contract.html index a1b7707..1597b2c 100644 --- a/src/templates/sponsoring_contract.html +++ b/src/templates/sponsoring_contract.html @@ -8,7 +8,6 @@ padding: 0; font-family: 'Sackers Gothic Std'; font-weight: 500; - font-size: 70px; background: rgb(241,241,241); -webkit-print-color-adjust: exact; box-sizing: border-box; @@ -40,7 +39,7 @@
-

{{Runner Name}}

+

{{Runner Name}}

\ No newline at end of file -- 2.47.2 From 3ca38abe9359e436fe0f80400ad0308ad9d64a4f Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Tue, 2 Feb 2021 11:28:07 +0100 Subject: [PATCH 08/11] Switched to the splitted functions ref #3 --- src/PdfCreator.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/PdfCreator.ts b/src/PdfCreator.ts index 3fa95a8..4ccd919 100644 --- a/src/PdfCreator.ts +++ b/src/PdfCreator.ts @@ -8,10 +8,11 @@ import path from 'path'; export class PdfCreator { private templateDir = path.join(__dirname, '/templates'); + //TODO: Accept the runner class public async generateSponsoringContract(): Promise { - const template = fs.readFileSync(`${this.templateDir}/sponsoring_contract.html`, 'utf8'); - let pdf = await pdf_converter.create(template, { format: "A5", orientation: "landscape" }); - return await this.toBuffer(pdf); + let template = fs.readFileSync(`${this.templateDir}/sponsoring_contract.html`, 'utf8'); + template = template.replace("{{Runner Name}}", "lelele"); + return await pdf_converter.create(template, { format: "A5", orientation: "landscape" }); } /** -- 2.47.2 From 64bd1ffc3ad8459c13154e2cd9a568f27baa2bf2 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Tue, 2 Feb 2021 11:28:24 +0100 Subject: [PATCH 09/11] Created a barebones pdf controller ref #3 --- src/controllers/PdfController.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/controllers/PdfController.ts diff --git a/src/controllers/PdfController.ts b/src/controllers/PdfController.ts new file mode 100644 index 0000000..9770b11 --- /dev/null +++ b/src/controllers/PdfController.ts @@ -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 this.pdf.toBuffer(contracts); + } +} \ No newline at end of file -- 2.47.2 From 7d5b5750ad78fd979cb1b647d8b318a84513532a Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Tue, 2 Feb 2021 11:30:51 +0100 Subject: [PATCH 10/11] Created a pdf class that takes care of the pdf wrapping ref #3 --- src/PdfCreator.ts | 25 ++++++++++++++++--------- src/controllers/PdfController.ts | 2 +- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/PdfCreator.ts b/src/PdfCreator.ts index 4ccd919..8e547b2 100644 --- a/src/PdfCreator.ts +++ b/src/PdfCreator.ts @@ -9,19 +9,27 @@ export class PdfCreator { private templateDir = path.join(__dirname, '/templates'); //TODO: Accept the runner class - public async generateSponsoringContract(): Promise { + public async generateSponsoringContract(): Promise { let template = fs.readFileSync(`${this.templateDir}/sponsoring_contract.html`, 'utf8'); template = template.replace("{{Runner Name}}", "lelele"); - return await pdf_converter.create(template, { format: "A5", orientation: "landscape" }); + return new Pdf(await pdf_converter.create(template, { format: "A5", orientation: "landscape" })); + } + +} + +export class Pdf { + content: any; + + constructor(pdf: any) { + this.content = pdf; } /** * Promise wrapper function that resolves the toBuffer promise for pdf generation. - * @param pdf The pdf object that shall be turned into a buffer. */ - public async toBuffer(pdf): Promise { + public async toBuffer(): Promise { let promise = await new Promise((resolve, reject) => { - pdf.toBuffer(function (err, buffer) { + this.content.toBuffer(function (err, buffer) { resolve(buffer); }); }); @@ -30,14 +38,13 @@ export class PdfCreator { /** * Promise wrapper function that resolves the toStream promise for pdf generation. - * @param pdf The pdf object that shall be turned into a stream. */ - public async toStream(pdf): Promise { + public async toStream(): Promise { let promise = await new Promise((resolve, reject) => { - pdf.toStream(function (err, stream) { + this.content.toStream(function (err, stream) { resolve(stream); }); }); return await promise; } -} +} \ No newline at end of file diff --git a/src/controllers/PdfController.ts b/src/controllers/PdfController.ts index 9770b11..0ec9581 100644 --- a/src/controllers/PdfController.ts +++ b/src/controllers/PdfController.ts @@ -15,6 +15,6 @@ export class PdfController { async generateContracts() { //TODO: Accept the real classes const contracts = await this.pdf.generateSponsoringContract(); - return await this.pdf.toBuffer(contracts); + return await contracts.toBuffer(); } } \ No newline at end of file -- 2.47.2 From 3fb8be22b74204accabed8a8c8fb5161c5ad7504 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Tue, 2 Feb 2021 11:34:01 +0100 Subject: [PATCH 11/11] Added typeing to the buffer and stream conversion ref #3 --- src/PdfCreator.ts | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/PdfCreator.ts b/src/PdfCreator.ts index 8e547b2..2345c8c 100644 --- a/src/PdfCreator.ts +++ b/src/PdfCreator.ts @@ -1,6 +1,8 @@ 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. @@ -17,6 +19,10 @@ export class PdfCreator { } +/** + * 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; @@ -27,9 +33,9 @@ export class Pdf { /** * Promise wrapper function that resolves the toBuffer promise for pdf generation. */ - public async toBuffer(): Promise { - let promise = await new Promise((resolve, reject) => { - this.content.toBuffer(function (err, buffer) { + public async toBuffer(): Promise { + let promise = await new Promise((resolve, reject) => { + this.content.toBuffer(function (err, buffer: Buffer) { resolve(buffer); }); }); @@ -39,9 +45,9 @@ export class Pdf { /** * Promise wrapper function that resolves the toStream promise for pdf generation. */ - public async toStream(): Promise { - let promise = await new Promise((resolve, reject) => { - this.content.toStream(function (err, stream) { + public async toStream(): Promise { + let promise = await new Promise((resolve, reject) => { + this.content.toStream(function (err, stream: Stream) { resolve(stream); }); }); -- 2.47.2