$root, 'config' => $config] = require __DIR__ . '/_bootstrap.php'; if ($_SERVER['REQUEST_METHOD'] !== 'POST') { Http::methodNotAllowed(['POST']); exit; } Security::validateCsrf(); (new RateLimiter($root, $config, RateLimiter::clientIp()))->check('pdf'); $maxBody = (int) ($config['max_body_bytes'] ?? 1_048_576); $body = Http::readJsonBody($maxBody > 0 ? $maxBody : null); $packId = (string) ($body['pack_id'] ?? 'onboarding-v1'); $answers = $body['answers'] ?? []; $optional = $body['optional'] ?? []; if (!is_array($answers) || !is_array($optional)) { Http::json(['error' => 'Invalid payload'], 400); exit; } $bookingUrl = (string) ($body['booking_url'] ?? ''); try { $packRepo = new PackRepository($root); $rulesRepo = new RulesRepository($root); $pack = $packRepo->getPack($packId); $rules = $rulesRepo->getRules(); $result = Engine::compute($pack, $answers, $optional, $rules); $pdf = (new PdfRenderer($root))->renderPdfBytes($pack, $result, [ 'booking_url' => $bookingUrl, 'date' => date('Y-m-d'), ]); (new Analytics($root))->track(Analytics::EVENT_PDF_GENERATED, $packId); $filename = 'controle-technique-' . ($pack['pack_id'] ?? 'resultat') . '.pdf'; // Notification admin via webhook $webhookUrl = (string) ($config['admin_notify_webhook'] ?? ''); $userEmail = (string) ($body['email'] ?? ''); if ($webhookUrl !== '' && filter_var($webhookUrl, FILTER_VALIDATE_URL)) { $ctx = ['pack_id' => $packId, 'answers' => $answers, 'optional' => $optional, 'result' => $result]; (new AdminNotifier($webhookUrl, $root . '/storage/outbox'))->notify('pdf', $ctx, $filename, $pdf, $userEmail !== '' ? $userEmail : null); } header('Content-Type: application/pdf'); header('Content-Disposition: attachment; filename="' . $filename . '"'); header('Content-Length: ' . strlen($pdf)); echo $pdf; } catch (Throwable $e) { Http::json(['error' => $e->getMessage()], 400); }