feat(pdfs): Experimental generation of large runner card files

This commit is contained in:
Nicolai Ort 2025-05-01 19:08:49 +02:00
parent 6dcfd9a4fe
commit 93422b9779
Signed by: niggl
GPG Key ID: 13AFA55AF62F269F

View File

@ -1,197 +1,256 @@
<script> <script>
import { _ } from "svelte-i18n"; import { _ } from "svelte-i18n";
import { import {
RunnerCardService, RunnerCardService,
RunnerOrganizationService, RunnerOrganizationService,
RunnerTeamService, RunnerTeamService,
} from "@odit/lfk-client-js"; } from "@odit/lfk-client-js";
import toast from "svelte-french-toast"; import toast from "svelte-french-toast";
import DocumentServer from "./DocumentServer.ts"; import DocumentServer from "./DocumentServer.ts";
import { init } from "@paralleldrive/cuid2"; import { init } from "@paralleldrive/cuid2";
const createId = init({ length: 10, fingerprint: "lfk-frontend" }); const createId = init({ length: 10, fingerprint: "lfk-frontend" });
const documentServer = new DocumentServer( const documentServer = new DocumentServer(
config.baseurl_documentserver, config.baseurl_documentserver,
config.documentserver_key config.documentserver_key
); );
export let cards_show = false; export let cards_show = false;
export let generate_cards = []; export let generate_cards = [];
export let generate_runners = []; export let generate_runners = [];
export let generate_orgs = []; export let generate_orgs = [];
export let generate_teams = []; export let generate_teams = [];
function download(blob, fileName) { function download(blob, fileName) {
const url = window.URL.createObjectURL(blob); const url = window.URL.createObjectURL(blob);
let a = document.createElement("a"); let a = document.createElement("a");
a.href = url; a.href = url;
a.download = fileName; a.download = fileName;
document.body.appendChild(a); document.body.appendChild(a);
a.click(); a.click();
a.remove(); a.remove();
toast.dismiss(); toast.dismiss();
toast.success($_("pdf-successfully-generated")); toast.success($_("pdf-successfully-generated"));
} }
function generateRunnerCards(locale) { function generateRunnerCards(locale, useCombined = false) {
if (generate_orgs.length > 0) { if (generate_orgs.length > 0) {
generateOrgCards(locale); if(useCombined){
} else if (generate_teams.length > 0) { generateOrgCardsCombined(locale);
generateTeamCards(locale);
} else if (generate_runners.length > 0) {
generateRunnersCards(locale);
} else { } else {
generateCards(locale); generateOrgCards(locale)
} }
} } else if (generate_teams.length > 0) {
generateTeamCards(locale);
} else if (generate_runners.length > 0) {
generateRunnersCards(locale);
} else {
generateCards(locale);
}
}
function generateCards(locale) { function generateCards(locale) {
toast.loading($_("generating-pdf")); toast.loading($_("generating-pdf"));
documentServer documentServer
.generateCards(generate_cards, locale) .generateCards(generate_cards, locale)
.then((blob) => { .then((blob) => {
download(blob, `${$_("runnercards")}-${locale}-${createId()}.pdf`); download(blob, `${$_("runnercards")}-${locale}-${createId()}.pdf`);
}) })
.catch((err) => { .catch((err) => {
console.error(err); console.error(err);
}); });
} }
async function generateRunnersCards(locale) { async function generateRunnersCards(locale) {
toast.loading($_("generating-pdf")); toast.loading($_("generating-pdf"));
const current_cards = await RunnerCardService.runnerCardControllerGetAll(); const current_cards = await RunnerCardService.runnerCardControllerGetAll();
let cards = []; let cards = [];
for (let runner of generate_runners) { for (let runner of generate_runners) {
let card = current_cards.find((c) => c.runner?.id == runner.id); let card = current_cards.find((c) => c.runner?.id == runner.id);
if (!card) { if (!card) {
card = await RunnerCardService.runnerCardControllerPost({ card = await RunnerCardService.runnerCardControllerPost({
runner: runner.id, runner: runner.id,
}); });
} }
cards.push(card); cards.push(card);
} }
documentServer documentServer
.generateCards(cards, locale) .generateCards(cards, locale)
.then((blob) => { .then((blob) => {
let fileName = `${$_("runnercards")}-${locale}-${createId()}.pdf`; let fileName = `${$_("runnercards")}-${locale}-${createId()}.pdf`;
if (generate_runners.length == 1) { if (generate_runners.length == 1) {
fileName = `${$_("runnercards")}_${generate_runners[0].firstname}_${ fileName = `${$_("runnercards")}_${generate_runners[0].firstname}_${
generate_runners[0].lastname generate_runners[0].lastname
}-${locale}-${createId()}.pdf`; }-${locale}-${createId()}.pdf`;
} }
download(blob, fileName); download(blob, fileName);
}) })
.catch((err) => {}); .catch((err) => {});
} }
async function generateTeamCards(locale) { async function generateTeamCards(locale) {
toast.loading($_("generating-pdfs")); toast.loading($_("generating-pdfs"));
let count = 0; let count = 0;
const current_cards = await RunnerCardService.runnerCardControllerGetAll(); const current_cards = await RunnerCardService.runnerCardControllerGetAll();
for (const t of generate_teams) { for (const t of generate_teams) {
const runners = await RunnerTeamService.runnerTeamControllerGetRunners( const runners = await RunnerTeamService.runnerTeamControllerGetRunners(
t.id t.id
); );
let cards = []; let cards = [];
for (let runner of runners) { for (let runner of runners) {
let card = current_cards.find((c) => c.runner?.id == runner.id); let card = current_cards.find((c) => c.runner?.id == runner.id);
if (!card) { if (!card) {
card = await RunnerCardService.runnerCardControllerPost({ card = await RunnerCardService.runnerCardControllerPost({
runner: runner.id, runner: runner.id,
}); });
} }
cards.push(card); cards.push(card);
} }
documentServer documentServer
.generateCards(cards, locale) .generateCards(cards, locale)
.then((blob) => { .then((blob) => {
download( download(
blob, blob,
`${$_("runnercards")}_${t.name}-${locale}-${createId()}.pdf` `${$_("runnercards")}_${t.name}-${locale}-${createId()}.pdf`
); );
}) })
.catch((err) => {}); .catch((err) => {});
} }
} }
async function generateOrgCards(locale) { async function generateOrgCards(locale) {
toast.loading($_("generating-pdfs")); toast.loading($_("generating-pdfs"));
const current_cards = await RunnerCardService.runnerCardControllerGetAll(); const current_cards = await RunnerCardService.runnerCardControllerGetAll();
let count = 0; let count = 0;
let count_orgs = 0; let count_orgs = 0;
for (const o of generate_orgs) { for (const o of generate_orgs) {
count_orgs++; count_orgs++;
let count = 0; let count = 0;
let runners = let runners =
await RunnerOrganizationService.runnerOrganizationControllerGetRunners( await RunnerOrganizationService.runnerOrganizationControllerGetRunners(
o.id, o.id,
true true
); );
let cards = []; let cards = [];
for (let runner of runners) { for (let runner of runners) {
let card = current_cards.find((c) => c.runner?.id == runner.id); let card = current_cards.find((c) => c.runner?.id == runner.id);
if (!card) { if (!card) {
card = await RunnerCardService.runnerCardControllerPost({ card = await RunnerCardService.runnerCardControllerPost({
runner: runner.id, runner: runner.id,
}); });
} }
cards.push(card); cards.push(card);
} }
await documentServer await documentServer
.generateCards(cards, locale) .generateCards(cards, locale)
.then((blob) => { .then((blob) => {
download( download(
blob, blob,
`${$_("runnercards")}_${o.name}_direct-${locale}-${createId()}.pdf` `${$_("runnercards")}_${o.name}_direct-${locale}-${createId()}.pdf`
); );
}) })
.catch((err) => {}); .catch((err) => {});
for (const t of o.teams) { for (const t of o.teams) {
count++; count++;
let runners = await RunnerTeamService.runnerTeamControllerGetRunners( let runners = await RunnerTeamService.runnerTeamControllerGetRunners(
t.id t.id
); );
let cards = []; let cards = [];
for (let runner of runners) { for (let runner of runners) {
let card = current_cards.find((c) => c.runner?.id == runner.id); let card = current_cards.find((c) => c.runner?.id == runner.id);
if (!card) { if (!card) {
card = await RunnerCardService.runnerCardControllerPost({ card = await RunnerCardService.runnerCardControllerPost({
runner: runner.id, runner: runner.id,
}); });
} }
cards.push(card); cards.push(card);
} }
await documentServer await documentServer
.generateCards(cards, locale) .generateCards(cards, locale)
.then((blob) => { .then((blob) => {
download( download(
blob, blob,
`${$_("runnercards")}_${o.name}_${ `${$_("runnercards")}_${o.name}_${
t.name t.name
}-${locale}-${createId()}.pdf` }-${locale}-${createId()}.pdf`
); );
}) })
.catch((err) => {}); .catch((err) => {});
} }
} }
} }
async function generateOrgCardsCombined(locale) {
toast.loading($_("generating-pdfs"));
const current_cards = await RunnerCardService.runnerCardControllerGetAll();
let count = 0;
let count_orgs = 0;
for (const o of generate_orgs) {
count_orgs++;
let cards = [];
let count = 0;
let runners =
await RunnerOrganizationService.runnerOrganizationControllerGetRunners(
o.id,
true
);
for (let runner of runners) {
let card = current_cards.find((c) => c.runner?.id == runner.id);
if (!card) {
card = await RunnerCardService.runnerCardControllerPost({
runner: runner.id,
});
}
cards.push(card);
}
for (const t of o.teams) {
count++;
let runners = await RunnerTeamService.runnerTeamControllerGetRunners(
t.id
);
for (let runner of runners) {
let card = current_cards.find((c) => c.runner?.id == runner.id);
if (!card) {
card = await RunnerCardService.runnerCardControllerPost({
runner: runner.id,
});
}
cards.push(card);
}
}
await documentServer
.generateCards(cards, locale)
.then((blob) => {
download(
blob,
`${$_("runnercards")}_${o.name}-${locale}-${createId()}.pdf`
);
})
.catch((err) => {});
}
}
</script> </script>
{#if cards_show} {#if cards_show}
<button <button
on:click={() => { on:click={() => {
generateRunnerCards("de"); generateRunnerCards("de");
}} }}
class="w-full justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-blue-600 text-base font-medium text-white hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 sm:w-auto sm:text-sm mb-1 lg:mb-0" on:contextmenu|preventDefault={() => {
> generateRunnerCards("de", true);
{$_("generate-runnercards")}: DE }}
</button> class="w-full justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-blue-600 text-base font-medium text-white hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 sm:w-auto sm:text-sm mb-1 lg:mb-0"
<button >
on:click={() => { {$_("generate-runnercards")}: DE
generateRunnerCards("en"); </button>
}} <button
class="w-full justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-blue-600 text-base font-medium text-white hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 sm:w-auto sm:text-sm mb-1 lg:mb-0" on:click={() => {
> generateRunnerCards("en");
{$_("generate-runnercards")}: EN }}
</button> on:contextmenu|preventDefault={() => {
generateRunnerCards("en", true);
}}
class="w-full justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-blue-600 text-base font-medium text-white hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 sm:w-auto sm:text-sm mb-1 lg:mb-0"
>
{$_("generate-runnercards")}: EN
</button>
{/if} {/if}