Generate Microsoft Word documents (.docx) using React components and JSX.
Personally, I’ve always felt that when it comes to building documents, XML-style templating languages are the most natural fit. The docx library is excellent — it’s well-designed, feature-rich, and gives you full control over every aspect of a Word document.
However, working with it directly can feel a bit cumbersome at times, especially when managing deeply nested structures. That’s why I built React-DOCX — not to replace or reinvent docx, but to port it into a more expressive JSX syntax.
This project uses the same types, the same structure, and still depends on docx under the hood. The only difference is that you can now use React’s familiar patterns — components, hooks, and context — to build documents in a cleaner and more declarative way.
- Direct Mapping - Props map directly to DOCX API, no abstraction layers
- React Patterns - Full support for Context, hooks, and composition
- TypeScript First - Complete type safety out of the box
- Zero Config - No build configuration needed
- Write DOCX with JSX - Use familiar React syntax to create Word documents
- Full TypeScript Support - Complete type safety with IntelliSense
- Direct DOCX API Mapping - Props map directly to the
docxlibrary API - React Patterns - Supports Context, hooks, and component composition
- Zero Configuration - Just import and start creating documents
npm install @react-docx/core
# or
yarn add @react-docx/core
# or
pnpm add @react-docx/coreimport React from "react";
import {
Document,
Section,
Paragraph,
TextRun,
renderToBuffer,
} from "@react-docx/core";
import fs from "fs";
const MyDocument = () => (
<Document>
<Section>
<Paragraph>
<TextRun text="Hello World!" bold={true} size={28} />
</Paragraph>
</Section>
</Document>
);
// Generate the document
const buffer = await renderToBuffer(<MyDocument />);
fs.writeFileSync("document.docx", buffer);Every document follows this hierarchy:
<Document>
{/* Document-level properties and metadata */}
<Section>
{/* Section-level properties (margins, headers, footers) */}
<Paragraph>
{/* Paragraph-level properties (spacing, alignment) */}
<TextRun /> {/* Text-level properties (font, size, color) */}
</Paragraph>
</Section>
</Document>The Document component accepts document-level properties including metadata and styles:
<Document
title="My Resume"
creator="John Doe"
description="Professional resume"
>
<Section>{/* content */}</Section>
</Document>Sections define page layout properties like margins, page size, headers, and footers:
<Section
properties={{
page: {
margin: {
top: 1440, // 1 inch (in twips: 1 inch = 1440 twips)
right: 1440,
bottom: 1440,
left: 1440,
},
},
}}
>
{/* content */}
</Section>Paragraphs contain text and inline elements:
<Paragraph
spacing={{ before: 200, after: 200 }}
alignment="center"
indent={{ left: 720 }}
>
<TextRun text="Centered paragraph" />
</Paragraph>TextRuns apply formatting to text:
<TextRun
text="Hello World"
bold={true}
italics={true}
size={24} // Font size in half-points (24 = 12pt)
color="2B6CB0" // Hex color without #
font="Arial"
/>Use <Break /> to add line breaks within a paragraph:
<Paragraph>
<TextRun text="First line" />
<Break />
<TextRun text="Second line" />
<Break />
<TextRun text="Third line" />
</Paragraph><Table
width={{ size: 100, type: "pct" }}
borders={{
top: { style: "single", size: 6, color: "CCCCCC" },
bottom: { style: "single", size: 6, color: "CCCCCC" },
}}
>
<TableRow>
<TableCell>
<Paragraph>
<TextRun text="Cell 1" />
</Paragraph>
</TableCell>
<TableCell>
<Paragraph>
<TextRun text="Cell 2" />
</Paragraph>
</TableCell>
</TableRow>
</Table><ExternalHyperlink link="https://github.com">
<TextRun text="Visit GitHub" color="0000FF" underline={{ type: "single" }} />
</ExternalHyperlink><PageBreak />Document- Root document componentSection- Document section with page layoutParagraph- Text paragraphTextRun- Formatted textBreak- Line break within paragraphTable- Table elementTableRow- Table rowTableCell- Table cellExternalHyperlink- External URL linkInternalHyperlink- Internal bookmark linkPageBreak- Page breakBookmark- Bookmark anchorTab- Tab characterImageRun- Embedded imageSymbolRun- Special symbolMath- Mathematical equation
renderToBuffer(element)- ReturnsPromise<Buffer>renderToBlob(element)- ReturnsPromise<Blob>renderToBase64(element)- ReturnsPromise<string>
All components have full TypeScript support. Props map directly to the docx library interfaces, so you can reference docx documentation for detailed prop options.
import {
DocumentProps,
SectionProps,
ParagraphProps,
TextRunProps,
} from "@react-docx/core";See the /examples directory for complete examples:
- basic.tsx - Basic document with text formatting
- advanced.tsx - Tables, hyperlinks, page breaks
- resume.tsx - Professional resume template
Contributions are welcome! Please feel free to submit issues or pull requests.
MIT © Silvi Lila
Built on top of the docx library by dolanmiu.