DOCX Export
It's possible to export BlockNote documents to docx, completely client-side.
This feature is provided by the @blocknote/xl-docx-exporter. xl- packages
are fully open source, but released under a copyleft license. A commercial
license for usage in closed source, proprietary products comes as part of the
Business subscription.
First, install the @blocknote/xl-docx-exporter and docx packages:
npm install @blocknote/xl-docx-exporter docxThen, create an instance of the DOCXExporter class. This exposes the following methods:
import {
DOCXExporter,
docxDefaultSchemaMappings,
} from "@blocknote/xl-docx-exporter";
import { Packer } from "docx";
// Create the exporter
const exporter = new DOCXExporter(editor.schema, docxDefaultSchemaMappings);
// Convert the blocks to a docxjs document
const docxDocument = await exporter.toDocxJsDocument(editor.document);
// Use docx to write to file:
await Packer.toBuffer(docxDocument);See the full example below:
Customizing the Docx output file
toDocxJsDocument takes an optional options parameter, which allows you to customize document metadata (like the author) and section options (like headers and footers).
Example usage:
import { Paragraph, TextRun } from "docx";
const doc = await exporter.toDocxJsDocument(testDocument, {
documentOptions: {
creator: "John Doe",
},
sectionOptions: {
headers: {
default: {
options: {
children: [new Paragraph({ children: [new TextRun("Header")] })],
},
},
},
footers: {
default: {
options: {
children: [new Paragraph({ children: [new TextRun("Footer")] })],
},
},
},
},
});Custom mappings / custom schemas
The DOCXExporter constructor takes a schema, mappings and options parameter.
A mapping defines how to convert a BlockNote schema element (a Block, Inline Content, or Style) to a docxjs element.
If you're using a custom schema in your editor, or if you want to overwrite how default BlockNote elements are converted to docx, you can pass your own mappings:
For example, use the following code in case your schema has an extraBlock type:
import {
DOCXExporter,
docxDefaultSchemaMappings,
} from "@blocknote/xl-docx-exporter";
import { Paragraph, TextRun } from "docx";
new DOCXExporter(schema, {
blockMapping: {
...docxDefaultSchemaMappings.blockMapping,
myCustomBlock: (block, exporter) => {
return new Paragraph({
children: [
new TextRun({
text: "My custom block",
}),
],
});
},
},
inlineContentMapping: docxDefaultSchemaMappings.inlineContentMapping,
styleMapping: docxDefaultSchemaMappings.styleMapping,
});Math & diagram blocks
The math and diagram blocks ship their own DOCX mappings — math exports as native (editable) Word equations, diagrams as embedded images. See exporting math and exporting diagrams for the setup.
Exporter options
The DOCXExporter constructor takes an optional options parameter.
While conversion happens on the client-side, the default setup uses a server hosted proxy to resolve files:
const defaultOptions = {
// a function to resolve external resources in order to avoid CORS issues
// by default, this calls a BlockNote hosted server-side proxy to resolve files
resolveFileUrl: corsProxyResolveFileUrl,
// the strings rendered into the exported document (file link texts, error
// placeholders); pass a locale from @blocknote/core/locales (or your
// editor's dictionary) to export in another language
dictionary: locales.en,
// the colors to use in the Docx for things like highlighting, background colors and font colors.
colors: COLORS_DEFAULT, // defaults from @blocknote/core
};Fonts
DOCX embeds Inter 18pt for body text and Geist Mono for code by default.
Set fontFamily or monoFontFamily to change the styles. To use a custom
family on devices where it isn't installed, embed it with fonts:
import { DOCXExporter, docxDefaultSchemaMappings } from "@blocknote/xl-docx-exporter";
const response = await fetch("/fonts/Lato-Regular.ttf");
if (!response.ok) throw new Error("Could not load Lato");
const exporter = new DOCXExporter(editor.schema, docxDefaultSchemaMappings, {
fontFamily: "Lato",
fonts: [{ name: "Lato", regular: await response.blob() }],
});Use static TrueType files that permit embedding, with one entry per family
and the name declared in the files. Each face accepts a Blob (including
File), ArrayBuffer, or Uint8Array. Add bold, italic, and
boldItalic when available. A supplied list replaces all bundled faces,
including the code font; use fonts: [] to embed none.
To use a font installed on the reader's machine without embedding it, set
its family name (for example, fontFamily: "Marianne"). For WOFF or WOFF2,
unpack it once at build time
and pass the resulting static .ttf file to fonts. If the result is
CFF/OpenType, use the installed family or obtain a static TrueType version.
An unreadable file or unsupported format rejects the export with a
FontEmbeddingError. Use the family and face in the error to identify
which file to replace before retrying.
If you supply documentOptions.fonts or documentOptions.externalStyles
when exporting, those override the corresponding generated values.