Files
faunosteo/src/pdf/render/conclusionPage.ts
T
Bastien ChanotandClaude d7ea99e040 fix(pdf): stack eyebrow + title without overlap, color-code severity pill
Eyebrow OBSERVATION / SYNTHÈSE now baseline-anchored at y=height-95;
title baseline at y=height-122. 27pt separation between baselines so
the Times-22 ascender no longer reaches into the eyebrow line.

Severity next to the observation number is rendered as a filled pill
with white uppercase label. Background color encodes severity at a
glance:
- légère  → sage green
- modérée → amber
- sévère  → red

Co-Authored-By: Claude <noreply@anthropic.com>
2026-05-19 06:14:14 +02:00

112 lines
2.9 KiB
TypeScript

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'
const MARGIN_X = 36
const EYEBROW_Y_OFFSET = 95
const TITLE_Y_OFFSET = 122
const TITLE_RULE_OFFSET = 12
const TITLE_RULE_WIDTH = 48
const SECTION_TOP_GAP = 28
const SECTION_LABEL_GAP = 4
const SECTION_BLOCK_GAP = 18
const TEXT_LINE_HEIGHT = 14
const COLOR_INK = rgb(0.12, 0.11, 0.09)
const COLOR_MUTED = rgb(0.42, 0.39, 0.35)
const COLOR_ACCENT = rgb(0.72, 0.35, 0.25)
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,
): Promise<void> {
const page = pdfDoc.addPage(PageSizes.A4)
const { width, height } = page.getSize()
const max = width - MARGIN_X * 2
drawHeader(page, report, fonts)
const eyebrowY = height - EYEBROW_Y_OFFSET
const titleY = height - TITLE_Y_OFFSET
page.drawText('SYNTHÈSE', {
x: MARGIN_X,
y: eyebrowY,
size: 9,
font: fonts.bodyBold,
color: COLOR_ACCENT,
})
page.drawText('Conclusion', {
x: MARGIN_X,
y: titleY,
size: 22,
font: fonts.headingRegular,
color: COLOR_INK,
})
page.drawLine({
start: { x: MARGIN_X, y: titleY - TITLE_RULE_OFFSET },
end: { x: MARGIN_X + TITLE_RULE_WIDTH, y: titleY - TITLE_RULE_OFFSET },
thickness: 2,
color: COLOR_ACCENT,
})
let cursorY = titleY - SECTION_TOP_GAP
const sections: Array<{ label: string; body: string; color?: typeof COLOR_INK }> = [
{ label: 'CONCLUSION', body: report.conclusion || '—' },
{ label: 'RECOMMANDATIONS', body: report.recommendations || '—' },
]
if (report.nextAppointment) {
const txt =
report.nextAppointment.kind === 'date'
? formatDateFr(report.nextAppointment.date)
: report.nextAppointment.text
sections.push({
label: 'PROCHAIN RENDEZ-VOUS',
body: txt || '—',
color: COLOR_MUTED,
})
}
for (const { label, body, color } of sections) {
page.drawText(label, {
x: MARGIN_X,
y: cursorY,
size: 9,
font: fonts.bodyBold,
color: COLOR_ACCENT,
})
cursorY -= SECTION_LABEL_GAP + 10
wrap(body, fonts.bodyRegular, 11, max).forEach((line) => {
page.drawText(line, {
x: MARGIN_X,
y: cursorY,
size: 11,
font: fonts.bodyRegular,
color: color ?? COLOR_INK,
})
cursorY -= TEXT_LINE_HEIGHT
})
cursorY -= SECTION_BLOCK_GAP
}
}