PdfRenderer
Keep fonts across renders with PdfRenderer, fetch a document's assets ahead with prepare(), and measure a band with measure().
The render(), prepare(), and measure() functions use one renderer shared by every call. Create a PdfRenderer to keep a font registry of your own, such as one per tenant.
import { } from "takumi-pdf";
const = new ();
await .("https://takumi.kane.tw/fonts/Geist.woff2");
const = await .("<h1>Invoice 1042</h1>");A registered font stays until you remove it. A font listed in fonts is reused while the renderer's cache keeps it, and loads again after the cache drops it.
| Method | Effect |
|---|---|
render(input, options?) | Renders to PDF bytes. Takes the render() options |
prepare(input, options?) | Loads a document's fonts and images, and returns a prepared document |
measure(input, options?) | Returns the laid-out { width, height } in CSS px |
registerFont(font) | Registers a font that stays until you remove it |
unregisterFont({ name }) | Removes every face of a family |
clearFonts() | Removes every registered font except the bundled fallback |
free() | Releases the renderer's WebAssembly memory |
new PdfRenderer({ cache }) sets the memory the renderer keeps between renders. See Caching.
prepare()
prepare() lays nothing out. It downloads every image and @font-face file the document draws, and the prepared document keeps the images. A @font-face file the cache drops before the render is fetched again. Its options are the layout options of render(). Its images option also takes the fetch settings timeout, maxBytes, allowUrl, and your own fetch.
const = await (, { : "a4", : { : 5000 } });
const = await (, { : true });A prepared document keeps its page setup. render() then takes only the options that change how its pages are written: metadata, outline, backgroundColor, uncoveredText, pdfa, tagged, attachments, and signal.
measure()
measure() lays a document out without rendering it and resolves to its size in CSS px. With page options it lays out against the full page width with unbounded height, as a header or footer band does, so the height is the margin the band needs.
import { } from "takumi-pdf";
const { } = await ("<footer>Page 1 of 12</footer>", { : "a4" });The size is the document's own, not the space it laid out in. A box with width: 100px measures 100 wide on any page. See Sizing the margin.
Last updated on