requireLogin(); $id = trim((string) ($_GET['id'] ?? '')); $store = new JsonStore(); $submission = $store->getSubmissionByKey($id); if ($submission === null) { http_response_code(404); echo 'Antrag nicht gefunden.'; exit; } $schema = new FormSchema(); $formatter = new SubmissionFormatter($schema); $pdfGenerator = new PdfGenerator($formatter, $schema); $pdfPath = $pdfGenerator->generateFormDataPdf($submission); if ($pdfPath === null || !is_file($pdfPath)) { http_response_code(500); echo 'PDF konnte nicht erstellt werden.'; exit; } $formData = (array) ($submission['form_data'] ?? []); $firstName = trim((string) ($formData['vorname'] ?? '')); $lastName = trim((string) ($formData['nachname'] ?? '')); $namePart = trim($firstName . '_' . $lastName, '_'); $downloadName = $namePart !== '' ? 'Antragsdaten_' . $namePart . '.pdf' : 'Antragsdaten.pdf'; $downloadName = str_replace(["\r", "\n"], '', $downloadName); $fallbackName = preg_replace('/[^A-Za-z0-9._-]/', '_', $downloadName) ?: 'Antragsdaten.pdf'; $encodedName = rawurlencode($downloadName); header('Content-Type: application/pdf'); header('Content-Length: ' . (string) filesize($pdfPath)); header('Content-Disposition: attachment; filename="' . $fallbackName . '"; filename*=UTF-8\'\'' . $encodedName); readfile($pdfPath); unlink($pdfPath); exit;