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:
1 parent
80e95ca61d
commit
293a6e0b36
22 files changed
+199
-185
No files matched your search
@@ -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 },
|
||||
}
|
||||
}
|
||||
@@ -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 }
|
||||
}
|
||||
Reference in new issue
Block a user