Files
docx/docs/quickstart.md
T
DolanandClaude Opus 5.5 9239334f73 docs: pipe Packer.toStream's stream straight away in the quickstart (#3835)
The quickstart called `.then` on `Packer.toStream`'s result, but it returns the stream itself, not a promise, so the
example threw "then is not a function". The Packers page already uses the stream directly.


Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-24 22:28:42 +01:00

239 lines
5.5 KiB
Markdown

# Quickstart Guide
Get up and running with docx in under 5 minutes.
**In this guide:**
- Install docx
- Create your first document
- Add text, tables, and images
- Export in multiple formats
## Installation
```terminal
npm install --save docx
```
In Node, docx needs Node.js 16 or later. `docx/html` and `docx/embedded-files` need 16.9 or later, and `docx/layout` 18 or later.
## Your First Document
Create a "Hello World" document in just a few lines:
```ts live
import { Document, Packer, Paragraph, TextRun } from "docx";
import * as fs from "fs";
const doc = new Document({
sections: [
{
// Documents are organized into sections
children: [
new Paragraph({
children: [new TextRun("Hello World!")],
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});
```
That's it! Run this code to create your first Word document.
## Adding Common Elements
### Bold and Styled Text
```ts live
import { Document, Paragraph, TextRun } from "docx";
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
new TextRun("Normal text, "),
new TextRun({
text: "bold text, ",
bold: true,
}),
new TextRun({
text: "italic text",
italics: true,
}),
],
}),
],
},
],
});
```
See the [Text guide](usage/text.md) for all formatting options.
### Headings
```ts live
import { Document, HeadingLevel, Paragraph } from "docx";
const doc = new Document({
sections: [
{
children: [
new Paragraph({
text: "My Heading",
heading: HeadingLevel.HEADING_1,
}),
new Paragraph("Some text under the heading."),
],
},
],
});
```
### Bullet Points
```ts live
import { Document, Paragraph } from "docx";
const doc = new Document({
sections: [
{
children: [
new Paragraph({
text: "First item",
bullet: { level: 0 },
}),
new Paragraph({
text: "Second item",
bullet: { level: 0 },
}),
new Paragraph({
text: "Nested item",
bullet: { level: 1 },
}),
],
},
],
});
```
See the [Bullet Points guide](usage/bullet-points.md) for more options.
### Tables
```ts live
import { Document, Paragraph, Table, TableCell, TableRow } from "docx";
const doc = new Document({
sections: [
{
children: [
new Table({
rows: [
new TableRow({
children: [
new TableCell({ children: [new Paragraph("Cell 1")] }),
new TableCell({ children: [new Paragraph("Cell 2")] }),
],
}),
],
}),
],
},
],
});
```
See the [Tables guide](usage/tables.md) for advanced table features.
### Images
```ts live
import { Document, ImageRun, Paragraph } from "docx";
import * as fs from "fs";
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
new ImageRun({
type: "png",
data: fs.readFileSync("./demo/assets/images/dog.png"),
transformation: {
width: 200,
height: 200,
},
}),
],
}),
],
},
],
});
```
See the [Images guide](usage/images.md) for positioning and wrapping options.
## Export Options
### Node.js - Save to File
```ts
import * as fs from "fs";
Packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("document.docx", buffer);
});
```
### Node.js - Stream
```ts
import * as fs from "fs";
Packer.toStream(doc).pipe(fs.createWriteStream("document.docx"));
```
### Browser - Download
```ts
import { saveAs } from "file-saver";
Packer.toBlob(doc).then((blob) => {
saveAs(blob, "document.docx");
});
```
### Base64 String
```ts
Packer.toBase64String(doc).then((base64) => {
console.log(base64);
});
```
See the [Packers guide](usage/packers.md) for all export options.
## Next Steps
Now that you've created your first document, explore these guides:
- **[Document](usage/document.md)** - Document properties and settings
- **[Sections](usage/sections.md)** - Page layouts and breaks
- **[Paragraph](usage/paragraph.md)** - Text formatting and alignment
- **[Tables](usage/tables.md)** - Complex table layouts
- **[Images](usage/images.md)** - Image positioning and wrapping
- **[Headers & Footers](usage/headers-and-footers.md)** - Page headers and footers
- **[Styling](usage/styling-with-js.md)** - Reusable styles
?> Check out the [demo folder](https://github.com/dolanmiu/docx/tree/master/demo) for 90+ working examples covering every feature.