Invalid nested tag div found, expected closing tag input
Hello all,
The solution I came up with is to fix the page source using HTMLAgilityPack which is to explicitly close the tags by assigning HtmlElementFlag.Closed enum to HtmlNode.ElementsFlag["img"] dictionary.
That's it.. :-D
I've been experimenting on how to print html document using the said 3rd party software called iTextSharp. iTextSharp is a popular tool and has several examples on the internet regarding integration to the project and occurring issues. One of the issue I encountered is Invalid nested div tag and is expecting a closing tag input. As I trace back my html source, the tags are well-formed except that they are self closing tags such as <input>, <hr>, <img>, <br> or the like. These tags when passed to an action method as string are not properly closed and thus an issue is thrown by iTextSharp's XMLWorkerHelper's ParseXHtml() method.
<img src="~/Images/success.png" /> <input type="hidden" name="OrderStatusHTML" /> <input type="submit" id="btnSubmit" value="Export to PDF" class="btn btn-success" />
[HttpPost] [ValidateInput(false)] public FileResult ExportToPDF(string OrderStatusHTML) { HtmlNode.ElementsFlags["img"] = HtmlElementFlag.Closed; HtmlNode.ElementsFlags["input"] = HtmlElementFlag.Closed; HtmlDocument doc = new HtmlDocument(); doc.OptionFixNestedTags = true; doc.LoadHtml(OrderStatusHTML); OrderStatusHTML = doc.DocumentNode.OuterHtml; using (MemoryStream stream = new System.IO.MemoryStream()) { Encoding unicode = Encoding.UTF8; StringReader sr = new StringReader(OrderStatusHTML); Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f); PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream); pdfDoc.Open(); XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr); pdfDoc.Close(); return File(stream.ToArray(), "application/pdf", "OrderStatus.pdf"); } }
That's it.. :-D
thanks for the idea
ReplyDeleteYou're welcome.. :-)
DeleteThanks. It worked perfectly.
ReplyDeleteGlad I could help! :)
Deletethanks its work perfectly... saved lots of time of me.. thanks a lot
ReplyDeleteYou're welcome
DeleteNice Article. It is very helpful for my scenario.
ReplyDeleteGlad I could help.. :)
DeleteHtmlNode does not exist. how to fix this
ReplyDeleteHello,
DeleteYou need to add the HTMLAgilityPack package into your project to access HtmlNode and other classes.
Instead of using any library you can use the build in XMLSerializer in javascript to rectify this.
ReplyDeleterefer the below link.
https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer
Thanks for the tip. Though our project before requires the fix to be done via controller action, I appreciate that you provide an alternative solution via front-end. By the way, .NET has a XMLSerializer class which would like work the same as XMLSerializer in JS.
DeleteThank you so much. It worked for me.
ReplyDeleteYou're welcome. Glad I could help!
DeleteCheers!
Thanks a lot ...
ReplyDeleteYou're welcome!
DeleteNot working if html contains textbox.
ReplyDeleteHave you tried closing the textbox element too?
DeleteThanks a lot my friend, It really worked for me as well.
ReplyDeleteYou're welcome!
DeleteMy friend, I wanna ask you one question, this method is creating a pdf file but my table is a little big bigger and it takes the pdf of the the visible page and all the other columns which are not visible are not present in the pdf
ReplyDeleteMaybe you need tweak some of the settings like margin and etc. Perhaps the documentation or forums related to iTextSharp might help. :-)
Delete