fix(pdf): bundle the branding assets into the app (no fetch at export), export returns its warnings and the editor reports missing elements

This commit is contained in:
bchanot committed 2026-10-11 16:09:39 +02:00
1 parent 80e95ca61d
commit 293a6e0b36
22 files changed
+199 -185

No files matched your search

+16 -2
View File
@@ -28,11 +28,17 @@ import {
} from '@/services/zoneInteraction'
import type { ViewStates } from '@/anatomy/drawing'
import { exportReportToPdf } from '@/pdf/export'
import type { ExportWarning } from '@faunosteo/pdf'
import { useT } from '@/i18n/useT'
import { DEFAULT_SPECIES_ID } from './NewReportRoute'
const DISCLAIMER_FLAG = 'osteo:disclaimer-shown'
/** What the PDF lacks, named for the practitioner: logo slots and view ids. */
function missingItemsOf(warnings: readonly ExportWarning[]): string[] {
return warnings.map((w) => (w.kind === 'logo_failed' ? w.slot : w.viewId))
}
/** Editor on the reference sheet: wires the canvas, the pickers and the store. */
export default function EditorRoute() {
const navigate = useNavigate()
@@ -144,14 +150,22 @@ export default function EditorRoute() {
if (!report) return
setExporting(true)
try {
const bytes = await exportReportToPdf(report, index, { locale })
const blob = new Blob([bytes as BlobPart], { type: 'application/pdf' })
const { pdf, warnings } = await exportReportToPdf(report, index, {
locale,
})
const blob = new Blob([pdf as BlobPart], { type: 'application/pdf' })
const url = URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = url
a.download = `bilan-${report.animal.name}-${report.date}.pdf`
a.click()
URL.revokeObjectURL(url)
const missing = missingItemsOf(warnings)
if (missing.length) {
window.alert(
t('editor.export_warnings').replace('{items}', missing.join(', ')),
)
}
} catch (err) {
console.error('PDF export failed', err)
window.alert(t('editor.export_failed'))
Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

+1
View File
@@ -0,0 +1 @@
<?xml version="1.0" encoding="utf-8" ?><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="2068" height="454" viewBox="0 0 2068 454"><clipPath id="cl_3"><rect x="-2.2888184e-05" y="3.0517578e-05" width="2068.0002" height="454.00003"/></clipPath><g clip-path="url(#cl_3)"><path fill="#E8E5D5" transform="matrix(1.05834 0 0 1.05828 -96.3091 -366.163)" d="M1630.54 509.152C1649.16 452.299 1680.88 409.344 1735.32 381.86C1783.51 357.739 1839.28 353.615 1890.49 370.385C1941.77 387.339 1984.22 423.951 2008.53 472.18C2032.87 520.653 2036.88 576.832 2019.67 628.272C2002.5 679.253 1965.89 721.392 1917.81 745.519C1869.41 769.747 1813.33 773.672 1762.02 756.425C1721.47 742.891 1692.68 720.282 1665.43 688.133L1664.23 686.609C1659.68 679.541 1655.38 672.974 1650.92 665.776C1642.29 672.216 1636.95 674.239 1625.91 674.386C1611.83 674.574 1586.77 660.964 1605.01 646.255C1620.74 633.575 1622.96 660.678 1633.93 664.684C1638.67 666.414 1643.84 662.061 1647.31 659.301C1627.37 622.165 1621.49 585.666 1624.3 544.067C1624.31 539.012 1615.15 521.237 1612.63 515.947C1608.86 516.125 1605.89 516.655 1602.46 515.258C1601.24 512.899 1601.53 512.409 1602 509.816C1606.76 508.217 1624.65 509.037 1630.54 509.152Z"/><path fill="#28361D" transform="matrix(1.05834 0 0 1.05828 -96.3091 -366.163)" d="M1874.22 562.706C1881.16 544.956 1887.02 514.135 1911.2 518.499L1912.65 519.875C1910.44 531.674 1904.24 545.521 1900.42 557.04C1896.12 570.008 1893.18 582.563 1891.16 596.073C1903.32 582.008 1911.22 564.282 1920.31 548.118C1928.8 533.02 1934.53 520.391 1954.85 522.307C1955.82 523.832 1956.57 525.086 1955.67 526.98C1943.73 552.156 1931.6 577.03 1920.7 602.753C1932.27 595.961 1956.28 580.795 1969.26 584.266C1974.7 585.721 1977.46 589.614 1980.98 593.725C1967.74 600.518 1955.81 606.243 1943.68 615.122C1934.97 621.495 1924.16 633.733 1915.06 637.833C1903.35 643.111 1883.43 642.8 1871.57 648.467C1862.97 652.571 1854.31 664.53 1844.85 667.45C1837.41 669.744 1826.74 668.686 1818.75 669.281C1795.5 671.01 1778.44 672.147 1757.29 681.618C1736.63 690.876 1720.33 722.412 1711.49 727.631C1711.81 720.724 1736.31 695.447 1741.97 689.807C1745.62 686.167 1753.6 680.845 1758.14 677.685C1772.69 667.56 1819.1 669.932 1830.6 665.518C1846.05 659.587 1873.61 625.34 1884.98 612.134C1887.27 609.338 1891.91 580.192 1893.23 574.643C1896.71 560.093 1902.5 546.713 1901.1 531.77C1900.84 529.035 1904.61 525.066 1903.84 520.812C1882.84 518.604 1874.8 581.016 1867.15 593.058C1865.38 595.847 1840.28 620.697 1838.45 621.751L1837.58 621.059L1841.98 616.013L1841.13 615.018C1832.86 615.35 1831 631.455 1826.13 637.79C1827.76 628.842 1832.47 620.823 1834.14 611.622C1836.19 600.334 1836.36 589.332 1836.94 577.629C1837.17 573.007 1842.08 548.109 1840.56 546.344C1834.06 538.806 1840.3 536.21 1838.81 529.945C1820.95 534.267 1819.7 596.122 1810.93 611.487C1807.01 618.35 1801.03 623.622 1795.21 628.987C1790.85 632.785 1795.48 633.445 1791.51 635.886C1781.78 630.453 1741.33 623.363 1740.31 622.467C1762.82 614.495 1797.29 657.476 1814.11 594.806C1775.38 597.582 1742.85 601.774 1715.21 632.784C1701.56 648.107 1679.96 674.681 1665.43 688.133L1664.23 686.609C1719.75 627.097 1725.74 597.242 1814.54 592.608C1817.62 576.045 1820.09 559.305 1825.22 543.25C1829.22 530.734 1837.17 528.715 1846.72 521.13C1851.62 525.028 1843.3 552.447 1841.89 559.304C1840.27 567.456 1839.12 575.694 1838.45 583.978C1849.9 576.961 1863.36 569.575 1874.22 562.706Z"/><path fill="#E8E5D5" transform="matrix(1.05834 0 0 1.05828 -96.3091 -366.163)" d="M1941.41 524.681C1944.55 524.619 1944.43 524.566 1947.38 525.563C1948.31 529.66 1943.24 533.337 1943.2 535.493C1942.83 553.072 1933.62 566.583 1926.81 582.485C1925.19 586.26 1916.73 608.309 1915.23 609.548C1899.8 622.258 1860.53 658.025 1844.34 665.6C1841.94 665.983 1839.34 666.272 1836.92 666.59L1836.48 665.477L1837.64 664.596C1850.12 654.979 1879.73 624.619 1888.61 610.345C1889.54 608.852 1890.56 601.357 1890.86 599.075C1911.19 577.84 1923.52 535.838 1941.41 524.681Z"/><path fill="#E8E5D5" transform="matrix(1.05834 0 0 1.05828 -96.3091 -366.163)" d="M1965.12 585.188C1968.9 585.438 1972.79 586.564 1974.73 590.058L1974.11 591.316C1965.39 596.019 1944.54 611.168 1937.53 617.883C1916.06 638.453 1913.61 635.571 1886.99 642.299L1875.93 644.666C1888.99 634.161 1914 618.804 1920.54 605.287C1935.26 597.207 1948.7 588.887 1965.12 585.188Z"/><path fill="#E8E5D5" transform="matrix(1.05834 0 0 1.05828 -96.3091 -366.163)" d="M1871.5 566.906C1872.54 567 1871.93 566.823 1873.09 567.95C1866.08 593.401 1863.72 600.766 1838.45 612.252L1837.99 612.394L1837.15 611.578C1837.94 602.798 1838.19 595.386 1838.4 586.598C1849.88 580.001 1859.09 572.931 1871.5 566.906Z"/><path fill="#E8E5D5" transform="matrix(1.05834 0 0 1.05828 -96.3091 -366.163)" d="M1845.4 523.554L1846.78 525.123C1847.47 531.827 1844.52 539.117 1842.25 545.384C1837.48 539.013 1842.64 530.168 1845.4 523.554Z"/><path fill="#E8E5D5" transform="matrix(1.05834 0 0 1.05828 -96.3091 -366.163)" d="M1953.64 524.81Line truncated

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

+2 -1
View File
@@ -1,5 +1,6 @@
import { useConfig } from '@/app/providers/ConfigProvider'
import { useT } from '@/i18n/useT'
import pawsInHarmonyUrl from '@/assets/brand/paws-in-harmony.svg?url'
export default function Footer() {
const cfg = useConfig()
@@ -37,7 +38,7 @@ export default function Footer() {
className="text-[var(--color-accent)] hover:text-[var(--color-accent-strong)] transition-colors font-medium"
>
<img
src="/Paws_in_harmony.svg"
src={pawsInHarmonyUrl}
alt="Paws in Harmony"
className="h-6 w-auto inline-block align-middle"
/>
+2
View File
@@ -66,6 +66,8 @@ export const en: Dict = {
'editor.export': 'Export as PDF',
'editor.exporting': 'Generating…',
'editor.export_failed': 'PDF generation failed. Retry or contact support.',
'editor.export_warnings':
'PDF exported with missing elements: {items}. Check the browser console.',
'editor.edit_infos': 'Edit info',
'editor.edit_dialog_title': 'Edit information',
'editor.disclaimer_title': 'Legal scope of animal osteopathy',
+2
View File
@@ -72,6 +72,8 @@ export const fr: Dict = {
'editor.exporting': 'Génération…',
'editor.export_failed':
'Échec de la génération du PDF. Réessayez ou contactez le support.',
'editor.export_warnings':
'PDF exporté avec des éléments manquants : {items}. Vérifiez la console du navigateur.',
'editor.edit_infos': 'Modifier les infos',
'editor.edit_dialog_title': 'Modifier les informations',
'editor.disclaimer_title': "Périmètre légal de l'ostéopathie animale",
+17 -54
View File
@@ -1,11 +1,14 @@
import type { Branding, ImageAsset } from '@faunosteo/domain'
import headerLogoDataUrl from '@/assets/brand/faunosteo-logo-horizontal-512.png?inline'
import footerLogoDataUrl from '@/assets/brand/faunosteo-horizontal-com-512.png?inline'
import poweredByLogoDataUrl from '@/assets/brand/zenquality-logo-fr.png?inline'
import partnerLogoSvg from '@/assets/brand/paws-in-harmony.svg?raw'
/**
* The visual identity the export embeds (STEP6 §14 "Branding in the app"):
* the four `public/` files fetched by their current URLs, each failure
* leaving its slot empty with a warning (the page then prints the credit's
* fallback text), plus the credit URLs. The result travels inside the
* archive, so a re-export never needs these files again.
* the four logo files bundled with the application (`src/assets/brand`), so
* an export needs no network and no deployed file, plus the credit URLs. The
* result travels inside the archive, so a re-export never needs them again.
*/
// shortcut: the site URL and the credit constants belong to the runtime
@@ -20,62 +23,22 @@ const PARTNER = {
fallbackText: '@paws.iin.harmony',
}
const HEADER_LOGO_URL = '/assets/faunosteo_logo_horizontal-512.png'
const FOOTER_LOGO_URL = '/assets/faunosteo-horizontal-com-512.png'
const POWERED_BY_LOGO_URL = '/brand/zenquality-logo-fr.png'
const PARTNER_LOGO_URL = '/Paws_in_harmony.svg'
const PNG_DATA_URL_PREFIX = 'data:image/png;base64,'
/** btoa takes a binary string; chunking keeps the argument list small. */
function bytesToBase64(bytes: Uint8Array): string {
const CHUNK = 0x8000
let binary = ''
for (let i = 0; i < bytes.length; i += CHUNK) {
binary += String.fromCharCode(...bytes.subarray(i, i + CHUNK))
}
return btoa(binary)
}
async function fetchOk(url: string): Promise<Response> {
const resp = await fetch(url)
if (!resp.ok) throw new Error(`HTTP ${resp.status}`)
return resp
}
async function pngAsset(url: string): Promise<ImageAsset> {
const bytes = new Uint8Array(await (await fetchOk(url)).arrayBuffer())
return { kind: 'png', base64: bytesToBase64(bytes) }
}
async function svgAsset(url: string): Promise<ImageAsset> {
return { kind: 'svg', svg: await (await fetchOk(url)).text() }
}
/** A slot fails alone: the export goes on without that logo. */
async function slot(
load: Promise<ImageAsset>,
url: string,
): Promise<ImageAsset | null> {
try {
return await load
} catch (err) {
console.warn(`Branding asset ${url} skipped:`, err)
return null
/** A `?inline` PNG import is a base64 data URL; the engine wants the base64 alone. */
function pngAsset(dataUrl: string): ImageAsset {
if (!dataUrl.startsWith(PNG_DATA_URL_PREFIX)) {
throw new Error('bundled logo is not a base64 PNG data URL')
}
return { kind: 'png', base64: dataUrl.slice(PNG_DATA_URL_PREFIX.length) }
}
export async function loadBranding(): Promise<Branding> {
const [headerLogo, footerLogo, poweredByLogo, partnerLogo] =
await Promise.all([
slot(pngAsset(HEADER_LOGO_URL), HEADER_LOGO_URL),
slot(pngAsset(FOOTER_LOGO_URL), FOOTER_LOGO_URL),
slot(pngAsset(POWERED_BY_LOGO_URL), POWERED_BY_LOGO_URL),
slot(svgAsset(PARTNER_LOGO_URL), PARTNER_LOGO_URL),
])
return {
siteUrl: SITE_URL,
headerLogo,
footerLogo,
poweredBy: { logo: poweredByLogo, ...POWERED_BY },
partner: { logo: partnerLogo, ...PARTNER },
headerLogo: pngAsset(headerLogoDataUrl),
footerLogo: pngAsset(footerLogoDataUrl),
poweredBy: { logo: pngAsset(poweredByLogoDataUrl), ...POWERED_BY },
partner: { logo: { kind: 'svg', svg: partnerLogoSvg }, ...PARTNER },
}
}
+15 -5
View File
@@ -1,5 +1,9 @@
import { buildSheetSnapshot, type Report } from '@faunosteo/domain'
import { defaultPdfLabels, exportReportArchive } from '@faunosteo/pdf'
import {
defaultPdfLabels,
exportReportArchive,
type ExportWarning,
} from '@faunosteo/pdf'
import type { Locale } from '@/i18n/types'
import type { SheetIndex } from '@/services/sheetIndex'
import { loadBranding } from './branding'
@@ -9,17 +13,23 @@ export interface PdfExportOptions {
locale: Locale
}
export interface PdfExportResult {
pdf: Uint8Array
/** Non-fatal engine warnings: a logo or a view that failed to embed. */
warnings: ExportWarning[]
}
/**
* Exports the report as an archive PDF through the engine: the loaded sheet
* becomes the snapshot the pages are drawn from and the attachment carries,
* the branding is fetched once from `public/`, the browser rasterises the
* views. Non-fatal engine warnings (a logo or a view that failed) are logged.
* the branding comes from the bundled logos, the browser rasterises the
* views. Warnings are logged and returned for the caller to surface.
*/
export async function exportReportToPdf(
report: Report,
index: SheetIndex,
opts: PdfExportOptions,
): Promise<Uint8Array> {
): Promise<PdfExportResult> {
const result = await exportReportArchive({
report,
sheet: buildSheetSnapshot(index.sheet, report),
@@ -32,5 +42,5 @@ export async function exportReportToPdf(
for (const warning of result.warnings) {
console.warn('PDF export warning', warning)
}
return result.pdf
return { pdf: result.pdf, warnings: result.warnings }
}