diff options
Diffstat (limited to 'src/pdf.js')
-rw-r--r-- | src/pdf.js | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/src/pdf.js b/src/pdf.js new file mode 100644 index 0000000..a943781 --- /dev/null +++ b/src/pdf.js @@ -0,0 +1,96 @@ +import * as html_to_pdf from "html-pdf-node" + +import { prepare, gettimefromintervalls } from "./main.js" +import { c, writePDF } from './files.js' + +let date = new Date(); +date = date.toLocaleDateString("de-DE", { + weekday: 'long', + year: 'numeric', + month: 'long', + day: 'numeric', +}); + +let hinweise = "<ul>"; +for (let hinweis of c.hinweise) { + hinweise += `<li>${hinweis}</li>`; +} +hinweise += `</ul>`; + +const options = { + format: 'A4', + landscape: true, + margin: { + top: "0.5cm", + right: "1cm", + bottom: "1cm", + left: "2cm", + }, +}; + +function format() { + let tage_runtimes = prepare() + let timeout = {} + let res = ""; + + // 1h has 4 15 minute intervalls: 08 to 19 means 44 intervalls + for (let i = 0; i < 44; i++) { + let mensa = ""; + + if (11 < i && i < 25) mensa = "mensa1"; + if (25 < i && i < 27) mensa = "mensa2"; + + res += `<tr class="${mensa}">` + gettimefromintervalls(i) + + for (let day in tage_runtimes) { + if (timeout[day] > 0) { + timeout[day]--; + continue; + } + + let el = tage_runtimes[day][0]; + + if (el.name.includes("STYLECLASS")) { + let styleclass = el.name.split("/")[1].trim().toLowerCase(); + res += `<td rowspan="${el.runtime}" class="${styleclass}"></td>`; + } else { + let name = el.name == "BUFFER" ? "" : `<div><span>${el.name}</span>`; + let raum = el.raum == "BUFFER" ? "" : `<span>${el.raum}</span>`; + + res += `<td rowspan="${el.runtime}">${name}${raum}</td>`; + } + + timeout[day] = el.runtime - 1; + tage_runtimes[day].splice(0, 1); + } + + res += "</tr>" + } + + return res +} + +function generatePDF() { + let html = ` + ${c.style} + <p>Persönlicher Stundenplan von ${c.student} für das ${c.semester}. Stand: ${date}.</p> + <table> + <tr> + <th>Uhrzeit</th> + <th class="tag">Montag</th> + <th class="tag">Dienstag</th> + <th class="tag">Mittwoch</th> + <th class="tag">Donnerstag</th> + <th class="tag">Freitag</th> + </tr> + ${format()} + </table> + <br> + <em>Hinweise:</em> + ${hinweise} + `; + + html_to_pdf.generatePdf({content: html}, options).then(pdfBuffer => {writePDF(pdfBuffer)}); +} + +generatePDF() |