Skip to main content
A Doc is a list of pages, and each page is a list of content blocks. You describe what the document contains, set role styles once, and call build. Pagination, headers, footers, and font loading are handled for you. Under the hood Doc compiles its block tree down to the low level Document and Page element API and then renders. It adds no separate engine, so anything Doc produces could also be built by hand with Document.

Constructor

new Doc(options?: DocOptions)
DocOptions extends DocumentOptions and adds a few document level fields:
interface DocOptions extends DocumentOptions {
  padding?: number | MarginValues;
  header?: HeaderFooter;
  footer?: HeaderFooter;
  auto_paginate?: boolean;
}
padding
number | MarginValues
Inner inset added on top of margin. Margin and padding are summed into the page content inset.
header
HeaderFooter
A document wide header. See Headers and footers.
A document wide footer. See Headers and footers.
auto_paginate
boolean
default:"true"
Flow content across pages automatically. Set it to false to keep each page literal.
Markdown parsing on plain string text is on by default in Doc. Plain strings honor **bold**, *italic*, ~~strike~~, and $inline math$. Structured inline nodes always format regardless of that flag. See Inline content.

Methods

doc.set_style(styles: RoleStyles): this
doc.set_header(header: HeaderFooter): this
doc.set_footer(footer: HeaderFooter): this
doc.load_font(name: string, path: string, variable?: boolean): this
doc.load_image(name: string, path: string): this
doc.page(config?: PageConfig): DocPage
await doc.build(file_path: string): Promise<void>

page.content(...items: Array<Block | Block[]>): this
set_style(styles)
RoleStyles
Assigns a default style per role. Block level style merges on top. See Styling.
set_header(header)
HeaderFooter
Sets the document wide header.
Sets the document wide footer.
load_font(name, path, variable?)
string, string, boolean
Registers a custom font by name for use in font_family. Pass variable: true to enable weight instancing. See Fonts and weights.
load_image(name, path)
string, string
Registers an image by name so a block can reference it by name instead of a path.
page(config?)
PageConfig
Adds a page and returns a DocPage. The optional config sets a per page header and footer.
interface PageConfig {
  header?: HeaderFooter;
  footer?: HeaderFooter;
}
build(file_path)
string
Renders the document and writes it to disk. Always awaited.

DocPage.content

page.content(...items: Array<Block | Block[]>): this
content flattens arrays, so a component that returns Block[] can be spread inline. This makes reusable blocks composable without wrapping them in a group.

A complete example

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");

Next

Blocks

Every block type, from headings and paragraphs to tables, charts, and groups.

Styling

Role styles, block level overrides, and the style properties that matter for documents.

Headers and footers

Static, dynamic, and raw draw headers and footers, document wide or per page.

Inline content

Strong, em, strike, links, inline code, and inline formulas, with markdown shortcuts.