我目前正在尝试使用docx4j库将文件从html转换为docx。
我已经使用itext5设法将html转换为pdf,但是由于jaxb,我现在正面临尝试转换为docx的异常。
我的项目使用了maven,所以我试图导入很多库...都是徒劳的...
public static void htmlToDocx(String html_content, String html_filename, String docx_filename)
{
try {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
NumberingDefinitionsPart ndp = new NumberingDefinitionsPart();
wordMLPackage.getMainDocumentPart().addTargetPart(ndp);
ndp.unmarshalDefaultNumbering();
XHTMLImporterImpl xHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
xHTMLImporter.setHyperlinkStyle("Hyperlink");
wordMLPackage.getMainDocumentPart().getContent().addAll(xHTMLImporter.convert(new File(html_filename), null));
File output = new File(docx_filename);
wordMLPackage.save(output);
System.out.println("done");
System.out.println("file path where it is stored is" + " "+ output.getAbsolutePath());
}
catch (Exception e) {
e.printStackTrace();
}
}
这是我应该将html转换为docx的函数:
2019-06-13 16:16:04.092 WARN 2988 --- [pool-1-thread-1] org.docx4j.utils.ResourceUtils : Couldn't get resource: docx4j.properties
2019-06-13 16:16:04.092 WARN 2988 --- [pool-1-thread-1] org.docx4j.Docx4jProperties : Couldn't find/read docx4j.properties; docx4j.properties not found via classloader.
2019-06-13 16:16:04.092 INFO 2988 --- [pool-1-thread-1] o.d.o.packages.WordprocessingMLPackage : Using paper size: A4
2019-06-13 16:16:04.092 INFO 2988 --- [pool-1-thread-1] o.d.o.packages.WordprocessingMLPackage : Landscape orientation: false
2019-06-13 16:16:04.118 INFO 2988 --- [pool-1-thread-1] org.docx4j.jaxb.Context : java.vendor=Oracle Corporation
2019-06-13 16:16:04.119 INFO 2988 --- [pool-1-thread-1] org.docx4j.jaxb.Context : java.version=1.8.0_212
2019-06-13 16:16:04.119 INFO 2988 --- [pool-1-thread-1] org.docx4j.jaxb.Context : java.vm.name=OpenJDK 64-Bit Server VM
2019-06-13 16:16:05.222 INFO 2988 --- [pool-1-thread-1] org.docx4j.jaxb.Context : JAXB Reference Implementation is in use.
2019-06-13 16:16:05.283 INFO 2988 --- [pool-1-thread-1] o.d.o.p.relationships.RelationshipsPart : adding part with proposed name: /word/document.xml
2019-06-13 16:16:05.388 INFO 2988 --- [pool-1-thread-1] org.docx4j.XmlUtils : setProperty com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl
2019-06-13 16:16:05.388 INFO 2988 --- [pool-1-thread-1] org.docx4j.XmlUtils : actual: com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl
2019-06-13 16:16:05.388 INFO 2988 --- [pool-1-thread-1] org.docx4j.XmlUtils : setProperty com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
2019-06-13 16:16:05.388 INFO 2988 --- [pool-1-thread-1] org.docx4j.XmlUtils : actual: com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
2019-06-13 16:16:05.398 INFO 2988 --- [pool-1-thread-1] o.d.o.p.relationships.RelationshipsPart : adding part with proposed name: /word/styles.xml
2019-06-13 16:16:05.403 INFO 2988 --- [pool-1-thread-1] org.docx4j.utils.XPathFactoryUtil : xpath implementation: org.docx4j.org.apache.xpath.jaxp.XPathFactoryImpl
2019-06-13 16:16:05.407 INFO 2988 --- [pool-1-thread-1] o.d.o.p.relationships.RelationshipsPart : adding part with proposed name: /docProps/core.xml
2019-06-13 16:16:05.407 INFO 2988 --- [pool-1-thread-1] o.d.o.p.relationships.RelationshipsPart : adding part with proposed name: /docProps/app.xml
2019-06-13 16:16:05.408 INFO 2988 --- [pool-1-thread-1] o.d.o.p.relationships.RelationshipsPart : adding part with proposed name: /word/settings.xml
2019-06-13 16:16:05.409 INFO 2988 --- [pool-1-thread-1] o.d.o.p.relationships.RelationshipsPart : adding part with proposed name: /word/numbering.xml
2019-06-13 16:16:05.440 INFO 2988 --- [pool-1-thread-1] o.d.convert.in.xhtml.XHTMLImporterImpl : tableFormatting: CLASS_PLUS_OTHER
2019-06-13 16:16:05.440 INFO 2988 --- [pool-1-thread-1] o.d.convert.in.xhtml.XHTMLImporterImpl : paragraphFormatting: CLASS_PLUS_OTHER
2019-06-13 16:16:05.440 INFO 2988 --- [pool-1-thread-1] o.d.convert.in.xhtml.XHTMLImporterImpl : runFormatting: CLASS_PLUS_OTHER
2019-06-13 16:16:05.444 WARN 2988 --- [pool-1-thread-1] org.docx4j.utils.ResourceUtils : Couldn't get resource: docx4j-ImportXHTML.properties
2019-06-13 16:16:05.444 WARN 2988 --- [pool-1-thread-1] o.d.c.in.xhtml.ImportXHTMLProperties : Couldn't find/read docx4j-ImportXHTML.properties; docx4j-ImportXHTML.properties not found via classloader.
2019-06-13 16:16:05.465 INFO 2988 --- [pool-1-thread-1] o.d.convert.in.xhtml.XHTMLImporterImpl :
/* TABLE STYLES */
/* PARAGRAPH STYLES */
.DocDefaults {display:block;margin-bottom: 4mm;line-height: 115%;font-size: 11.0pt;}
/* CHARACTER STYLES */
org.docx4j.org.xhtmlrenderer.load INFO:: SAX XMLReader in use (parser): org.apache.xerces.parsers.SAXParser
org.docx4j.org.xhtmlrenderer.load INFO:: SAX XMLReader in use (parser): org.apache.xerces.parsers.SAXParser
org.docx4j.org.xhtmlrenderer.load INFO:: SAX XMLReader in use (parser): org.apache.xerces.parsers.SAXParser
org.docx4j.org.xhtmlrenderer.load INFO:: SAX XMLReader in use (parser): org.apache.xerces.parsers.SAXParser
org.docx4j.org.xhtmlrenderer.load INFO:: SAX XMLReader in use (parser): org.apache.xerces.parsers.SAXParser
org.docx4j.org.xhtmlrenderer.load INFO:: SAX XMLReader in use (parser): org.apache.xerces.parsers.SAXParser
org.docx4j.org.xhtmlrenderer.load INFO:: Loaded document in ~43ms
org.docx4j.org.xhtmlrenderer.load INFO:: TIME: parse stylesheets 60ms
org.docx4j.org.xhtmlrenderer.match INFO:: media = print
org.docx4j.org.xhtmlrenderer.match INFO:: Matcher created with 134 selectors
java.lang.RuntimeException: javax.xml.bind.JAXBException: JAXB: Can't instantiate JAXB Reference Implementation
- with linked exception:
[java.lang.ClassNotFoundException: org.docx4j.jaxb.ri.NamespacePrefixMapper]
at org.docx4j.XmlUtils.marshaltoString(XmlUtils.java:840)
at org.docx4j.XmlUtils.marshaltoString(XmlUtils.java:716)
at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.addParagraphProperties(XHTMLImporterImpl.java:2315)
at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.populatePPr(XHTMLImporterImpl.java:1575)
at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.getPPr(XHTMLImporterImpl.java:1486)
at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.traverse(XHTMLImporterImpl.java:1261)
at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.traverse(XHTMLImporterImpl.java:825)
at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.convert(XHTMLImporterImpl.java:501)
at jasper_listener.ReportGenerator.htmlToDocx(ReportGenerator.java:157)
at jasper_listener.ReportGenerator.generate(ReportGenerator.java:142)
at jasper_listener.CustomMessageListener$1.run(CustomMessageListener.java:45)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: javax.xml.bind.JAXBException: JAXB: Can't instantiate JAXB Reference Implementation
- with linked exception:
[java.lang.ClassNotFoundException: org.docx4j.jaxb.ri.NamespacePrefixMapper]
at org.docx4j.jaxb.NamespacePrefixMapperUtils.tryUsingRI(NamespacePrefixMapperUtils.java:89)
at org.docx4j.jaxb.NamespacePrefixMapperUtils.getPrefixMapper(NamespacePrefixMapperUtils.java:65)
at org.docx4j.XmlUtils.marshaltoString(XmlUtils.java:789)
... 13 more
Caused by: java.lang.ClassNotFoundException: org.docx4j.jaxb.ri.NamespacePrefixMapper
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at org.docx4j.jaxb.NamespacePrefixMapperUtils.tryUsingRI(NamespacePrefixMapperUtils.java:73)
... 15 more
最后,这是我的例外情况:
{{1}}
也许有人可以帮助我解决此异常...
谢谢, 纪尧姆。
答案 0 :(得分:0)
对于docx4j,您可以选择要使用的JAXB。如果要使用参考实现,请添加:
b [1, 5, 3]
a [1, 2, 3]
进一步查看https://search.maven.org/artifact/org.docx4j/docx4j-JAXB-ReferenceImpl/8.1.0/jar
删除所有您手动添加的部门,然后将其留给Maven进行。
注意:要在Java 8上使用参考实现,您可能需要注意背书的dir机制。使用Java 8,仅使用https://search.maven.org/artifact/org.docx4j/docx4j-JAXB-Internal/8.1.0/jar
可能会更容易注意2:MVN依赖关系:树是你的朋友