118 lines
3.0 KiB
TypeScript
118 lines
3.0 KiB
TypeScript
import { PageSizes, type PDFDocument } from 'pdf-lib'
|
|
import type { Report } from '@faunosteo/domain'
|
|
import {
|
|
COLOR_ACCENT,
|
|
COLOR_INK,
|
|
COLOR_MUTED,
|
|
EYEBROW_Y_OFFSET,
|
|
MARGIN_X,
|
|
SECTION_LABEL_GAP,
|
|
TITLE_RULE_OFFSET,
|
|
TITLE_RULE_WIDTH,
|
|
TITLE_Y_OFFSET,
|
|
} from './constants'
|
|
import type { RenderContext } from './context'
|
|
import { formatDate } from './dates'
|
|
import { drawHeader, type HeaderAssets } from './header'
|
|
import { drawText, wrapLines } from './text'
|
|
|
|
const SECTION_TOP_GAP = 28
|
|
const SECTION_BLOCK_GAP = 18
|
|
const TEXT_LINE_HEIGHT = 14
|
|
|
|
interface ConclusionSection {
|
|
label: string
|
|
body: string
|
|
color?: typeof COLOR_INK
|
|
}
|
|
|
|
function nextAppointmentText(report: Report, ctx: RenderContext): string {
|
|
const next = report.nextAppointment!
|
|
return next.kind === 'date' ? formatDate(next.date, ctx.locale) : next.text
|
|
}
|
|
|
|
function conclusionSections(
|
|
report: Report,
|
|
ctx: RenderContext,
|
|
): ConclusionSection[] {
|
|
const { labels } = ctx
|
|
const sections: ConclusionSection[] = [
|
|
{
|
|
label: labels['pdf.section.conclusion'],
|
|
body: report.conclusion || labels['pdf.empty'],
|
|
},
|
|
{
|
|
label: labels['pdf.section.recommendations'],
|
|
body: report.recommendations || labels['pdf.empty'],
|
|
},
|
|
]
|
|
if (report.nextAppointment) {
|
|
sections.push({
|
|
label: labels['pdf.section.next_appointment'],
|
|
body: nextAppointmentText(report, ctx) || labels['pdf.empty'],
|
|
color: COLOR_MUTED,
|
|
})
|
|
}
|
|
return sections
|
|
}
|
|
|
|
export async function addConclusionPage(
|
|
pdfDoc: PDFDocument,
|
|
report: Report,
|
|
ctx: RenderContext,
|
|
headerAssets: HeaderAssets,
|
|
): Promise<void> {
|
|
const { fonts, labels } = ctx
|
|
const page = pdfDoc.addPage(PageSizes.A4)
|
|
const { width, height } = page.getSize()
|
|
const max = width - MARGIN_X * 2
|
|
|
|
drawHeader(page, report, ctx, headerAssets)
|
|
|
|
const eyebrowY = height - EYEBROW_Y_OFFSET
|
|
const titleY = height - TITLE_Y_OFFSET
|
|
drawText(page, labels['pdf.eyebrow.synthesis'], {
|
|
x: MARGIN_X,
|
|
y: eyebrowY,
|
|
size: 9,
|
|
font: fonts.bodyBold,
|
|
color: COLOR_ACCENT,
|
|
})
|
|
drawText(page, labels['pdf.title.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
|
|
for (const { label, body, color } of conclusionSections(report, ctx)) {
|
|
drawText(page, label, {
|
|
x: MARGIN_X,
|
|
y: cursorY,
|
|
size: 9,
|
|
font: fonts.bodyBold,
|
|
color: COLOR_ACCENT,
|
|
})
|
|
cursorY -= SECTION_LABEL_GAP + 10
|
|
wrapLines(body, fonts.bodyRegular, 11, max).forEach((line) => {
|
|
drawText(page, line, {
|
|
x: MARGIN_X,
|
|
y: cursorY,
|
|
size: 11,
|
|
font: fonts.bodyRegular,
|
|
color: color ?? COLOR_INK,
|
|
})
|
|
cursorY -= TEXT_LINE_HEIGHT
|
|
})
|
|
cursorY -= SECTION_BLOCK_GAP
|
|
}
|
|
}
|