Convert RTF to HTML in pure JavaScript.
npm install @iarna/rtf-to-htmlConvert RTF to HTML in pure JavaScript.
``js
const rtfToHTML = require('@iarna/rtf-to-html')
fs.createReadStream('example.rtf').pipe(rtfToHTML((err, html) => {
// …
})
rtfToHTML.fromStream(fs.createReadStream('example.rtf'), (err, html) => {
// …
})
rtfToHTML.fromString('{\\rtf1\\ansi\\b hi there\\b0}', (err, html) => {
console.log(html)
// prints a document containing:
//
hi there
rtf-parser
and shares its limitations.This generates complete HTML documents from RTF documents. It does not
currently have the facility to work on snippets of either.
Supported features:
* Paragraph detection (results in
tags) plus empty paragraph trimming.font-family: Font Name, Font Family
* Font (as ). Font families in RTFfont-size: #pt
don't map perfectly to HTML. This is the mapping we currently use:
* roman: serif
* swiss: sans-serif
* script: cursive
* decor: fantasy
* modern: sans-serif
* tech: monospace
* bidi: serif
* Font size (as )
* Bold (as )
* Italic (as )
* Underline (as )
* Strikethrough (as )
* Superscript (as )
* Subscript (as )color: rgb(#,#,#)
* Foreground color (as )color: rgb(#,#,#)
* Background color (as )text-indent: #pt
* Paragraph first-line indents (as )padding-left: #pt
* Idented regions (as )text-align:
* Text alignment: left, right, center, justify (as )
* opts - Optional options to pass to the HTML generator. See the section on Options for details.
* cb - A callback accepting (err, html), see the section on the Callback for details.
Returns a WritableStream that you can pipe into.
* stream - A readable stream that should contain RTF.
* opts - Optional options to pass to the HTML generator. See the section on Options for details.
* cb - A callback accepting (err, html), see the section on the Callback for details.
* string - A string containing RTF.
* opts - Optional options to pass to the HTML generator. See the section on Options for details.
* cb - A callback accepting (err, html), see the section on the Callback for details.
If we encounter an error in parsing then it will be set in err. Otherwisehtml
the resulting HTML will be in .
Options are always optional. You can configure how HTML is generated with the following:
* paraBreaks - (Defaults to \n\n) Inserted between resulting paragraphs.p
* paraTag - (Defaults to ) The tagname to use for paragraphs.`
* template - A function that is used to generate the final HTML document, defaults to:
js
function outputTemplate (doc, defaults, content) {
return
}
`You can also configure some of the starting (default) state of the output formatting with:
* disableFonts - Defaults to
true. If you set this to false then we'll output font change information when we encounter it. This is
a bit broken due to our not supporting styles.
* fontSize - Defaults to the document-wide declared font size, or if that's missing, 24.
* bold - Defaults to false
* italic - Defaults to false
* underline - Defaults to false
* strikethrough - Defaults to false
* foreground - Defaults to {red: 0, blue: 0, green: 0}
* background - Defaults to {red: 255, blue: 255, green: 255}
* firstLineIndent - Defaults to the document-wide value, or if that's missing, 0. This is how far to indent the first line of new paragraphs.
* indent: Defaults to 0
* align: Defaults to left.
* valign: Defaults to normalconst rtfToHTML = require('rtf-to-html/rtf-to-html.js')
rtfToHTML(doc[, opts]) → HTML
This is internally how the other interfaces are implemented. Unlike the
other interfaces, this one is synchronous.
* doc - A parsed RTF document as produced by the
rtf-parser` library.