Easily generate .docx files with JS/TS with a nice declarative API. Works for Node and on the Browser.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Core document creation functionality including sections, properties, and metadata management for creating new Word documents.
The main class representing a Word document with all its content and configuration.
/**
* Main class representing a Word document
* @param options - Configuration options for the document
*/
class Document {
constructor(options: IPropertiesOptions);
// Properties for accessing document components
readonly Document: DocumentWrapper;
readonly Styles: Styles;
readonly CoreProperties: CoreProperties;
readonly Numbering: Numbering;
readonly Media: Media;
readonly FileRelationships: Relationships;
readonly Headers: readonly HeaderWrapper[];
readonly Footers: readonly FooterWrapper[];
readonly ContentTypes: ContentTypes;
readonly CustomProperties: CustomProperties;
readonly AppProperties: AppProperties;
readonly FootNotes: FootnotesWrapper;
readonly Settings: Settings;
readonly Comments: Comments;
readonly FontTable: FontWrapper;
}Usage Example:
import { Document } from "docx";
const doc = new Document({
sections: [{
properties: {},
children: [
new Paragraph("Hello World")
]
}],
title: "My Document",
creator: "John Doe",
description: "A sample document"
});Configuration options for creating a new document.
/**
* Configuration options for creating a Document
*/
interface IPropertiesOptions {
/** Document sections containing content and layout */
readonly sections: readonly ISectionOptions[];
/** Document title metadata */
readonly title?: string;
/** Document subject metadata */
readonly subject?: string;
/** Document creator/author metadata */
readonly creator?: string;
/** Document keywords for search */
readonly keywords?: string;
/** Document description */
readonly description?: string;
/** Last modified by user */
readonly lastModifiedBy?: string;
/** Document revision number */
readonly revision?: number;
/** External styles XML content */
readonly externalStyles?: string;
/** Document styles configuration */
readonly styles?: IStylesOptions;
/** Numbering configuration */
readonly numbering?: INumberingOptions;
/** Comments configuration */
readonly comments?: ICommentsOptions;
/** Footnotes definitions */
readonly footnotes?: Record<string, { readonly children: readonly Paragraph[] }>;
/** Document background configuration */
readonly background?: IDocumentBackgroundOptions;
/** Document features configuration */
readonly features?: {
readonly trackRevisions?: boolean;
readonly updateFields?: boolean;
};
/** Compatibility mode version */
readonly compatabilityModeVersion?: number;
/** Compatibility options */
readonly compatibility?: ICompatibilityOptions;
/** Custom document properties */
readonly customProperties?: readonly ICustomPropertyOptions[];
/** Enable different headers/footers for even/odd pages */
readonly evenAndOddHeaderAndFooters?: boolean;
/** Default tab stop distance */
readonly defaultTabStop?: number;
/** Font definitions */
readonly fonts?: readonly FontOptions[];
/** Hyphenation settings */
readonly hyphenation?: IHyphenationOptions;
}Configuration for document sections containing headers, footers, and content.
/**
* Options for document sections
*/
interface ISectionOptions {
/** Section headers for different page types */
readonly headers?: {
readonly default?: Header;
readonly first?: Header;
readonly even?: Header;
};
/** Section footers for different page types */
readonly footers?: {
readonly default?: Footer;
readonly first?: Footer;
readonly even?: Footer;
};
/** Section properties (margins, orientation, etc.) */
readonly properties?: ISectionPropertiesOptions;
/** Content elements in this section */
readonly children: readonly FileChild[];
}Usage Example:
const doc = new Document({
sections: [
{
properties: {
page: {
margin: {
top: 720, // 0.5 inches in twips
right: 720,
bottom: 720,
left: 720,
},
},
},
headers: {
default: new Header({
children: [new Paragraph("Document Header")],
}),
},
children: [
new Paragraph("Section content goes here"),
new Table({
rows: [
new TableRow({
children: [
new TableCell({
children: [new Paragraph("Cell content")],
}),
],
}),
],
}),
],
},
],
});Document header and footer elements for consistent page layout.
/**
* Document header element
*/
class Header {
constructor(options: IHeaderOptions);
}
/**
* Document footer element
*/
class Footer {
constructor(options: IFooterOptions);
}
/**
* Options for header configuration
*/
interface IHeaderOptions {
readonly children: readonly Paragraph[];
}
/**
* Options for footer configuration
*/
interface IFooterOptions {
readonly children: readonly Paragraph[];
}Configuration for document background styling.
/**
* Document background configuration
*/
interface IDocumentBackgroundOptions {
readonly color?: string;
}Configuration for custom document properties and metadata.
/**
* Custom document property configuration
*/
interface ICustomPropertyOptions {
readonly name: string;
readonly value: string | number | boolean | Date;
}Usage Example:
const doc = new Document({
sections: [{ children: [...] }],
customProperties: [
{ name: "Department", value: "Engineering" },
{ name: "Project", value: "Documentation System" },
{ name: "Version", value: 1.0 },
{ name: "Created", value: new Date() },
{ name: "Published", value: true },
],
});Install with Tessl CLI
npx tessl i tessl/npm-docx