2

I have a requirement to display rtf file in my HTML. I am using object tag to embed my file into html.

When I use below code showing the plugin is not supported. Is there any way I can display rtf type document in my HTML

<object data="assets/display.rtf" type="application/rtf"></object>

Note: I am able to display PDF file with the same approach

Gourav
  • 2,746
  • 5
  • 28
  • 45
Oxygen
  • 65
  • 7
  • Possible duplicate of [Is it possible to display an RTF file inside a web page using PHP?](https://stackoverflow.com/questions/678192/is-it-possible-to-display-an-rtf-file-inside-a-web-page-using-php) – Martin Zeitler Feb 03 '19 at 06:46

1 Answers1

0

After trying a lot found something.

First i downloaded the rtf.js from this link. rtf.js . Then went through this link. getting started with rtf.js. (download entire dist folder and place it into your html folder. dist folder doesn't contain jquery min js plz download it. and include all the script files in your html). Below is the code i modified. (Entire rtf will be converted into objects). Along with the following code please include the default code given by rtf.js.(Please make sure your rtf file is in proper way.)

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

{\\colortbl;\\red0\\green0\\blue0;\\red255\\green0\\blue0;}
This line is the default color\\line
\\cf2
This line is red\\line
\\cf1
This line is the default color
}
}`;




<div id="display">

</div>

<script
          src="https://code.jquery.com/jquery-3.3.1.min.js"
          integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
          crossorigin="anonymous"></script>
<script>
  doc.render().then(function(htmlElements) {
     //console.log(meta);
     //console.log(htmlElements);
     console.log(htmlElements);
     var display = document.getElementById("display");
     for(var i=0;i<htmlElements.length;i++){
        //console.log(htmlElements[i][0].innerHTML);
        display.innerHTML+=htmlElements[i][0].innerHTML; //RENDERING ALL DIVS INTO display DIV
     }
     }).catch(error => console.error(error))
</script>
Pallamolla Sai
  • 2,337
  • 1
  • 13
  • 14
  • Thanks for your answer. display.innerHTML=htmlElements[0][0].innerHTML giving me only one div. How can I assign all the htmlElements to display. – Oxygen Feb 03 '19 at 13:31
  • Showing [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object] if I use below code. let display = document.getElementById('display'); display.innerHTML = htmlElements; – Oxygen Feb 03 '19 at 14:17
  • I have edited the code through which all divs inside htmlElements will be rendered – Pallamolla Sai Feb 04 '19 at 08:33
  • after adding this display.appendChild(htmlElements[i][0]); giving me the same display as file . Thanks fo your help – Oxygen Feb 04 '19 at 13:51
  • This library is working fine for rtf file.. but why chrome is not supporting application/rtf type ? The same content working with pdf just adding application/pdf to object tag. any idea ? – Oxygen Feb 04 '19 at 13:53