| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- <?php
- declare(strict_types=1);
- use App\Admin\Auth;
- use App\App\Bootstrap;
- use App\Form\FormSchema;
- use App\Mail\PdfGenerator;
- use App\Mail\SubmissionFormatter;
- use App\Storage\JsonStore;
- require dirname(__DIR__) . '/src/autoload.php';
- Bootstrap::init();
- $auth = new Auth();
- $auth->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;
|