We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Html generated by the Rich Text Box is invalid.

The Text written into the Rich Text Box is stored in html, rft as well as plain text in our db.

Since the Text Box does not offer the option to get the rtf string directly i am converting the html value i get from the Text Box using DocIO.

So far so good, in most test cases the html gets converted to rtf without issue. If shitf + enter is used for a line break tough the html is considered invalid by DocIO.


 

 The report.DescritionHtml property is directly bound with @bind-Value so there is no changes made to it that could invalide the structure.

In my test case the html resulting from the content in the Text Box was the following:

<p>Test Content before shift enter.<br>Test Content after shift enter.</p>



Trying to convert this html value resulted in a error in this line:
document = new WordDocument(stream, FormatType.Html, XHTMLValidationType.None);

The error was the following:

System.NotSupportetException | DocIO support only welformatted xhtml Details:The 'br' start tag on line 1 position 77 does not match the end tag of 'p'. Line 1, position 113.

Is this a mistake on my side? Do i need to somehow support badly structured html? Is my conversion methode badly implemented?


7 Replies

VY Vinothkumar Yuvaraj Syncfusion Team April 21, 2023 10:26 AM UTC

Hi Richard,


To solve your problem, we suggest using the XmlConversion and XhtmlConversion classes to convert the HTML to well-formed XHTML before passing it to the DocIO library.


public WordDocument GetDocument(string htmlText)

 {

   WordDocument document = null;

   MemoryStream stream = new MemoryStream();

   StreamWriter writer = new StreamWriter(stream, System.Text.Encoding.Default);

   htmlText = htmlText.Replace("\"", "'");

   XmlConversion XmlText = new XmlConversion(htmlText);

   XhtmlConversion XhtmlText = new XhtmlConversion(XmlText);

   writer.Write(XhtmlText.ToString());

   writer.Flush();

   stream.Position = 0;

   document = new WordDocument(stream, FormatType.Html, XHTMLValidationType.None);

   return document;

}


We hope this fix will resolve your issue. If the issue still persists, kindly revert back to us.


Regards,

Vinothkumar



JB Joachim Bergholz May 24, 2023 07:50 AM UTC

Hi Vinothkumar,


where can i find the

XmlConversion and XhtmlConversion classes ?

Regards
Joachim



VY Vinothkumar Yuvaraj Syncfusion Team May 26, 2023 07:09 AM UTC

Hi Joachim,


You can use Syncfusion.EJ.Export the namespace in your application to use the XmlConversion classes. I have attached the Nuget package screenshot for your reference.



Regards,

Vinothkumar




JB Joachim Bergholz replied to Vinothkumar Yuvaraj June 5, 2023 08:12 AM UTC

Hi Vinothkumar,

thank you for the information.

But now the following line

XhtmlConversion XhtmlText = new XhtmlConversion(XmlText);

leads to the error message:

Unable to cast object of type 'System.Xml.XmlText' to type 'System.Xml.XmlElement'.

I'm using:

"Syncfusion.EJ.Export" Version="20.4.0.44"

"Syncfusion.Blazor" Version="21.1.35"


Thanks in advance !

Regards
Joachim




VY Vinothkumar Yuvaraj Syncfusion Team June 8, 2023 05:13 AM UTC

Hi Joachim,


We suspect that the reported issue of "Html generated by the Rich Text Box is invalid" is occurred due to unformatted HTML while converted to rtf using DocIO. That means html tags must open and close properly, so here we have replaced the <br> with <br/> from the retrieved HTML stringsYou can convert them by using the following code:


public WordDocument GetDocument(string htmlText)

  {

     htmlText = htmlText.Replace("\"", "'").Replace("<br>","<br/>");

  }


If you are still facing an error, could you please share a runnable sample so that we can replicate your problem at our end?



FG Frederik Gysel July 28, 2023 07:59 AM UTC

for what's it's worth: i had the same issue and it was because I has a space between br and / in the code.


NOT OK: <br />

OK: <br/>


Rgds

Frederik



VY Vinothkumar Yuvaraj Syncfusion Team July 31, 2023 12:44 PM UTC

Hi Frederik,


Thanks for an update,

 

From your query, we suspect that your issue has been resolved on you own.

 

If you have any further queries, please get back to us.



Loader.
Live Chat Icon For mobile
Up arrow icon