Skip to main content
This page builds one small PDF with each API. Both write a file and are awaited.

With the Doc API

Describe the document as content blocks, set role styles once, and call build.
1

Create a Doc

import { Doc } from "@nemu-ai/pdf";

const doc = new Doc({ page_size: "A4", margin: 54, padding: 10 });
2

Set role styles

Styles assigned per role apply to every block of that type.
doc.set_style({
  heading: { font_family: "inter", color: "#111827" },
  paragraph: { font_family: "source-serif-4", font_size: 11.5, line_height: 1.6 },
  link: { color: "#2563eb" },
});
3

Add content

doc.page().content(
  { type: "heading", text: "Quarterly Report", level: 1 },
  { type: "paragraph", text: "Revenue grew across every region this quarter." },
  { type: "note", variant: "info", title: "Note", text: "Figures are unaudited." },
);
4

Build the file

await doc.build("report.pdf");
The full script:
import { Doc } from "@nemu-ai/pdf";

const doc = new Doc({ page_size: "A4", margin: 54, padding: 10 });

doc.set_style({
  heading: { font_family: "inter", color: "#111827" },
  paragraph: { font_family: "source-serif-4", font_size: 11.5, line_height: 1.6 },
  link: { color: "#2563eb" },
});

doc.page().content(
  { type: "heading", text: "Quarterly Report", level: 1 },
  { type: "paragraph", text: "Revenue grew across every region this quarter." },
  { type: "note", variant: "info", title: "Note", text: "Figures are unaudited." },
);

await doc.build("report.pdf");

Explore the Doc API

Blocks, inline content, styling, fonts, formulas, tables, charts, and more.

With the Document API

Build elements with the page factories, then place them with page.add.
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.

Explore the Document API

Elements, containers, layout, tables, themes, and positioning.

Choosing an API

Use Doc when

You are writing a document: a report, an article, an invoice. You want pagination, headers, and footers handled for you.

Use Document when

You need exact coordinates, custom layouts, overlapping elements, or per element control.