import { createAutoClient, type RenderClient } from "../pdf/render/client"; import type { UserFace } from "../pdf/fonts/userFontStore"; import { api } from "./api"; let client: RenderClient | null = null; let lastId = 0; /** The one render client (and so one worker) shared by the preview and export. */ export function getRenderClient(): RenderClient { client ??= createAutoClient({ // Font metadata comes from the database row, the bytes over the binary IPC; both by the file's sha256. loadUserFont: async (sha256) => { const rows = await api.listUserFonts(true); const row = rows.find((r) => r.sha256 === sha256); if (!row) throw new Error("An imported font used by this invoice is no longer in the font library"); const bytes = await api.getUserFontBytes(sha256); return { face: row.face as UserFace, weight: row.weight, italic: row.style === "italic", sha256, bytes }; }, }); return client; } export const nextRenderJobId = (): number => ++lastId;