Donate

Invalid nested tag div found, expected closing tag input

Hello all,

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" />
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.
[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

Comments

  1. thanks its work perfectly... saved lots of time of me.. thanks a lot

    ReplyDelete
  2. Nice Article. It is very helpful for my scenario.

    ReplyDelete
  3. HtmlNode does not exist. how to fix this

    ReplyDelete
    Replies
    1. Hello,
      You need to add the HTMLAgilityPack package into your project to access HtmlNode and other classes.

      Delete
  4. Instead of using any library you can use the build in XMLSerializer in javascript to rectify this.
    refer the below link.
    https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer

    ReplyDelete
    Replies
    1. 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.

      Delete
  5. Thank you so much. It worked for me.

    ReplyDelete
  6. Not working if html contains textbox.

    ReplyDelete
    Replies
    1. Have you tried closing the textbox element too?

      Delete
  7. Thanks a lot my friend, It really worked for me as well.

    ReplyDelete
  8. My 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

    ReplyDelete
    Replies
    1. Maybe you need tweak some of the settings like margin and etc. Perhaps the documentation or forums related to iTextSharp might help. :-)

      Delete

Post a Comment

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

How To Insert Or Add Emojis In Microsoft Teams Status Message

TypeScript Error Or Bug: The term 'tsc' is not recognized as the name of a cmdlet, function, script file, or operable program.