Note An UnRTF v0.19.3 binary is included as an optional dependency for Windows users via node-unrtf-win32. Due to its age, it has several issues, including the inability to convert RTF documents generated from 2007 onwards and a bug in the
noPicturesoption that still generates pictures.It is recommended that applications using the
node-unrtfmodule run in a Linux environment with the latest available UnRTF binaries, which do not have these issues.
Asynchronous Node.js wrapper for the UnRTF conversion utility
UnRTF is a command-line utility for manipulating RTF documents and extracting data from them, including converting RTF files to HTML or TXT.
The node-unrtf module provides an asynchronous Node.js wrapper around the UnRTF binary for easier use.
Install using npm:
npm i node-unrtfFor Linux and macOS users, the unrtf binary will need to be installed separately.
An example of downloading the binary on a Debian system:
sudo apt-get install unrtfFor macOS users, the binary can be installed with Homebrew:
brew install unrtfPlease refer to the JSDoc comments in the source code or the generated type definitions for information on the available options.
Example of an async/await call to unRtf.convert() to convert an RTF file to HTML using ESM syntax:
import { UnRTF } from "node-unrtf";
const file = "test_document.rtf";
const unRtf = new UnRTF();
const options = {
outputHtml: true,
};
const res = await unRtf.convert(file, options);
console.log(res);Example of calling unRtf.convert() with a promise chain using CJS syntax:
"use strict";
const { UnRTF } = require("node-unrtf");
const file = "test_document.rtf";
const unRtf = new UnRTF("/usr/bin");
const options = {
outputHtml: true,
};
unRtf
.convert(file, options)
.then((res) => {
console.log(res);
return res;
})
.catch((err) => {
console.error(err);
throw err;
});UnRTF writes any images embedded in an RTF file to its working directory.
Set noPictures: true if they are not needed, though UnRTF versions before v0.20.4 have an upstream bug that writes them regardless.
If images are needed, or an older UnRTF binary is in use, give each conversion its own directory via the cwd extra and remove it afterwards.
The cwd extra only affects where images are written; the file path is still resolved relative to the process's working directory.
import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { UnRTF } from "node-unrtf";
const file = "test/files/test-rtf-complex.rtf";
const unRtf = new UnRTF();
const outputDirectory = await mkdtemp(join(tmpdir(), "unrtf-"));
try {
await unRtf.convert(
file,
{ noPictures: false, outputHtml: true },
{ cwd: outputDirectory }
);
} finally {
await rm(outputDirectory, { force: true, recursive: true });
}Contributions are welcome, and any help is greatly appreciated!
See the contributing guide for details on how to get started. Please adhere to this project's Code of Conduct when contributing.
node-unrtf is licensed under the MIT license.