feat(pdf): add branding to PDF exports (header logo + footer links)
Header: centered Faunosteo logo on first page. Footer: Faunosteo logo + faunosteo.com, ZenQuality logo + "Powered by", Paws iin Harmony + @instagram handle, page numbers. Removed export date and plain-text site name from footer. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
c853c681f9
commit
33a7f47fdf
7 files changed
+314
-21
No files matched your search
+31
-3
@@ -8,6 +8,7 @@ import { addObservationPage } from './render/observationPage'
|
||||
import { addConclusionPage } from './render/conclusionPage'
|
||||
import { attachReportJson } from './attachment'
|
||||
import { drawFooter } from './render/footer'
|
||||
import { loadFaunosteoLogo, loadZenqualityLogo } from './render/logo'
|
||||
import { loadSvg } from '@/anatomy/svgLoader'
|
||||
import { parseSvgBones } from '@/anatomy/svgConvention'
|
||||
import { getSpecies } from '@/domain/species/registry'
|
||||
@@ -102,7 +103,25 @@ export async function exportReportToPdf(
|
||||
const zoneLabels = await buildZoneLabels(report)
|
||||
const findingsTree = await loadSpeciesFindings(report.animal.speciesId)
|
||||
|
||||
for (let i = 0; i < report.observations.length; i += 1) {
|
||||
let logoPdfImage = null
|
||||
let zqLogoPdfImage = null
|
||||
try {
|
||||
const logoPngBytes = await loadFaunosteoLogo(400)
|
||||
logoPdfImage = await pdfDoc.embedPng(logoPngBytes)
|
||||
} catch (err) {
|
||||
console.warn('Faunosteo logo embed failed:', err)
|
||||
}
|
||||
try {
|
||||
const zqPngBytes = await loadZenqualityLogo(300)
|
||||
zqLogoPdfImage = await pdfDoc.embedPng(zqPngBytes)
|
||||
} catch (err) {
|
||||
console.warn('ZenQuality logo embed failed:', err)
|
||||
}
|
||||
|
||||
const headerAssets = { logoPng: logoPdfImage }
|
||||
const totalObs = report.observations.length
|
||||
|
||||
for (let i = 0; i < totalObs; i += 1) {
|
||||
await addObservationPage(
|
||||
pdfDoc,
|
||||
report,
|
||||
@@ -112,12 +131,21 @@ export async function exportReportToPdf(
|
||||
zoneLabels,
|
||||
findingsTree,
|
||||
locale,
|
||||
i === 0,
|
||||
headerAssets,
|
||||
)
|
||||
}
|
||||
await addConclusionPage(pdfDoc, report, fonts, locale)
|
||||
await addConclusionPage(
|
||||
pdfDoc, report, fonts, locale,
|
||||
totalObs === 0,
|
||||
headerAssets,
|
||||
)
|
||||
|
||||
const pages = pdfDoc.getPages()
|
||||
pages.forEach((page, idx) => drawFooter(page, fonts, idx + 1, pages.length, locale))
|
||||
const footerAssets = { logoPng: logoPdfImage, zqLogoPng: zqLogoPdfImage }
|
||||
pages.forEach((page, idx) => {
|
||||
drawFooter(page, fonts, idx + 1, pages.length, locale, footerAssets)
|
||||
})
|
||||
|
||||
await attachReportJson(pdfDoc, report)
|
||||
return pdfDoc.save()
|
||||
|
||||
@@ -3,7 +3,7 @@ import type { EmbeddedFonts } from '@/pdf/fonts'
|
||||
import type { Report } from '@/domain/schema/report'
|
||||
import type { Locale } from '@/i18n/types'
|
||||
import { getDict } from '@/i18n'
|
||||
import { drawHeader } from './header'
|
||||
import { drawHeader, type HeaderAssets } from './header'
|
||||
import { formatDate } from '@/lib/dates'
|
||||
|
||||
const MARGIN_X = 36
|
||||
@@ -42,13 +42,15 @@ export async function addConclusionPage(
|
||||
report: Report,
|
||||
fonts: EmbeddedFonts,
|
||||
locale: Locale,
|
||||
isFirstPage: boolean,
|
||||
headerAssets: HeaderAssets,
|
||||
): Promise<void> {
|
||||
const dict = getDict(locale)
|
||||
const page = pdfDoc.addPage(PageSizes.A4)
|
||||
const { width, height } = page.getSize()
|
||||
const max = width - MARGIN_X * 2
|
||||
|
||||
drawHeader(page, report, fonts, locale)
|
||||
drawHeader(page, report, fonts, locale, isFirstPage, headerAssets)
|
||||
|
||||
const eyebrowY = height - EYEBROW_Y_OFFSET
|
||||
const titleY = height - TITLE_Y_OFFSET
|
||||
|
||||
+102
-12
@@ -1,12 +1,18 @@
|
||||
import { rgb, type PDFPage } from 'pdf-lib'
|
||||
import { rgb, type PDFPage, type PDFImage } from 'pdf-lib'
|
||||
import type { EmbeddedFonts } from '@/pdf/fonts'
|
||||
import type { Locale } from '@/i18n/types'
|
||||
import { appConfig } from '@/config/env'
|
||||
import { formatDate, todayIso } from '@/lib/dates'
|
||||
import { getDict } from '@/i18n'
|
||||
|
||||
const MARGIN_X = 36
|
||||
const FOOTER_Y = 28
|
||||
const FOOTER_BASE_Y = 20
|
||||
const SEPARATOR_Y = 48
|
||||
const LINE_HEIGHT = 12
|
||||
const FONT_SIZE = 8
|
||||
|
||||
export interface FooterAssets {
|
||||
logoPng: PDFImage | null
|
||||
zqLogoPng: PDFImage | null
|
||||
}
|
||||
|
||||
export function drawFooter(
|
||||
page: PDFPage,
|
||||
@@ -14,19 +20,103 @@ export function drawFooter(
|
||||
pageNumber: number,
|
||||
totalPages: number,
|
||||
locale: Locale,
|
||||
assets: FooterAssets,
|
||||
): void {
|
||||
const dict = getDict(locale)
|
||||
const { width } = page.getSize()
|
||||
const muted = rgb(0.42, 0.39, 0.35)
|
||||
const left = appConfig.footerName
|
||||
const center = `${dict['pdf.export_date_prefix']}${formatDate(todayIso(), locale)}`
|
||||
const right = `${pageNumber} / ${totalPages}`
|
||||
const accent = rgb(0.28, 0.56, 0.42)
|
||||
|
||||
page.drawText(left, { x: MARGIN_X, y: FOOTER_Y, size: 9, font: fonts.bodyRegular, color: muted })
|
||||
page.drawLine({
|
||||
start: { x: MARGIN_X, y: SEPARATOR_Y },
|
||||
end: { x: width - MARGIN_X, y: SEPARATOR_Y },
|
||||
thickness: 0.5,
|
||||
color: rgb(0.85, 0.83, 0.8),
|
||||
})
|
||||
|
||||
const centerW = fonts.bodyRegular.widthOfTextAtSize(center, 9)
|
||||
page.drawText(center, { x: (width - centerW) / 2, y: FOOTER_Y, size: 9, font: fonts.bodyRegular, color: muted })
|
||||
const logoW = 60
|
||||
const logoH = 23
|
||||
const topRowY = FOOTER_BASE_Y + LINE_HEIGHT + 2
|
||||
|
||||
const rightW = fonts.bodyRegular.widthOfTextAtSize(right, 9)
|
||||
page.drawText(right, { x: width - MARGIN_X - rightW, y: FOOTER_Y, size: 9, font: fonts.bodyRegular, color: muted })
|
||||
if (assets.logoPng) {
|
||||
page.drawImage(assets.logoPng, {
|
||||
x: MARGIN_X,
|
||||
y: topRowY,
|
||||
width: logoW,
|
||||
height: logoH,
|
||||
})
|
||||
}
|
||||
const siteUrl = 'faunosteo.com'
|
||||
page.drawText(siteUrl, {
|
||||
x: MARGIN_X + (assets.logoPng ? logoW + 4 : 0),
|
||||
y: topRowY + 6,
|
||||
size: FONT_SIZE,
|
||||
font: fonts.bodyRegular,
|
||||
color: accent,
|
||||
})
|
||||
|
||||
const poweredLabel = `${dict['footer.powered_by']} `
|
||||
const poweredLabelW = fonts.bodyRegular.widthOfTextAtSize(poweredLabel, FONT_SIZE)
|
||||
const zqLogoW = 50
|
||||
const zqLogoH = 12
|
||||
const zqFallback = 'zenquality.fr'
|
||||
const zqFallbackW = fonts.bodyRegular.widthOfTextAtSize(zqFallback, FONT_SIZE)
|
||||
const poweredBlockW = assets.zqLogoPng
|
||||
? poweredLabelW + zqLogoW
|
||||
: poweredLabelW + zqFallbackW
|
||||
const poweredX = (width - poweredBlockW) / 2
|
||||
|
||||
page.drawText(poweredLabel, {
|
||||
x: poweredX,
|
||||
y: topRowY + 6,
|
||||
size: FONT_SIZE,
|
||||
font: fonts.bodyRegular,
|
||||
color: muted,
|
||||
})
|
||||
if (assets.zqLogoPng) {
|
||||
page.drawImage(assets.zqLogoPng, {
|
||||
x: poweredX + poweredLabelW,
|
||||
y: topRowY + 2,
|
||||
width: zqLogoW,
|
||||
height: zqLogoH,
|
||||
})
|
||||
} else {
|
||||
page.drawText(zqFallback, {
|
||||
x: poweredX + poweredLabelW,
|
||||
y: topRowY + 6,
|
||||
size: FONT_SIZE,
|
||||
font: fonts.bodyRegular,
|
||||
color: accent,
|
||||
})
|
||||
}
|
||||
|
||||
const partnerText = `${dict['footer.in_partnership']} Paws iin Harmony`
|
||||
const partnerW = fonts.bodyRegular.widthOfTextAtSize(partnerText, FONT_SIZE)
|
||||
page.drawText(partnerText, {
|
||||
x: width - MARGIN_X - partnerW,
|
||||
y: topRowY + 6,
|
||||
size: FONT_SIZE,
|
||||
font: fonts.bodyRegular,
|
||||
color: muted,
|
||||
})
|
||||
|
||||
const instagramHandle = '@paws.iin.harmony'
|
||||
const handleW = fonts.bodyRegular.widthOfTextAtSize(instagramHandle, FONT_SIZE)
|
||||
page.drawText(instagramHandle, {
|
||||
x: width - MARGIN_X - handleW,
|
||||
y: FOOTER_BASE_Y + 2,
|
||||
size: FONT_SIZE,
|
||||
font: fonts.bodyRegular,
|
||||
color: accent,
|
||||
})
|
||||
|
||||
const pageText = `${pageNumber} / ${totalPages}`
|
||||
const pageW = fonts.bodyRegular.widthOfTextAtSize(pageText, FONT_SIZE)
|
||||
page.drawText(pageText, {
|
||||
x: (width - pageW) / 2,
|
||||
y: FOOTER_BASE_Y + 2,
|
||||
size: FONT_SIZE,
|
||||
font: fonts.bodyRegular,
|
||||
color: muted,
|
||||
})
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { rgb, type PDFPage } from 'pdf-lib'
|
||||
import { rgb, type PDFPage, type PDFImage } from 'pdf-lib'
|
||||
import type { EmbeddedFonts } from '@/pdf/fonts'
|
||||
import type { Report } from '@/domain/schema/report'
|
||||
import type { Locale } from '@/i18n/types'
|
||||
@@ -7,18 +7,40 @@ import { formatDate } from '@/lib/dates'
|
||||
|
||||
const MARGIN_X = 36
|
||||
const TOP_Y = 800
|
||||
const LOGO_WIDTH = 120
|
||||
const LOGO_HEIGHT = 46
|
||||
|
||||
export interface HeaderAssets {
|
||||
logoPng: PDFImage | null
|
||||
}
|
||||
|
||||
export function drawHeader(
|
||||
page: PDFPage,
|
||||
report: Report,
|
||||
fonts: EmbeddedFonts,
|
||||
locale: Locale,
|
||||
isFirstPage: boolean,
|
||||
assets: HeaderAssets,
|
||||
): void {
|
||||
const dict = getDict(locale)
|
||||
const { width } = page.getSize()
|
||||
const ink = rgb(0.12, 0.11, 0.09)
|
||||
const muted = rgb(0.42, 0.39, 0.35)
|
||||
|
||||
let contentTopY = TOP_Y
|
||||
|
||||
if (isFirstPage && assets.logoPng) {
|
||||
const logoX = (width - LOGO_WIDTH) / 2
|
||||
const logoY = TOP_Y - LOGO_HEIGHT + 20
|
||||
page.drawImage(assets.logoPng, {
|
||||
x: logoX,
|
||||
y: logoY,
|
||||
width: LOGO_WIDTH,
|
||||
height: LOGO_HEIGHT,
|
||||
})
|
||||
contentTopY = logoY - 16
|
||||
}
|
||||
|
||||
const left = [
|
||||
report.practitioner.name,
|
||||
report.practitioner.cabinet,
|
||||
@@ -28,7 +50,7 @@ export function drawHeader(
|
||||
: undefined,
|
||||
].filter(Boolean) as string[]
|
||||
|
||||
const y = TOP_Y
|
||||
const y = contentTopY
|
||||
left.forEach((line, i) => {
|
||||
page.drawText(line, {
|
||||
x: MARGIN_X,
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
const FAUNOSTEO_LOGO = '/logo-horizontal.svg'
|
||||
const ZENQUALITY_LOGO = '/brand/zenquality-logo.svg'
|
||||
|
||||
async function rasterizeSvgToCanvas(
|
||||
svgString: string,
|
||||
width: number,
|
||||
height: number,
|
||||
): Promise<HTMLCanvasElement> {
|
||||
const doc = new DOMParser().parseFromString(svgString, 'image/svg+xml')
|
||||
const root = doc.documentElement
|
||||
if (!root.getAttribute('xmlns')) {
|
||||
root.setAttribute('xmlns', 'http://www.w3.org/2000/svg')
|
||||
}
|
||||
if (!root.getAttribute('width')) root.setAttribute('width', String(width))
|
||||
if (!root.getAttribute('height')) root.setAttribute('height', String(height))
|
||||
|
||||
const normalized = new XMLSerializer().serializeToString(root)
|
||||
const blob = new Blob([normalized], { type: 'image/svg+xml;charset=utf-8' })
|
||||
const url = URL.createObjectURL(blob)
|
||||
const img = new Image()
|
||||
img.decoding = 'sync'
|
||||
img.crossOrigin = 'anonymous'
|
||||
|
||||
try {
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
img.onload = () => resolve()
|
||||
img.onerror = () => reject(new Error('Failed to rasterize logo SVG'))
|
||||
img.src = url
|
||||
})
|
||||
} finally {
|
||||
URL.revokeObjectURL(url)
|
||||
}
|
||||
|
||||
const canvas = document.createElement('canvas')
|
||||
canvas.width = width
|
||||
canvas.height = height
|
||||
const ctx = canvas.getContext('2d')
|
||||
if (!ctx) throw new Error('Canvas 2D context unavailable')
|
||||
ctx.drawImage(img, 0, 0, width, height)
|
||||
return canvas
|
||||
}
|
||||
|
||||
async function canvasToPng(canvas: HTMLCanvasElement): Promise<Uint8Array> {
|
||||
const blob = await new Promise<Blob | null>((resolve) => {
|
||||
canvas.toBlob((b) => resolve(b), 'image/png')
|
||||
})
|
||||
if (blob) return new Uint8Array(await blob.arrayBuffer())
|
||||
const dataUrl = canvas.toDataURL('image/png')
|
||||
const comma = dataUrl.indexOf(',')
|
||||
const binary = atob(dataUrl.slice(comma + 1))
|
||||
const bytes = new Uint8Array(binary.length)
|
||||
for (let i = 0; i < binary.length; i += 1) bytes[i] = binary.charCodeAt(i)
|
||||
return bytes
|
||||
}
|
||||
|
||||
async function loadSvgLogoPng(
|
||||
path: string,
|
||||
widthPx: number,
|
||||
fallbackViewBox: string,
|
||||
): Promise<Uint8Array> {
|
||||
const resp = await fetch(path)
|
||||
if (!resp.ok) throw new Error(`Logo fetch failed: HTTP ${resp.status}`)
|
||||
const svgText = await resp.text()
|
||||
|
||||
const vb = svgText.match(/viewBox="([^"]*)"/)
|
||||
const parts = (vb?.[1] ?? fallbackViewBox).split(/\s+/).map(Number)
|
||||
const ratio = parts[3] / parts[2]
|
||||
const heightPx = Math.round(widthPx * ratio)
|
||||
|
||||
const canvas = await rasterizeSvgToCanvas(svgText, widthPx, heightPx)
|
||||
return canvasToPng(canvas)
|
||||
}
|
||||
|
||||
export async function loadFaunosteoLogo(widthPx: number): Promise<Uint8Array> {
|
||||
return loadSvgLogoPng(FAUNOSTEO_LOGO, widthPx, '0 0 1070 410')
|
||||
}
|
||||
|
||||
export async function loadZenqualityLogo(widthPx: number): Promise<Uint8Array> {
|
||||
return loadSvgLogoPng(ZENQUALITY_LOGO, widthPx, '0 0 400 100')
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import type { SpeciesFindings } from '@/domain/schema/findings'
|
||||
import type { Locale } from '@/i18n/types'
|
||||
import { getDict } from '@/i18n'
|
||||
import type { ZoneLabelPair } from '@/pdf/export'
|
||||
import { drawHeader } from './header'
|
||||
import { drawHeader, type HeaderAssets } from './header'
|
||||
import { rasterizeSvg } from './svgToPng'
|
||||
import { loadSvg } from '@/anatomy/svgLoader'
|
||||
import { parseSvgBones } from '@/anatomy/svgConvention'
|
||||
@@ -275,11 +275,13 @@ export async function addObservationPage(
|
||||
zoneLabels: Map<string, ZoneLabelPair>,
|
||||
findingsTree: SpeciesFindings | null,
|
||||
locale: Locale,
|
||||
isFirstPage: boolean,
|
||||
headerAssets: HeaderAssets,
|
||||
): Promise<void> {
|
||||
const dict = getDict(locale)
|
||||
const page = pdfDoc.addPage(PageSizes.A4)
|
||||
const { width, height } = page.getSize()
|
||||
drawHeader(page, report, fonts, locale)
|
||||
drawHeader(page, report, fonts, locale, isFirstPage, headerAssets)
|
||||
|
||||
const eyebrowY = height - EYEBROW_Y_OFFSET
|
||||
const titleY = height - TITLE_Y_OFFSET
|
||||
|
||||
Reference in new issue
Block a user