mirror of
https://git.v0id.ovh/n3wt-innov/n3wt-school.git
synced 2026-04-05 20:51:26 +00:00
feat(backend,frontend): régénération et visualisation inline de la fiche élève PDF
This commit is contained in:
@ -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
|
||||
|
||||
@ -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'
|
||||
|
||||
58
Front-End/src/pages/api/generate-pdf.js
Normal file
58
Front-End/src/pages/api/generate-pdf.js
Normal 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' });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user