Java RuntimeWorkerException:找到无效的嵌套标记头,预期结束标记元

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25491379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:29:53  来源:igfitidea点击:

RuntimeWorkerException: Invalid nested tag head found, expected closing tag meta

javahtmlpdfitexthtml-to-pdf

提问by Drazen Bjelovuk

I'm using iText in order to convert html into a pdf, but I keep getting a RuntimeWorkerException thrown at parseXHtml. Here's my code:

我正在使用 iText 将 html 转换为 pdf,但我一直在parseXHtml. 这是我的代码:

Document tempDoc = new Document();
PdfWriter pdfWriter = PdfWriter.getInstance(tempDoc, out);
tempDoc.open();
XMLWorkerHelper.getInstance().parseXHtml(pdfWriter, tempDoc, new ByteArrayInputStream(html.getBytes()));
tempDoc.close();

I'm not too familiar with the differences between HTML and XHTML, so I'm at a bit of a loss as to how I should handle this. Here's the html source if it helps.

我不太熟悉 HTML 和 XHTML 之间的差异,所以我不知道应该如何处理这个问题。如果有帮助,这里是 html 源代码

采纳答案by Chris Haas

The error message is pretty clear, you have a <meta>tag in the header that isn't closed which is valid in HTMLbut not XHTMLwhich is what you are parsing it as. You need to close those, <meta ... />

错误消息非常清楚,您<meta>在标头中有一个未关闭的标记,该标记有效HTML但不是XHTML您正在解析的标记。你需要关闭那些,<meta ... />

回答by saktiprasad swain

If you are using XMLWorkerHelper make sure you end image, breakpoint tag properly like />.

如果您使用 XMLWorkerHelper,请确保结束图像,断点标记正确,如 />。

回答by Yair Segal

For a similar error message -

对于类似的错误消息 -

invalid nested tag body found, expected closing tag meta

invalid nested tag body found, expected closing tag meta

turned out the XHTML I was parsing had a <script>section at the bottom, that contained JS code, something like:

结果我正在解析的 XHTML<script>在底部有一个部分,其中包含 JS 代码,例如:

<script>
  function my_func(var) {
    ...
  }     
</script>

After removing that code (with simple string manipulations), I was able to get the .parseXHtmlto work without issues.

删除该代码(使用简单的字符串操作)后,我能够.parseXHtml正常工作。

回答by shivraj kumar

you have to close each and every tag. example- in HTML

你必须关闭每一个标签。示例 - 在 HTML 中

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

is valid.

已验证。

But in xhtml you have to use

但是在 xhtml 中你必须使用

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>

So close each and every tag in html (example meta tag, col tag, img tag etc).

所以关闭 html 中的每个标签(例如元标签、col 标签、img 标签等)。

回答by Flavio Troia

Remember to close all meta tags

记得关闭所有元标签

<meta ... />