Files
faunosteo/apps/app/src/pdf/render/header.ts
T

101 lines
2.5 KiB
TypeScript

import type { PDFPage, PDFImage } from 'pdf-lib'
import type { EmbeddedFonts } from '@/pdf/fonts'
import type { Report } from '@faunosteo/domain'
import type { Locale } from '@/i18n/types'
import { getDict } from '@/i18n'
import { formatDate } from '@/lib/dates'
import { addLinkAnnotation } from './linkAnnotation'
import { MARGIN_X, COLOR_INK, COLOR_MUTED } from './constants'
const TOP_Y = 800
const LOGO_WIDTH = 100
const LOGO_HEIGHT = 38
export interface HeaderAssets {
logoPng: PDFImage | null
}
export function drawHeader(
page: PDFPage,
report: Report,
fonts: EmbeddedFonts,
locale: Locale,
assets: HeaderAssets,
): void {
const dict = getDict(locale)
const { width } = page.getSize()
const y = TOP_Y
const left = [
report.practitioner.name,
report.practitioner.cabinet,
report.practitioner.contact,
report.practitioner.siret
? `${dict['pdf.siret_prefix']}${report.practitioner.siret}`
: undefined,
].filter(Boolean) as string[]
left.forEach((line, i) => {
page.drawText(line, {
x: MARGIN_X,
y: y - i * 12,
size: i === 0 ? 11 : 9,
font: i === 0 ? fonts.bodyBold : fonts.bodyRegular,
color: i === 0 ? COLOR_INK : COLOR_MUTED,
})
})
if (assets.logoPng) {
const logoX = (width - LOGO_WIDTH) / 2
const logoY = y - LOGO_HEIGHT + 10
page.drawImage(assets.logoPng, {
x: logoX,
y: logoY,
width: LOGO_WIDTH,
height: LOGO_HEIGHT,
})
addLinkAnnotation(page, {
x: logoX,
y: logoY,
width: LOGO_WIDTH,
height: LOGO_HEIGHT,
url: 'https://faunosteo.com',
})
}
const animalLine = [
report.animal.name,
report.animal.breed ? `(${report.animal.breed})` : '',
report.animal.age ? `· ${report.animal.age}` : '',
]
.filter(Boolean)
.join(' ')
const right = [
animalLine,
`${dict['pdf.owner_prefix']}${report.owner.name}`,
formatDate(report.date, locale),
]
right.forEach((line, i) => {
const size = i === 0 ? 11 : 9
const font = i === 0 ? fonts.bodyBold : fonts.bodyRegular
const w = font.widthOfTextAtSize(line, size)
page.drawText(line, {
x: width - MARGIN_X - w,
y: y - i * 12,
size,
font,
color: i === 0 ? COLOR_INK : COLOR_MUTED,
})
})
const separatorY = y - Math.max(left.length * 12, LOGO_HEIGHT) - 8
page.drawLine({
start: { x: MARGIN_X, y: separatorY },
end: { x: width - MARGIN_X, y: separatorY },
thickness: 0.5,
color: COLOR_MUTED,
})
}