diff --git a/src/pdf/render/conclusionPage.ts b/src/pdf/render/conclusionPage.ts index 2e233099..04270163 100644 --- a/src/pdf/render/conclusionPage.ts +++ b/src/pdf/render/conclusionPage.ts @@ -1,15 +1,65 @@ -import type { PDFDocument } from 'pdf-lib' +import { rgb, PageSizes, type PDFDocument, type PDFFont } from 'pdf-lib' import type { EmbeddedFonts } from '@/pdf/fonts' import type { Report } from '@/domain/schema/report' +import { drawHeader } from './header' +import { formatDateFr } from '@/lib/dates' -/** - * Appends the final page to the PDF: conclusion, recommendations, - * next appointment. - */ +const MARGIN_X = 36 + +function wrap(text: string, font: PDFFont, size: number, max: number): string[] { + const words = text.split(/\s+/) + const lines: string[] = [] + let line = '' + for (const w of words) { + const test = line ? `${line} ${w}` : w + if (font.widthOfTextAtSize(test, size) > max && line) { + lines.push(line) + line = w + } else { + line = test + } + } + if (line) lines.push(line) + return lines +} + export async function addConclusionPage( - _pdfDoc: PDFDocument, - _report: Report, - _fonts: EmbeddedFonts, + pdfDoc: PDFDocument, + report: Report, + fonts: EmbeddedFonts, ): Promise { - throw new Error('not implemented') + const page = pdfDoc.addPage(PageSizes.A4) + const { width, height } = page.getSize() + drawHeader(page, report, fonts) + + const ink = rgb(0.12, 0.11, 0.09) + const muted = rgb(0.42, 0.39, 0.35) + const max = width - MARGIN_X * 2 + + let y = height - 80 + page.drawText('Conclusion', { x: MARGIN_X, y, size: 16, font: fonts.headingRegular, color: ink }) + y -= 20 + wrap(report.conclusion || '—', fonts.bodyRegular, 11, max).forEach((line) => { + page.drawText(line, { x: MARGIN_X, y, size: 11, font: fonts.bodyRegular, color: ink }) + y -= 14 + }) + + y -= 12 + page.drawText('Recommandations', { x: MARGIN_X, y, size: 14, font: fonts.headingRegular, color: ink }) + y -= 18 + wrap(report.recommendations || '—', fonts.bodyRegular, 11, max).forEach((line) => { + page.drawText(line, { x: MARGIN_X, y, size: 11, font: fonts.bodyRegular, color: ink }) + y -= 14 + }) + + if (report.nextAppointment) { + y -= 12 + page.drawText('Prochain rendez-vous', { x: MARGIN_X, y, size: 14, font: fonts.headingRegular, color: ink }) + y -= 18 + const txt = + report.nextAppointment.kind === 'date' + ? formatDateFr(report.nextAppointment.date) + : report.nextAppointment.text + page.drawText(txt || '—', { x: MARGIN_X, y, size: 11, font: fonts.bodyRegular, color: muted }) + } }