| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- <?php
- /**
- * Snippet : AuditExpress_PDF
- * Génère et télécharge le PDF à la demande (depuis la page résultats, bouton "Télécharger").
- *
- * Appelé en mode uncached via un lien direct :
- * /audit-express/pdf/?audit=<uid>
- * ou via une ressource dédiée avec ce snippet.
- *
- * Propriétés :
- * &booking_url = `https://calendly.com/livinweb/diagnostic`
- *
- * Envoie directement les headers PDF + contenu binaire, puis exit.
- */
- declare(strict_types=1);
- $corePath = $modx->getOption('ae.core_path', null,
- $modx->getOption('core_path') . 'components/audit_express/');
- $modx->addPackage('audit_express', $corePath . 'model/');
- require_once $corePath . 'src/PackRepository.php';
- require_once $corePath . 'src/PdfRenderer.php';
- $bookingUrl = $scriptProperties['booking_url'] ?? $modx->getOption('ae.booking_url', null, '');
- $auditUid = trim($_GET['audit'] ?? '');
- if (!$auditUid) {
- http_response_code(400);
- echo json_encode(['error' => 'audit_uid manquant']);
- return '';
- }
- /** @var AuditRequest|null $auditReq */
- $auditReq = $modx->getObject('AuditRequest', ['audit_uid' => $auditUid]);
- if (!$auditReq || $auditReq->get('status') === 'draft') {
- http_response_code(404);
- echo json_encode(['error' => 'Audit non trouvé ou non terminé']);
- return '';
- }
- try {
- $packId = $auditReq->get('pack_id');
- $repo = new \AuditExpress\PackRepository($corePath . 'config/');
- $pack = $repo->getPack($packId);
- $result = $auditReq->getResultArray();
- $renderer = new \AuditExpress\PdfRenderer(
- $corePath . 'templates/pdf_a4.php',
- $corePath . '../vendor/autoload.php'
- );
- $pdfBytes = $renderer->render($pack, $result, $bookingUrl);
- $filename = 'controle-technique-' . preg_replace('/[^a-z0-9\-]/', '', $packId) . '.pdf';
- header('Content-Type: application/pdf');
- header('Content-Disposition: attachment; filename="' . $filename . '"');
- header('Content-Length: ' . strlen($pdfBytes));
- header('Cache-Control: no-store');
- echo $pdfBytes;
- exit;
- } catch (\Throwable $e) {
- http_response_code(500);
- $modx->log(xPDO::LOG_LEVEL_ERROR, '[AuditExpress] PDF: ' . $e->getMessage());
- echo json_encode(['error' => 'Erreur génération PDF']);
- return '';
- }
|