使用 iTextSharp 将 HTML 转换为 Pdf - 发现无效的嵌套 p 标签

Converting HTML to Pdf with iTextSharp - invalid nested p tag found

这是我的代码:

Dim sr As StreamReader = New StreamReader(args(0))
Dim htmlStr As String = sr.ReadToEnd
sr.Close()

Using document As Document = New Document()
   Using writer As PdfWriter = PdfWriter.GetInstance(document, New FileStream("C:\Test\myfile.pdf", FileMode.Create))
      document.Open()
      XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, New StringReader(htmlStr)) <--Error here
   End Using
   document.Close()
End Using

谁能帮我弄清楚我该怎么做才能解决这个问题?我正在阅读的 htm 文件看起来不错,而且我也无法更改文件本身。

错误消息解释了问题所在:错误在您的 HTML.

你在某个地方有一个 <p> 标签,它后面跟着另一个不是 </p>.

的结束标签(只有你可以告诉我们是哪个标签)

例如:

这是正确的 [1]:

<p>This is a paragraph<br />with a new line</p>

但是,这是不正确的 [2]:

<p>This is a paragraph</br>with an incorrect new line</p>

这也是无效的[3]:

<b>Some bold text <p>inside a paragraph</b> that is not correctly nested.</p>

解析器将理解 [1],但抛出当您理解 [2] 或 [3] 时遇到的错误。