From 7f58dd694b53152069c2095b2e18dd3a46cd04dd Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Mon, 8 Feb 2021 16:14:10 +0100 Subject: [PATCH] Fixed double-sided printing ref #14 --- src/PdfCreator.ts | 21 ++++++++++++++++++++- src/templates/runner_card.html | 7 +++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/PdfCreator.ts b/src/PdfCreator.ts index 64286ad..707e003 100644 --- a/src/PdfCreator.ts +++ b/src/PdfCreator.ts @@ -129,10 +129,11 @@ export class PdfCreator { const pdfs = await Promise.all(pdf_promises); return await this.mergePdfs(pdfs); } + const cards_swapped = this.swapArrayPairs(cards); await i18next.changeLanguage(locale); const template_source = fs.readFileSync(`${this.templateDir}/runner_card.html`, 'utf8'); const template = Handlebars.compile(template_source); - const result = template({ cards, eventname: "LfK! 2069" }) + const result = template({ cards, cards_swapped, eventname: "LfK! 2069" }) const pdf = await this.renderPdf(result, { format: "A4", landscape: false }); return pdf } @@ -220,4 +221,22 @@ export class PdfCreator { runner.group = group; return runner; } + + /** + * Swaps pairs (0/1, 2/3, ...) of elements in an array recursively. + * If the last element has no partner it inserts an empty element at the end and swaps the two + * This is needed to generate pdfs with front- and backside that get printet on one paper. + * @param array The array which's pairs shall get switched. + * @returns Array with swapped pairs, + */ + private swapArrayPairs(array): Array { + if (array.length == 1) { + return [null, array[0]]; + } + if (array.length == 0) { + return null; + } + + return [array[1], array[0]].concat(this.swapArrayPairs(array.slice(2))); + } } \ No newline at end of file diff --git a/src/templates/runner_card.html b/src/templates/runner_card.html index 2a85229..987194e 100644 --- a/src/templates/runner_card.html +++ b/src/templates/runner_card.html @@ -46,19 +46,18 @@

{{this.runner.lastname}}, {{this.runner.firstname}} {{this.runner.middlename}}

-

{{this.runner.group.name}}

+

{{this.runner.group.name}} {{this.id}}

{{/each}}
- {{#each cards}} + {{#each cards_swapped}}
+

{{this.id}}

- -
{{/each}}