The low level Document and Page classes for placing elements with full control
The Document API places elements directly on a page. You create a Document, build elements
with the page factories, add them to the page or to a container, and call build. Elements
flow top to bottom by default, or sit at exact coordinates when given a position.
import { Document } from "@nemu-ai/pdf";const pdf = new Document({ page_size: "A4", margin: 50 });const page = pdf.create_page();page.add( page.text({ content: "Invoice", style: { font_size: 28, font_weight: "bold" } }), page.text({ content: "Thank you for your business.", style: { color: "#6b7280" } }),);await pdf.build("invoice.pdf");
Page factories like page.text build an element but do not place it. Add what you build with
page.add or a container’s add, or it will not render.
create_page returns a fresh page and optionally applies a theme to it. add_page appends
another page to the document. set_theme applies a theme document wide. load_font_sync
registers a font by name, with variable: true to enable weight instancing for a custom
variable font. load_image_sync registers an image by name so it can be referenced from
page.image.
build runs the engine in three passes. Measure computes each element’s intrinsic size from
font metrics. Layout assigns positions from the page margin and header zone. Render draws
every element in z index order.
header_container and footer_container are idempotent. Calling either again returns the
existing zone rather than creating a new one.
The page also exposes get_content_width() and get_content_height(), which give the
usable area inside the margins. They are handy for sizing full width containers and tables.