feat(pdf): exportReportToPdf orchestrator with finalized page numbering
This commit is contained in:
1 parent
fb9a8fc3bd
commit
cf57297d2d
1 file changed
+43
-7
+43
-7
@@ -1,10 +1,46 @@
|
|||||||
|
import { PDFDocument } from 'pdf-lib'
|
||||||
import type { Report } from '@/domain/schema/report'
|
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'
|
||||||
|
|
||||||
/**
|
async function buildZoneLabels(report: Report): Promise<Map<string, string>> {
|
||||||
* Generates a self-contained A4 PDF for the given report.
|
const labels = new Map<string, string>()
|
||||||
* The full report JSON is attached as a native PDF embedded file.
|
const species = getSpecies(report.animal.speciesId)
|
||||||
* Returns a Uint8Array of the PDF bytes for download.
|
if (!species) return labels
|
||||||
*/
|
const mainRoot = await loadSvg(species.mainSvg)
|
||||||
export async function exportReportToPdf(_report: Report): Promise<Uint8Array> {
|
parseSvgBones(mainRoot).forEach((b, id) => labels.set(id, b.fr))
|
||||||
throw new Error('not implemented')
|
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()
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user