This guide shows how to use the library to render a basic rtf file # Loading rtf.js as a script ## Prerequisites - Include a promise polyfill if you have to support older browsers ## Code Put the following code into a html file: ```html ``` # Importing rtf.js as a module ## Prerequisites - Install rtf.js from npm (`npm install rtf.js`) ## Code ```javascript import {EMFJS, RTFJS, WMFJS} from 'rtf.js'; const rtf = `{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fnil\\fcharset0 Calibri;}} {\\*\\generator Msftedit 5.41.21.2510;}\\viewkind4\\uc1\\pard\\sa200\\sl276\\slmult1\\lang9\\f0\\fs22 This \\fs44 is \\fs22 a \\b simple \\ul one \\i paragraph \\ulnone\\b0 document\\i0 .\\par }`; function stringToArrayBuffer(string) { const buffer = new ArrayBuffer(string.length); const bufferView = new Uint8Array(buffer); for (let i = 0; i < string.length; i++) { bufferView[i] = string.charCodeAt(i); } return buffer; } RTFJS.loggingEnabled(false); WMFJS.loggingEnabled(false); EMFJS.loggingEnabled(false); const doc = new RTFJS.Document(stringToArrayBuffer(rtf)); const meta = doc.metadata(); doc.render().then(function(htmlElements) { console.log("Meta:"); console.log(meta); console.log("Html:"); console.log(htmlElements); }).catch(error => console.error(error)) ``` # Importing rtf.js as a module (Typescript) ## Prerequisites - Install rtf.js from npm (`npm install rtf.js`) - Disable lib check in `tsconfig.json` (add `"skipLibCheck": true`) ## Code ```typescript import {EMFJS, RTFJS, WMFJS} from 'rtf.js'; const rtf: string = `{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fnil\\fcharset0 Calibri;}} {\\*\\generator Msftedit 5.41.21.2510;}\\viewkind4\\uc1\\pard\\sa200\\sl276\\slmult1\\lang9\\f0\\fs22 This \\fs44 is \\fs22 a \\b simple \\ul one \\i paragraph \\ulnone\\b0 document\\i0 .\\par }`; function stringToArrayBuffer(string) { const buffer = new ArrayBuffer(string.length); const bufferView = new Uint8Array(buffer); for (let i = 0; i < string.length; i++) { bufferView[i] = string.charCodeAt(i); } return buffer; } RTFJS.loggingEnabled(false); WMFJS.loggingEnabled(false); EMFJS.loggingEnabled(false); const doc = new RTFJS.Document(stringToArrayBuffer(rtf)); const meta = doc.metadata(); doc.render().then(function(htmlElements) { console.log("Meta:"); console.log(meta); console.log("Html:"); console.log(htmlElements); }).catch(error => console.error(error)) ``` # Node (using jsdom) ## Prerequisites - Install rtf.js and jsdom from npm (`npm install rtf.js jsdom`) ## Code This is a minimal example for wrapper around rtf.js and jsdom: ```javascript const jsdom = require("jsdom"); const {JSDOM} = jsdom; const rtf = `{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fnil\\fcharset0 Calibri;}} {\\*\\generator Msftedit 5.41.21.2510;}\\viewkind4\\uc1\\pard\\sa200\\sl276\\slmult1\\lang9\\f0\\fs22 This \\fs44 is \\fs22 a \\b simple \\ul one \\i paragraph \\ulnone\\b0 document\\i0 .\\par }`; function stringToArrayBuffer(string) { const buffer = new ArrayBuffer(string.length); const bufferView = new Uint8Array(buffer); for (let i = 0; i < string.length; i++) { bufferView[i] = string.charCodeAt(i); } return buffer; } function runRtfjs(rtf, callback, errorCallback) { const virtualConsole = new jsdom.VirtualConsole(); virtualConsole.sendTo(console); new JSDOM(` `, { resources: "usable", runScripts: "dangerously", url: "file://" + __dirname + "/", virtualConsole, beforeParse(window) { window.rtfFile = stringToArrayBuffer(rtf); window.done = function (meta, html) { callback(meta, html); }; window.onerror = function (error) { errorCallback(error) }; } }); } runRtfjs( rtf, (meta, html) => { console.log("Meta:"); console.log(meta); console.log("Html:"); console.log(html); }, error => console.error(error)); ```