我遇到一个问题,我在程序中使用以下库将Html转换为pdf。
itextpdf.jar - 5.4.1版本
xmlworker - 5.4.1版本
我为很多HTML测试了我的代码并且工作正常,但其中一个HTML无效并抛出以下异常..请仔细研究一下,让我们知道你们是否知道任何解决方案。
java.lang.IllegalArgumentException: Element not allowed. at com.itextpdf.text.pdf.ColumnText.addElement(ColumnText.java:471)
at com.itextpdf.text.pdf.PdfPCell.addElement(PdfPCell.java:283)
at com.itextpdf.tool.xml.html.table.TableData.end(TableData.java:151)
at com.itextpdf.tool.xml.html.AbstractTagProcessor.endElement(AbstractTagProcessor.java:189)
at com.itextpdf.tool.xml.pipeline.html.HtmlPipeline.close(HtmlPipeline.java:206)
at com.itextpdf.tool.xml.XMLWorker.endElement(XMLWorker.java:141)
at com.itextpdf.tool.xml.parser.XMLParser.endElement(XMLParser.java:395)
at com.itextpdf.tool.xml.parser.state.ClosingTagState.process(ClosingTagState.java:70)
at com.itextpdf.tool.xml.parser.XMLParser.parseWithReader(XMLParser.java:235)
at com.itextpdf.tool.xml.parser.XMLParser.parse(XMLParser.java:213)
at com.cisco.wem.transformation.HtmlProcessor.generatePDF(HtmlProcessor.java:1728)
at com.cisco.wem.transformation.HtmlProcessor.processHtml(HtmlProcessor.java:292)
at com.cisco.wem.transformation.HtmlProcessor.execute(HtmlProcessor.java:207)
at com.cisco.wem.transformation.HtmlProcessor.run(HtmlProcessor.java:1451)
at java.lang.Thread.run(Thread.java:662)
我看到其中一个博客,他们说要使用以下代码段,但我不知道在我的代码中将其用于何处
for (Element e : elements) {
if (!ColumnText.isAllowedElement(e))
continue;
// do stuff
}
这是我的代码:
public String generatePDF(String tempFolder,String completeHtmlFilePath,NodeBO renditionRequestBO)throws FileNotFoundException, IOException, DocumentException,
CssResolverException {
<< some code stuff here>>
com.itextpdf.text.Document document = new com.itextpdf.text.Document();
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream(pdfFileNameWithPath));
document.open();
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
//htmlContext.setLastMarginBottom(100.0f);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
CSSResolver cssResolver = XMLWorkerHelper.getInstance()
.getDefaultCssResolver(true);
Pipeline<?> pipeline = new CssResolverPipeline(cssResolver,
new HtmlPipeline(htmlContext, new PdfWriterPipeline(document,
writer)));
XMLWorker worker = new XMLWorker(pipeline, true);
XMLParser p = new XMLParser(worker);
File input = new File(completeHtmlFilePath);
p.parse(new InputStreamReader(new FileInputStream(input), "UTF-8"));
document.close();
return pdfFileNameWithPath;
}
}
由于 斯
这是我获得此异常的HTML代码段,您只能使用下面的表格元素。
<pre><code><table class="stepTable" border="0" cellpadding="3"
cellspacing="0"
width="90%"><ol><tr><td align="left" valign="top"><b>Step 1</b>
</td><td valign="top" class="stepexpand">
<a name="F3BD336EF5F6499EAB5C6A39AA81852F"><!-- --></a><span>Log in to Cisco
CCO
<a href="http://www.cisco.com/cgi-bin/tablebuild.pl/sccm"
target="_blank">http://www.cisco.com/cgi-bin/tablebuild.pl/sccm</a>.
</span></td></tr>
<tr><td align="left" valign="top"><b>Step 2</b>
</td><td valign="top" class="stepexpand"><a name="24BB6E38029E4A7493CE85443C212D09"><!-- --></a><span>Enter your Cisco
CCO password when prompted.
</span></td></tr>
<tr><td align="left" valign="top"><b>Step 3</b>
</td><td valign="top" class="stepexpand"><a name="step_C3603915B0C2485684A1AD73989307D9">
<!-- -- ></a><span><span>Download the SCMS-CM
Installation software package (scms-cm-v500-b100-linux-64.tar).</span>
</span><br/></td></tr>
<tr><td align="left" valign="top"><b>Step 4</b>
</td><td valign="top" class="stepexpand"><a
name="4818AB7377474E80824041ED0C3F240B"><!-- --></a><span>Extract the
package into a temporary directory.
</span>
<br/><br/><b>Example:</b>
<pre class="codeblock">#
<kbd class="userinput"><b>mkdir /usr/tmp/cm_install_temp</b></kbd></pre>
<br/><br/><b>Example:</b>
<pre class="codeblock">#
<kbd class="userinput"><b>cd /usr/tmp/cm_install_temp</b></kbd></pre>
<br/><br/><b>Example:</b>
<pre class="codeblock"># <kbd class="userinput"><b>tar
xvf ../cm_full_package.tar</b></kbd></pre>
</td></tr> test
</ol></table></code></pre>
答案 0 :(得分:2)
它可能是HTML vs XHTML问题。 iText需要输入XHTML,它比HTML更严格。例如,不允许使用br元素。您还需要正确关闭元素。
您可以在使用开源项目“Tidy”http://tidy.sourceforge.net/
将HTML发送到iText之前将其转换为XHTMLpublic static String convertHtmlToXhtml(String html) {
Tidy tidy = new Tidy();
tidy.setXHTML(true);
tidy.setDocType("omit");
InputStream inputStream = new StringBufferInputStream(html);
OutputStream outputStream = new ByteArrayOutputStream();
tidy.parse(inputStream, outputStream);
return outputStream.toString();
}