Provides a converter which converts .docx
files (documents created by Microsoft Word) to HTML.
This package is part of the Mutaforma monorepo.
npm install @mtfm/docx-to-html
The conversion result is not persisted to disk, requiring programmatic handling to store or further process the output.
import { DocxToHtmlConverter } from "@mtfm/docx-to-html";
const converter = new DocxToHtmlConverter({
input: "path/to/the/docx/file.docx",
});
const execOpResult = converter.execute();
if (execOpResult.isErr()) {
const error = execOpResult.error;
// Handle error during execution
return;
}
const finalResult = execOpResult.value;
// Handle the conversion result
To persist the conversion result to disk, simply provide the desired output folder path via the output
property of the options object passed to the converter.
If the specified folder structure does not yet exist, it will be automatically created. Furthermore, the conversion result can still be processed programmatically.
import { DocxToHtmlConverter } from "@mtfm/docx-to-html";
const converter = new DocxToHtmlConverter({
input: "path/to/the/docx/file.docx",
output: "path/to/output/folder",
});
// Same as in minimum example
To customize the default conversion configuration, you can specify the path to a custom configuration file in JSON format using the config
property of the options object passed to the converter.
For a comprehensive overview of all available configuration settings and the required structure of the configuration file, please refer to the official Mutaforma documentation.
import { DocxToHtmlConverter } from "@mtfm/docx-to-html";
const converter = new DocxToHtmlConverter({
input: "path/to/the/docx/file.docx",
output: "path/to/output/folder",
config: "path/to/custom/config/file.json",
});
// Same as in minimum example
Name | Parameters | Returns |
---|---|---|
constructor |
IDocumentConverterOpts<...> |
DocxToHtmlConverter |
execute |
Promise<Result<IDocumentConverterResult, Error>> |
Name | Type | Mandatory | Description |
---|---|---|---|
input |
string or Buffer
|
yes | Either a path to a *.docx file on disk or an already buffered *.docx file |
output |
string |
no | A path to a folder to which the output should be saved |
config |
string |
no | Path to a custom configuration file in JSON format |
Name | Type | Mandatory | Description |
---|---|---|---|
images |
IAsset[] |
yes | All images contained within the *.docx file |
document |
IAsset |
yes | The HTML document |
Name | Type | Mandatory | Description |
---|---|---|---|
id |
string |
yes | The asset id |
data |
string |
yes | The asset data |
dataEncoding |
string |
yes | The asset data encoding |
name |
string |
yes | The name of the asset |
ext |
string |
yes | The file extension of the asset |
MIT