feat(pdf): exportReportToPdf orchestrator with finalized page numbering

This commit is contained in:
Bastien Chanot committed 2026-05-18 21:53:55 +02:00
1 parent fb9a8fc3bd
commit cf57297d2d
1 file changed
+43 -7
+43 -7
View File
@@ -1,10 +1,46 @@
import { PDFDocument } from 'pdf-lib'
import type { Report } from '@/domain/schema/report'
import { embedFonts } from './fonts'
import { addObservationPage } from './render/observationPage'
import { addConclusionPage } from './render/conclusionPage'
import { attachReportJson } from './attachment'
import { drawFooter } from './render/footer'
import { loadSvg } from '@/anatomy/svgLoader'
import { parseSvgBones } from '@/anatomy/svgConvention'
import { getSpecies } from '@/domain/species/registry'
/**
* Generates a self-contained A4 PDF for the given report.
* The full report JSON is attached as a native PDF embedded file.
* Returns a Uint8Array of the PDF bytes for download.
*/
export async function exportReportToPdf(_report: Report): Promise<Uint8Array> {
throw new Error('not implemented')
async function buildZoneLabels(report: Report): Promise<Map<string, string>> {
const labels = new Map<string, string>()
const species = getSpecies(report.animal.speciesId)
if (!species) return labels
const mainRoot = await loadSvg(species.mainSvg)
parseSvgBones(mainRoot).forEach((b, id) => labels.set(id, b.fr))
for (const detailPath of Object.values(species.details)) {
const detailRoot = await loadSvg(detailPath)
parseSvgBones(detailRoot).forEach((b, id) => labels.set(id, b.fr))
}
return labels
}
export async function exportReportToPdf(report: Report): Promise<Uint8Array> {
const pdfDoc = await PDFDocument.create()
pdfDoc.setTitle(`Bilan ostéo — ${report.animal.name}`)
pdfDoc.setAuthor(report.practitioner.name)
pdfDoc.setSubject("Bilan d'ostéopathie animalière")
pdfDoc.setCreationDate(new Date())
pdfDoc.setModificationDate(new Date())
const fonts = await embedFonts(pdfDoc)
const zoneLabels = await buildZoneLabels(report)
for (let i = 0; i < report.observations.length; i += 1) {
await addObservationPage(pdfDoc, report, report.observations[i], i, fonts, zoneLabels)
}
await addConclusionPage(pdfDoc, report, fonts)
const pages = pdfDoc.getPages()
pages.forEach((page, idx) => drawFooter(page, fonts, idx + 1, pages.length))
await attachReportJson(pdfDoc, report)
return pdfDoc.save()
}