export-pdf.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. declare(strict_types=1);
  3. use App\Admin\Auth;
  4. use App\App\Bootstrap;
  5. use App\Form\FormSchema;
  6. use App\Mail\PdfGenerator;
  7. use App\Mail\SubmissionFormatter;
  8. use App\Storage\JsonStore;
  9. require dirname(__DIR__) . '/src/autoload.php';
  10. Bootstrap::init();
  11. $auth = new Auth();
  12. $auth->requireLogin();
  13. $id = trim((string) ($_GET['id'] ?? ''));
  14. $store = new JsonStore();
  15. $submission = $store->getSubmissionByKey($id);
  16. if ($submission === null) {
  17. http_response_code(404);
  18. echo 'Antrag nicht gefunden.';
  19. exit;
  20. }
  21. $schema = new FormSchema();
  22. $formatter = new SubmissionFormatter($schema);
  23. $pdfGenerator = new PdfGenerator($formatter, $schema);
  24. $pdfPath = $pdfGenerator->generateFormDataPdf($submission);
  25. if ($pdfPath === null || !is_file($pdfPath)) {
  26. http_response_code(500);
  27. echo 'PDF konnte nicht erstellt werden.';
  28. exit;
  29. }
  30. $formData = (array) ($submission['form_data'] ?? []);
  31. $firstName = trim((string) ($formData['vorname'] ?? ''));
  32. $lastName = trim((string) ($formData['nachname'] ?? ''));
  33. $namePart = trim($firstName . '_' . $lastName, '_');
  34. $downloadName = $namePart !== '' ? 'Antragsdaten_' . $namePart . '.pdf' : 'Antragsdaten.pdf';
  35. $downloadName = str_replace(["\r", "\n"], '', $downloadName);
  36. $fallbackName = preg_replace('/[^A-Za-z0-9._-]/', '_', $downloadName) ?: 'Antragsdaten.pdf';
  37. $encodedName = rawurlencode($downloadName);
  38. header('Content-Type: application/pdf');
  39. header('Content-Length: ' . (string) filesize($pdfPath));
  40. header('Content-Disposition: attachment; filename="' . $fallbackName . '"; filename*=UTF-8\'\'' . $encodedName);
  41. readfile($pdfPath);
  42. unlink($pdfPath);
  43. exit;