WFDASH-WEBAPP/src/lib/pdf/render.tsx

24 lines
1.1 KiB
TypeScript
Raw Normal View History

import { pdf } from "@react-pdf/renderer";
import { QuoteDocument, type QuoteDocumentProps } from "./quote-document";
/**
* Server-side render of a `<QuoteDocument>` to a PDF Buffer.
*
* Uses `@react-pdf/renderer`'s Node API (`pdf(<JSX />).toBuffer()`) which
* runs entirely in-process — no headless Chromium, no Playwright/Puppeteer,
* no browser binary in the Docker image.
*
* Always returns a fresh Buffer; never cached. Reads of letterhead,
* quote, items, and customer happen in the API route before this is
* called, so any edit in PB propagates on the next generation.
*/
export async function renderQuotePdf(props: QuoteDocumentProps): Promise<Buffer> {
const instance = pdf(<QuoteDocument {...props} />);
// `toBuffer()` works in pure Node, but under jsdom (Vitest's default
// test environment) it returns a PDFKit document handle instead. We
// funnel through `toString()` which always resolves to a string of
// raw PDF bytes, then convert to a real Node Buffer. The string path
// is ~the same performance as toBuffer in production.
const pdfString = await instance.toString();
return Buffer.from(pdfString, "binary");
}