iText~html转pdf出现异常:com.itextpdf.tool.xml.exceptions.RuntimeWorkerException: Inva

yeah_aly 2013-07-31 09:28:04
public static void parseHTML2PDFFile(String pdfFile,
String htmlFile) {
try{
File htmlfile = new File(htmlFile);
InputStream htmlFileStream = new FileInputStream(htmlfile);

BaseFont bfCN = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",false);
// 中文字体定义
Font chFont = new Font(bfCN, 12, Font.NORMAL, BaseColor.BLUE);
Font secFont = new Font(bfCN, 12, Font.NORMAL, new BaseColor(0, 204,
255));

Document document = new Document();

PdfWriter pdfwriter = PdfWriter.getInstance(document,
new FileOutputStream(pdfFile));
pdfwriter.setViewerPreferences(PdfWriter.HideToolbar);
document.open();

// html文件
InputStreamReader isr = new InputStreamReader(htmlFileStream, "UTF-8");

//默认参数转换
XMLWorkerHelper.getInstance().parseXHtml(pdfwriter, document, isr);

document.close();
}catch (Exception e) {
e.printStackTrace();
}
}

com.itextpdf.tool.xml.exceptions.RuntimeWorkerException: Invalid nested tag head found, expected closing tag link.
at com.itextpdf.tool.xml.XMLWorker.endElement(XMLWorker.java:134)
at com.itextpdf.tool.xml.parser.XMLParser.endElement(XMLParser.java:393)
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.itextpdf.tool.xml.XMLWorkerHelper.parseXHtml(XMLWorkerHelper.java:168)
at com.hoosen.webquery.action.WordToHtml.parseHTML2PDFFile(WordToHtml.java:128)
at com.hoosen.webquery.action.WordToHtml.main(WordToHtml.java:156)
...全文
5815 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
枫逸轩 2021-07-07
  • 打赏
  • 举报
回复

请问转化的网站不是自己的,能不能从itext工具方面解决强校验html格式问题

qq_24905111 2014-12-30
  • 打赏
  • 举报
回复
加上3楼的代码 咋不管用呢
evoleht 2013-10-11
  • 打赏
  • 举报
回复
引用 4 楼 yeah_aly 的回复:
这个问题解决了,原因是:itext html转pdf对html格式要求比较严格,html页面加上这个就可以了 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <style> body{ font-family:SimSun; font-size:14px; } </style> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"></meta>
这个加在哪里呢?
yeah_aly 2013-08-21
  • 打赏
  • 举报
回复 1
这个问题解决了,原因是:itext html转pdf对html格式要求比较严格,html页面加上这个就可以了 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <style> body{ font-family:SimSun; font-size:14px; } </style> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"></meta>
Fchine 2013-08-01
  • 打赏
  • 举报
回复
同求答案。。。。。。
yeah_aly 2013-07-31
  • 打赏
  • 举报
回复
这是我将word转换成html的方法(参照别人的) public boolean changeFormat (String FileName){ String FileFormat = ""; FileFormat = FileName.substring(FileName.length()-4,FileName.length()); System.out.println(FileFormat); if(FileFormat.equalsIgnoreCase(".doc")){ String DocFile = FileName; System.out.println("word文件路径:"+DocFile); //word文件的完整路径 String HtmlFile = DocFile.substring(0, (DocFile.length() - 4)) + ".htm"; System.out.println("htm文件路径:"+HtmlFile); //html文件的完整路径 ActiveXComponent app = new ActiveXComponent("Word.Application"); //启动word try { app.setProperty("Visible", new Variant(false)); //设置word程序非可视化运行 Dispatch docs = app.getProperty("Documents").toDispatch(); Dispatch doc = Dispatch.invoke(docs,"Open", Dispatch.Method, new Object[]{DocFile,new Variant(false), new Variant(true)}, new int[1]).toDispatch(); //打开word文件 Dispatch.invoke(doc,"SaveAs",Dispatch.Method, new Object[]{HtmlFile,new Variant(8)}, new int[1]); //作为htm格式保存文件 Dispatch.call(doc, "Close",new Variant(false)); //关闭文件 } catch (Exception e){ e.printStackTrace(); } finally { app.invoke("Quit", new Variant[] {}); //退出word程序 } //转化完毕 return true; } return false; }
S117 2013-07-31
  • 打赏
  • 举报
回复
应该是你的html不规范吧,有没有关闭的标签!

50,523

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧