import { createElement as h } from "react"; import path from "node:path"; import { renderToBuffer, Document, Page, Text } from "@react-pdf/renderer"; import { describe, expect, it } from "vitest"; import { registerBundledFonts, stack } from "./register"; registerBundledFonts(path.resolve(process.cwd(), "public/fonts")); const render = (page: object, style: object) => renderToBuffer( h(Document, null, h(Page, page as never, h(Text, { style: style as never }, "Total ₹1,23,456.78 नमस्ते ಕನ್ನಡ"))), ); describe("bundled fonts render", () => { it("renders every family-stack in italic and bold-italic without throwing", async () => { for (const fontWeight of [300, 400, 500, 600, 700]) { const buf = await render({ size: "A4" }, { fontFamily: stack("IBM Plex Sans"), fontStyle: "italic", fontWeight }); expect(buf.subarray(0, 5).toString()).toBe("%PDF-"); } const mono = await render({ size: "A4" }, { fontFamily: stack("IBM Plex Mono"), fontStyle: "italic", fontWeight: 600 }); expect(mono.length).toBeGreaterThan(1000); }); it("accepts mm strings in the Page size object", async () => { const buf = await render({ size: { width: "210mm", height: "297mm" } }, { fontFamily: stack("IBM Plex Sans") }); const m = /\/MediaBox \[0 0 ([\d.]+) ([\d.]+)\]/.exec(buf.toString("latin1")); expect(m).not.toBeNull(); expect(Number(m![1])).toBeCloseTo(595.28, 1); expect(Number(m![2])).toBeCloseTo(841.89, 1); }); });