You can use this converter
However it does not address bullet points (ul, li elements)
function convertHtmlToRtf(html) {
if (!(typeof html === "string" && html)) {
return null;
}
var tmpRichText, hasHyperlinks;
var richText = html;
// Singleton tags
richText = richText.replace(/<(?:hr)(?:\s+[^>]*)?\s*[\/]?>/ig, "{\\pard \\brdrb \\brdrs \\brdrw10 \\brsp20 \\par}\n{\\pard\\par}\n");
richText = richText.replace(/<(?:br)(?:\s+[^>]*)?\s*[\/]?>/ig, "{\\pard\\par}\n");
// Empty tags
richText = richText.replace(/<(?:p|div|section|article)(?:\s+[^>]*)?\s*[\/]>/ig, "{\\pard\\par}\n");
richText = richText.replace(/<(?:[^>]+)\/>/g, "");
// Hyperlinks
richText = richText.replace(
/<a(?:\s+[^>]*)?(?:\s+href=(["'])(?:javascript:void\(0?\);?|#|return false;?|void\(0?\);?|)\1)(?:\s+[^>]*)?>/ig,
"{{{\n");
tmpRichText = richText;
richText = richText.replace(
/<a(?:\s+[^>]*)?(?:\s+href=(["'])(.+)\1)(?:\s+[^>]*)?>/ig,
"{\\field{\\*\\fldinst{HYPERLINK\n \"$2\"\n}}{\\fldrslt{\\ul\\cf1\n");
hasHyperlinks = richText !== tmpRichText;
richText = richText.replace(/<a(?:\s+[^>]*)?>/ig, "{{{\n");
richText = richText.replace(/<\/a(?:\s+[^>]*)?>/ig, "\n}}}");
// Start tags
richText = richText.replace(/<(?:b|strong)(?:\s+[^>]*)?>/ig, "{\\b\n");
richText = richText.replace(/<(?:i|em)(?:\s+[^>]*)?>/ig, "{\\i\n");
richText = richText.replace(/<(?:u|ins)(?:\s+[^>]*)?>/ig, "{\\ul\n");
richText = richText.replace(/<(?:strike|del)(?:\s+[^>]*)?>/ig, "{\\strike\n");
richText = richText.replace(/<sup(?:\s+[^>]*)?>/ig, "{\\super\n");
richText = richText.replace(/<sub(?:\s+[^>]*)?>/ig, "{\\sub\n");
richText = richText.replace(/<(?:p|div|section|article)(?:\s+[^>]*)?>/ig, "{\\pard\n");
// End tags
richText = richText.replace(/<\/(?:p|div|section|article)(?:\s+[^>]*)?>/ig, "\n\\par}\n");
richText = richText.replace(/<\/(?:b|strong|i|em|u|ins|strike|del|sup|sub)(?:\s+[^>]*)?>/ig, "\n}");
// Strip any other remaining HTML tags [but leave their contents]
richText = richText.replace(/<(?:[^>]+)>/g, "");
// Prefix and suffix the rich text with the necessary syntax
richText =
"{\\rtf1\\ansi\n" + (hasHyperlinks ? "{\\colortbl\n;\n\\red0\\green0\\blue255;\n}\n" : "") + richText + "\n}";
return richText;
}
Answer from Samra on Stack Overflow Top answer 1 of 5
12
You can use this converter
However it does not address bullet points (ul, li elements)
function convertHtmlToRtf(html) {
if (!(typeof html === "string" && html)) {
return null;
}
var tmpRichText, hasHyperlinks;
var richText = html;
// Singleton tags
richText = richText.replace(/<(?:hr)(?:\s+[^>]*)?\s*[\/]?>/ig, "{\\pard \\brdrb \\brdrs \\brdrw10 \\brsp20 \\par}\n{\\pard\\par}\n");
richText = richText.replace(/<(?:br)(?:\s+[^>]*)?\s*[\/]?>/ig, "{\\pard\\par}\n");
// Empty tags
richText = richText.replace(/<(?:p|div|section|article)(?:\s+[^>]*)?\s*[\/]>/ig, "{\\pard\\par}\n");
richText = richText.replace(/<(?:[^>]+)\/>/g, "");
// Hyperlinks
richText = richText.replace(
/<a(?:\s+[^>]*)?(?:\s+href=(["'])(?:javascript:void\(0?\);?|#|return false;?|void\(0?\);?|)\1)(?:\s+[^>]*)?>/ig,
"{{{\n");
tmpRichText = richText;
richText = richText.replace(
/<a(?:\s+[^>]*)?(?:\s+href=(["'])(.+)\1)(?:\s+[^>]*)?>/ig,
"{\\field{\\*\\fldinst{HYPERLINK\n \"$2\"\n}}{\\fldrslt{\\ul\\cf1\n");
hasHyperlinks = richText !== tmpRichText;
richText = richText.replace(/<a(?:\s+[^>]*)?>/ig, "{{{\n");
richText = richText.replace(/<\/a(?:\s+[^>]*)?>/ig, "\n}}}");
// Start tags
richText = richText.replace(/<(?:b|strong)(?:\s+[^>]*)?>/ig, "{\\b\n");
richText = richText.replace(/<(?:i|em)(?:\s+[^>]*)?>/ig, "{\\i\n");
richText = richText.replace(/<(?:u|ins)(?:\s+[^>]*)?>/ig, "{\\ul\n");
richText = richText.replace(/<(?:strike|del)(?:\s+[^>]*)?>/ig, "{\\strike\n");
richText = richText.replace(/<sup(?:\s+[^>]*)?>/ig, "{\\super\n");
richText = richText.replace(/<sub(?:\s+[^>]*)?>/ig, "{\\sub\n");
richText = richText.replace(/<(?:p|div|section|article)(?:\s+[^>]*)?>/ig, "{\\pard\n");
// End tags
richText = richText.replace(/<\/(?:p|div|section|article)(?:\s+[^>]*)?>/ig, "\n\\par}\n");
richText = richText.replace(/<\/(?:b|strong|i|em|u|ins|strike|del|sup|sub)(?:\s+[^>]*)?>/ig, "\n}");
// Strip any other remaining HTML tags [but leave their contents]
richText = richText.replace(/<(?:[^>]+)>/g, "");
// Prefix and suffix the rich text with the necessary syntax
richText =
"{\\rtf1\\ansi\n" + (hasHyperlinks ? "{\\colortbl\n;\n\\red0\\green0\\blue255;\n}\n" : "") + richText + "\n}";
return richText;
}
2 of 5
7
After a bit of search I found a working solution:
https://www.npmjs.com/package/html-to-rtf
With html-to-rtf the conversion is easy (here's a piece of code based on browserify):
var htmlToRtf = require('html-to-rtf');
var htmlText = "<div>...</div>"; //or whatever html you want to transform
var htmlAsRtf = htmlToRtf.convertHtmlToRtf(htmlText); // html transformed to rtf
This solution worked for me. Without browserify you'll have to find implied js inside downloaded modules with npm and link them to your html page.
npm
npmjs.com › package › html-to-rtf
html-to-rtf - npm
February 20, 2023 - var htmlToRtf = require('html-to-rtf'); var html = ` <h1>Title <span style="color:rgb(255,0,0);">with</span> tag h1<h1> <div> <p style="color:#333; margin:5px;" class="test" align="center"> text of paragraph <b>text with bold <i>text with italic and bold</i></b><i>text with italic</i> </p> <p style="color:rgb(255,0,0);" align="right">red paragraph => right with tag</p> <p style="color:rgb(0,0,255); text-align:center;">blue paragraph => center with style</p> <table> <tbody> <tr> <td><mark>column 1</mark></td> <td>column 2</td> <td><mark>column 3</mark></td> <td>column 4</td> </tr> <tr> <td>content 1</td> <td>content 2<br></td> <td>content 3<br></td> <td>content 4<br></td> </tr> </tbody> </table> </div> ` htmlToRtf.saveRtfInFile('<Path>/<FileName>.rtf', htmlToRtf.convertHtmlToRtf(html))
» npm install html-to-rtf
Published Feb 20, 2023
Version 2.1.0
Convert html to rtf and back again
I’m building a very simple web page editor. My intention is to load and save html files, and edit them in a styled text area in my editor app. To handle the loading and saving, I need to convert between html and rtf. I found a couple of previous discussions on the forum for doing this. More on forum.xojo.com
Convert RTF to HTML in Javascript - Stack Overflow
Is there a way to convert RichTextFormat to HTML in Javascript? Iam trying to paste the RTF content copied , from clipboard iam getting the text/rtf content, now i need to show it with all styles More on stackoverflow.com
Treat/Convert Rich Text Editor Value as HTML
I would like to parse my rich text editor contents into a JSON format. This would be simple if I could use something like querySelectorAll but I cannot seem to find a way to convert the value from the editor into a DOM node. Calling on domparser returns error: "Failed to execute 'postMessage' ... More on community.retool.com
Convert HTML clipboard content to RTF?
This forum has many impressive routines that convert RTF to HTML, but I can't find anything that does the reverse conversion: converts HTML text in the clipboard to RTF. I know that this is only partly possible because there's no RTF equivalent of or . But I'm trying to fin... More on autoitscript.com
Videos
10:46
Rich Text Editor in HTML, CSS and JavaScript | Create your own ...
How to convert HTML to RTF using JavaScript - YouTube
49:03
Rich Text Editor With Javascript | Step By Step Javascript Project ...
22:06
Rich Text Editor with HTML, CSS and JS - YouTube
09:03
Rich Text Editor Using HTML, CSS & JavaScript - YouTube
JSFiddle
jsfiddle.net › JamesMGreene › 2b6Lc
Convert HTML to RTF - JSFiddle - Code Playground
Adding External Resources will no longer create a list of resources in the sidebar but will be injected as a LINK or SCRIPT tag inside of the HTML panel.
Storyblok
storyblok.com › tp › converting-html-to-rich-text-json
Converting HTML to Richtext JSON
Similar to overwriting resolves via richtext options, you can customize how specific HTML elements are converted by providing custom resolvers: import { htmlToStoryblokRichtext, HTMLTags } from "@storyblok/richtext/html-parser"; const html = '<h1>Custom Heading\nThis is a paragraph with <a href="https://example.com">a link</a>.</h1>'; const richtextDoc = htmlToStoryblokRichtext(html, { ... The @storyblok/richtext package is framework-agnostic and can be used with any JavaScript-based frontend framework.
npm
npmjs.com › package › convert-rich-text
convert-rich-text - npm
May 2, 2023 - var convert = require('convert-rich-text'); var html = convert(delta, formats, options); // last argument optional
» npm install convert-rich-text
Published May 02, 2023
Version 6.1.0
Author Blake Thomson
GitHub
github.com › antoniolucasnobar › html-to-rtf-browser
GitHub - antoniolucasnobar/html-to-rtf-browser: Convert html to rtf format in the browser · GitHub
const HtmlToRtfBrowser = require('html-to-rtf-browser'); var htmlToRtf = new HtmlToRtfBrowser(); var html = ` <h1>Title <span style="color:rgb(255,0,0);">with</span> tag h1<h1> <p> start of an image (with width and height defined): </p> <img style="width: 5cm; height: 300px" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFQAAABdCAAAAAAeDx7VAAAAAmJLR0QA /4ePzL8AAAAJcEhZcwAACxIAAAsSAdLdfvwAAAGxSURBVFjD7dctdsMwDABgQcPCwMDCwsHAwsLCwcLeoLBHCMwRDAsHB3uMHCFQs52sP4llW7H33oDEmtZf/WxZcgD/IEBQQQUVVFBBOaFrqLvCaK8AQGWgt9oA7/MaH50y0ApcqNkj7flpOjqab/MyH/fen3LQhEf/ATUbXbdmc5quIHqeFvVlq/LR4fNXLYg+2Of+T3+y
Starred by 7 users
Forked by 3 users
Languages JavaScript 83.4% | Rich Text Format 14.6% | HTML 2.0%
GitHub
github.com › iarna › rtf-to-html
GitHub - iarna/rtf-to-html: Convert RTF to HTML in pure JavaScript. · GitHub
Convert RTF to HTML in pure JavaScript. Contribute to iarna/rtf-to-html development by creating an account on GitHub.
Starred by 72 users
Forked by 36 users
Languages JavaScript
AnyConv
anyconv.com › html-to-rtf-converter
Convert HTML to RTF Online - Free & Fast | AnyConv
Combined with CSS and JavaScript, HTML enables dynamic and responsive web design. HTML files are opened directly in web browsers. To view or edit the code, use any text or code editor such as Notepad++, Atom, or Visual Studio Code. You can convert HTML to formats like PDF or EPUB using AnyConv or online conversion tools. ... RTF (Rich ...
npm
npmjs.com › package › html-to-text
html-to-text - npm
Advanced converter that parses HTML and returns beautiful text.
» npm install html-to-text
Published Mar 23, 2023
Version 9.0.5
DHTMLX
dhtmlx.com › docs › products › dhtmlxRichText
JavaScript/HTML Rich Text Editor - 📝DHTMLX RichText
DHTMLX Rich Text Editor is a lightweight and customizable JavaScript WYSIWYG editor for web apps. Input and output content in HTML & Markdown formats · In addition to typing in the text directly, our JavaScript rich text editor allows loading content into the component as well as saving text after editing in the two supported formats: HTML and Markdown.
Convertio
convertio.co › html-rtf
Convert HTML to RTF / URL to RTF (Online & Free) — Convertio
Convertio — Easy tool to convert HTML files to RTF online. For mac & windows. No download required.
Word to HTML
wordtohtml.net
Convert Word and PDF files to clean HTML | Free online HTML editor
Your converted HTML will appear in the HTML Editor. You can also create new content by typing directly into the Visual Editor box. It works just like any text editor. You have full control over fonts, font size, and font colors, as well as the ability to create lists, tables, and insert images. ... Word to HTML supports Word files (.DOCX and .DOC), PDF files, RTF (rich text format), Open Doc files (from Libre or Open Office) and .TXT plain text files.
jsDelivr
jsdelivr.com › package › npm › html-to-rtf
html-to-rtf CDN by jsDelivr - A CDN for npm and GitHub
February 20, 2023 - A free, fast, and reliable CDN for html-to-rtf. Convert html to rtf format in the browser and the server
Published Jul 24, 2017
Repository https://github.com/oziresrds/html-to-rtf