feat(backend,frontend): régénération et visualisation inline de la fiche élève PDF

This commit is contained in:
Luc SORIGNET
2026-04-04 17:40:46 +02:00
parent 2d678b732f
commit e37aee2abc
8 changed files with 487 additions and 268 deletions

View File

@ -53,10 +53,10 @@ const FilesModal = ({
.then((parentFiles) => {
// Construct the categorized files list
const categorizedFiles = {
registrationFile: selectedRegisterForm.registration_file
registrationFile: selectedRegisterForm.student?.id
? {
name: 'Fiche élève',
url: getSecureFileUrl(selectedRegisterForm.registration_file),
url: `/api/generate-pdf?studentId=${selectedRegisterForm.student.id}`,
}
: null,
fusionFile: selectedRegisterForm.fusion_file

View File

@ -152,7 +152,11 @@ export default function ValidateSubscription({
};
const allTemplates = [
{ name: 'Fiche élève', file: student_file, type: 'main' },
{
name: 'Fiche élève',
file: `/api/generate-pdf?studentId=${studentId}`,
type: 'main',
},
...schoolFileTemplates.map((template) => ({
name: template.name || 'Document scolaire',
file: template.file,
@ -213,7 +217,11 @@ export default function ValidateSubscription({
{allTemplates[currentTemplateIndex].name || 'Document sans nom'}
</h3>
<iframe
src={getSecureFileUrl(allTemplates[currentTemplateIndex].file)}
src={
allTemplates[currentTemplateIndex].type === 'main'
? allTemplates[currentTemplateIndex].file
: getSecureFileUrl(allTemplates[currentTemplateIndex].file)
}
title={
allTemplates[currentTemplateIndex].type === 'main'
? 'Document Principal'

View File

@ -0,0 +1,58 @@
import { getToken } from 'next-auth/jwt';
const BACKEND_URL = process.env.NEXT_PUBLIC_API_URL;
export default async function handler(req, res) {
if (req.method !== 'GET') {
return res.status(405).json({ error: 'Method not allowed' });
}
const token = await getToken({
req,
secret: process.env.AUTH_SECRET,
cookieName: 'n3wtschool_session_token',
});
if (!token?.token) {
return res.status(401).json({ error: 'Non authentifié' });
}
const { studentId } = req.query;
if (!studentId) {
return res
.status(400)
.json({ error: 'Le paramètre "studentId" est requis' });
}
try {
const backendUrl = `${BACKEND_URL}/Subscriptions/registerForms/${encodeURIComponent(studentId)}/pdf`;
const backendRes = await fetch(backendUrl, {
headers: {
Authorization: `Bearer ${token.token}`,
Connection: 'close',
},
});
if (!backendRes.ok) {
return res.status(backendRes.status).json({
error: `Erreur backend: ${backendRes.status}`,
});
}
const contentType =
backendRes.headers.get('content-type') || 'application/pdf';
const contentDisposition = backendRes.headers.get('content-disposition');
res.setHeader('Content-Type', contentType);
if (contentDisposition) {
res.setHeader('Content-Disposition', contentDisposition);
}
const buffer = Buffer.from(await backendRes.arrayBuffer());
return res.send(buffer);
} catch {
return res
.status(500)
.json({ error: 'Erreur lors de la génération du PDF' });
}
}