// Erzeugt eine PDF-Tabelle aller BEOs mit Wohnort und Straßenentfernung (km) // zur Sternwarte Welzheim — zum Versand an die BEOs zur Verifizierung. // // Daten kommen über den PHP-Proxy (cmd GET_BEOS). Das HTML wird mit Headless- // Chrome nach PDF gerendert. // node --env-file=.env erzeuge-pdf.mjs // // Ergebnis: beo-entfernungen.html und beo-entfernungen.pdf import { writeFile } from 'node:fs/promises'; import { execFile } from 'node:child_process'; import { promisify } from 'node:util'; import { fileURLToPath } from 'node:url'; import { dirname, join } from 'node:path'; const execFileP = promisify(execFile); const __dirname = dirname(fileURLToPath(import.meta.url)); const HTML_FILE = join(__dirname, 'beo-entfernungen.html'); const PDF_FILE = join(__dirname, 'beo-entfernungen.pdf'); const PHP_DB_URL = process.env.PHP_DB_URL || 'http://localhost:8080/DB4js_all.php'; const CHROME = process.env.CHROME_PATH || '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'; async function callProxy(cmd, params = {}) { const headers = { 'Content-Type': 'application/json' }; if (process.env.API_USER && process.env.API_PASS) { const token = Buffer.from(`${process.env.API_USER}:${process.env.API_PASS}`).toString('base64'); headers.Authorization = `Basic ${token}`; } const res = await fetch(PHP_DB_URL, { method: 'POST', headers, body: JSON.stringify({ cmd, ...params }), }); const json = await res.json(); if (json && !Array.isArray(json) && json.error) throw new Error(`DB4js ${cmd}: ${json.error}`); return json; } const esc = (s) => String(s ?? '').replace(/[&<>]/g, (c) => ({ '&': '&', '<': '<', '>': '>' }[c])); function fullName(r) { return [r.vorname, r.name].map((x) => (x || '').trim()).filter(Boolean).join(' '); } function buildHtml(rows) { const stand = new Date().toLocaleDateString('de-DE', { day: '2-digit', month: 'long', year: 'numeric' }); const body = rows.map((r, i) => `
Einfache Straßenentfernung in Kilometern · Stand: ${stand}
Die folgende Tabelle listet die einfache Straßenentfernung (Auto) von deinem Wohnort zur Sternwarte Welzheim. Die Werte wurden automatisch über OpenStreetMap / OSRM ermittelt. Bitte prüfe deinen Wert und melde ihn in jedem Fall zurück – auch wenn er stimmt.
| # | Name | Wohnort | km |
|---|
${rows.length} Beobachter · Sternwarte Welzheim · automatisch erzeugt
`; } async function main() { const rows = await callProxy('GET_BEOS', { what: 'id,vorname,name,ort,km' }); rows.sort((a, b) => (a.name || '').localeCompare(b.name || '', 'de')); const html = buildHtml(rows); await writeFile(HTML_FILE, html, 'utf8'); await execFileP(CHROME, [ '--headless=new', '--disable-gpu', '--no-pdf-header-footer', `--print-to-pdf=${PDF_FILE}`, HTML_FILE, ]); console.log(`PDF erzeugt: ${PDF_FILE} (${rows.length} BEOs)`); } main().catch((err) => { console.error('Fehler:', err.message); process.exit(1); });