19 lines
703 B
TypeScript
19 lines
703 B
TypeScript
export type PdfDefinition = Record<string, unknown>;
|
|
|
|
let fontsReady = false;
|
|
|
|
/** Renders a pdfmake document definition. pdfmake and its Roboto fonts download only when the first PDF is made. */
|
|
export async function renderPdf(definition: PdfDefinition): Promise<Blob> {
|
|
const [{ default: pdfMake }, { default: roboto }] = await Promise.all([
|
|
import('pdfmake/build/pdfmake'),
|
|
import('pdfmake/build/fonts/Roboto'),
|
|
]);
|
|
if (!fontsReady) {
|
|
pdfMake.addFontContainer(roboto);
|
|
// A document only carries inline data; it must never make pdfmake fetch an address.
|
|
pdfMake.setUrlAccessPolicy(() => false);
|
|
fontsReady = true;
|
|
}
|
|
return pdfMake.createPdf(definition).getBlob();
|
|
}
|