com.itextpdf.text.html.simpleparser.CellWrapper cannot be cast to com.itextpdf.text.Paragraph
2015-01-14 17:01
2461 查看
用itext把html生成pdf的时候,如果html是这样的话:
重现的测试方法:
解决办法:
因为生成的pdf不要求点击里面的链接,所以就简单的把所有的a标签全都去掉了。
来自Oracle的回答:
1. You're using HTMLWorker. We're abandoning HTMLWorker in favor of XML Worker.
2. Your HTML is wrong.
You have:
<table><tr><p>A p-tag where you would expect a td-tag</p></tr></table>
or
<table><tr><div>A div-tag where you would expect a td-tag</div></tr></table>
or something like that; you get the idea ;-)
String content = "<table><tr><td><a>dayna</a></td></tr></table>";就会报错:
Exception in thread "main" java.lang.ClassCastException: com.itextpdf.text.html.simpleparser.CellWrapper cannot be cast to com.itextpdf.text.Paragraph
重现的测试方法:
private static void testATag() throws Exception { Document document = new Document(); PdfWriter.getInstance(document,new FileOutputStream("D:\\dayna.pdf")); document.open(); String content = "<table><tr><td><a>dayna</a></td></tr></table>"; Paragraph p = new Paragraph(); HashMap<String,Object> map = new HashMap<String,Object>(); map.put(HTMLWorker.IMG_PROVIDER, new ImgProvider()); List<Element> list = HTMLWorker.parseToList(new StringReader(content),null,map); for(Element e : list) { p.add(e); } document.add(p); document.close(); }
解决办法:
因为生成的pdf不要求点击里面的链接,所以就简单的把所有的a标签全都去掉了。
private static String handleATag(String content) { content = content.replaceAll("<a.*?>", "").replaceAll("<A.*?>", ""); content = content.replaceAll("</a>", "").replaceAll("</A>", ""); return content; }
来自Oracle的回答:
1. You're using HTMLWorker. We're abandoning HTMLWorker in favor of XML Worker.
2. Your HTML is wrong.
You have:
<table><tr><p>A p-tag where you would expect a td-tag</p></tr></table>
or
<table><tr><div>A div-tag where you would expect a td-tag</div></tr></table>
or something like that; you get the idea ;-)
相关文章推荐
- com.ibm.jms.JMSBytesMessage cannot be cast to javax.jms.TextMessage
- android.os.BinderProxy cannot be cast to com.android.server.am.Activit 解决办法
- XML11Configuration cannot be cast to org.apache.xerces.xni.parser.XMLParserConfiguration
- Caused by: java.lang.ClassCastException: com.tencent.mm.sdk.modelmsg.SendMessageToWX$Resp cannot be
- Ljava.lang.Object; cannot be cast to com.bdqn.hibernate.entity.*
- java.lang.ClassCastException: android.widget.HeaderViewListAdapter cannot be cast to com.android.mms
- java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.xdcjc.model.D_Users
- org.apache.struts.taglib.html.MessagesTei cannot be cast to javax.servlet.js
- Jackson转换泛型List出现错误java.util.LinkedHashMap cannot be cast to com.xxx
- java.lang.ClassCastException: com.sun.proxy.$Proxy* cannot be cast to***问题解决方案
- Weblogic10g部署web项目报错:com.ctc.wstx.stax.WstxInputFactory cannot be cast to javax.xml.stream.XMLInputF
- weblogic.xml.jaxp.RegistrySAXParserFactory cannot be cast to javax.xml.parsers.SAXParserFactory
- 【Android】程序崩溃错误为Android.widget,textView cannot be cast to android.widget.button
- java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
- spring jpa 测试时报错:java.lang.ClassCastException: com.sun.proxy.$Proxy20 cannot be cast to com.pp.dao.
- jboss发布com.sun.faces.config.WebConfiguration cannot be cast to com.sun.faces.config.WebConfiguration
- ClassCastException: $Proxy0 cannot be cast to (com......) 的解决办法
- android.app.Application cannot be cast to com.mr.realcus.GlobalApplication
- java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.TextView解决办法
- java.lang.ClassCastException: android.content.res.XmlBlock$Parser cannot be cast to android.view.ani