The conversion works well (thank you) with the following code - except that there's an error message embedded in the output "TO HIDE THESE MESSAGES, TURN OFF debug level logging for org.docx4j.convert.out.common.writer.AbstractMessageWriter":
Maven:
- Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>DocumentConverter</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>15</source>
<target>15</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-JAXB-ReferenceImpl</artifactId>
<version>8.2.9</version>
<!-- <scope>test</scope>-->
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<!-- logging config files; docx4j.properties -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>com.github.mayconmfl</groupId>
<artifactId>log4j-configurator</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
Code:
- Code: Select all
import org.docx4j.Docx4J;
import org.docx4j.Docx4jProperties;
import org.docx4j.convert.out.HTMLSettings;
import org.docx4j.convert.out.html.AbstractHtmlExporter;
import org.docx4j.convert.out.html.HtmlExporterNG2;
import org.docx4j.fonts.PhysicalFonts;
import org.docx4j.model.fields.FieldUpdater;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.org.apache.poi.util.IOUtils;
import java.io.*;
public class DocXTest {
public static void docxToHTML(String filename, String outputname) throws FileNotFoundException, Docx4JException {
String regex=".*(Courier New|Arial|Times New Roman|Comic Sans|Georgia|Impact|Lucida Console|Lucida Sans Unicode|Palatino Linotype|Tahoma|Trebuchet|Verdana|Symbol|Webdings|Wingdings|MS Sans Serif|MS Serif).*";
PhysicalFonts.setRegex(regex);
File file = new File(filename);
FileInputStream inputStream = new FileInputStream(file);
WordprocessingMLPackage pkg = WordprocessingMLPackage.load(inputStream);
String root = System.getProperty("user.dir");
FieldUpdater updater = new FieldUpdater(pkg);
updater.update(true);
AbstractHtmlExporter exporter = new HtmlExporterNG2();
HTMLSettings htmlSettings = Docx4J.createHTMLSettings();
htmlSettings.setWmlPackage(pkg);
Docx4jProperties.setProperty("docx4j.Convert.Out.HTML.OutputMethodXML", true);
OutputStream os = new FileOutputStream(root + "/" + outputname + ".html");
Docx4J.toHTML(htmlSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
IOUtils.closeQuietly(os);
if (pkg.getMainDocumentPart().getFontTablePart() != null) {
pkg.getMainDocumentPart().getFontTablePart()
.deleteEmbeddedFontTempFiles();
}
htmlSettings = null;
pkg = null;
}
public static void main(String[] args) throws Exception {
String parameter;
if (args.length < 1) {
System.out.println("No parameter supplied");
System.exit(1);
}
parameter = args[0];
docxToHTML(parameter, "testa");
}
}
Configuration - which is in /src/main/configuration, and which I ripped from Github (without properly understanding it I'm afraid). I've set the configuration folder to be marked as 'sources' as suggested in a stack overflow answer, because I think that adds it to the class path and makes it available.
- Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="console">
<PatternLayout pattern="%d{dd.MM.yyyy HH:mm:ss} *%-5p* %c{1}: %m (%F, line %L)%n"/>
</Console>
<!-- error.log -->
<File name="OFF" fileName="logs/docx4j.log" >
<PatternLayout pattern="%d{dd.MM.yyyy HH:mm:ss} *%-5p* %c{1}: %m (%F, line %L)%n"/>
</File>
</Appenders>
<Loggers>
<Logger name="org.apache.commons" level="OFF"/>
<Logger name="org.apache.fop" level="OFF"/>
<!-- docx4j loggers -->
<Logger name="org.docx4j" level="OFF"/>
<Logger name="org.docx4j.Docx4J" level="OFF"/>
<Logger name="org.docx4j.TraversalUtil" level="OFF"/>
<Logger name="org.docx4j.XmlUtils" level="OFF"/>
<Logger name="org.docx4j.utils.ResourceUtils" level="OFF"/>
<Logger name="org.docx4j.convert.in" level="OFF"/>
<Logger name="org.docx4j.convert.in.xhtml" level="OFF"/>
<Logger name="org.docx4j.convert.in.xhtml.ListHelper" level="OFF"/>
<Logger name="org.docx4j.convert.out" level="OFF"/>
<Logger name="org.docx4j.convert.out.common.AbstractConversionContext" level="OFF"/>
<Logger name="org.docx4j.convert.out.common.Preprocess" level="OFF"/>
<Logger name="org.docx4j.convert.out.common.preprocess" level="OFF"/>
<Logger name="org.docx4j.convert.out.common.preprocess.FieldsCombiner" level="OFF"/>
<!-- PDF output via XSL FO: change this to warn to hide messages -->
<Logger name="org.docx4j.convert.out.common.writer.AbstractMessageWriter" level="OFF"/>
<Logger name="org.docx4j.convert.out.fo" level="OFF"/>
<Logger name="org.docx4j.convert.out.html.ListsToContentControls" level="OFF"/>
<Logger name="org.docx4j.diff" level="OFF"/>
<Logger name="com.topologi" level="OFF"/>
<Logger name="org.docx4j.fonts" level="OFF"/>
<Logger name="org.docx4j.fonts.PhysicalFonts" level="OFF"/>
<Logger name="org.docx4j.fonts.RunFontSelector" level="OFF"/>
<Logger name="org.docx4j.jaxb" level="OFF"/>
<Logger name="org.docx4j.model" level="OFF"/>
<Logger name="org.docx4j.model.fields" level="OFF"/>
<Logger name="org.docx4j.model.listnumbering" level="OFF"/>
<Logger name="org.docx4j.model.PropertyResolver" level="OFF"/>
<Logger name="org.docx4j.model.structure" level="OFF"/>
<Logger name="org.docx4j.model.styles" level="OFF"/>
<Logger name="org.docx4j.model.table" level="OFF"/>
<Logger name="org.docx4j.openpackaging" level="OFF"/>
<Logger name="org.docx4j.convert.out.pdf.viaXSLFO" level="OFF"/>
<Logger name="org.docx4j.openpackaging.io3" level="OFF"/>
<Logger name="org.docx4j.openpackaging.packages" level="OFF"/>
<Logger name="org.docx4j.openpackaging.parts" level="OFF"/>
<Logger name="org.docx4j.openpackaging.parts.Parts" level="OFF"/>
<Logger name="org.docx4j.openpackaging.parts.ThemePart" level="OFF"/>
<Logger name="org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage" level="OFF"/>
<Logger name="org.docx4j.openpackaging.parts.WordprocessingML.ObfuscatedFontPart" level="OFF"/>
<Logger name="org.docx4j.services.client" level="debug"/>
<Logger name="org.docx4j.toc" level="debug"/>
<Logger name="org.docx4j.wml" level="OFF"/>
<Logger name="org.docx4j.utils.XPathFactoryUtil" level="OFF"/>
<Logger name="org.docx4j.org.xhtmlrenderer" level="OFF"/>
<Logger name="org.pptx4j" level="OFF"/>
<!-- OpenDoPE functionality loggers -->
<Logger name="org.docx4j.model.datastorage" level="OFF"/>
<Logger name="org.docx4j.model.datastorage.OpenDoPEHandlerComponents" level="OFF"/>
<Logger name="org.docx4j.openpackaging.parts.CustomXmlDataStoragePart" level="OFF"/>
<Logger name="org.docx4j.openpackaging.parts.XmlPart" level="OFF"/>
<Logger name="org.opendope.conditions.Xpathref" level="OFF"/>
<Logger name="org.opendope" level="OFF"/>
<!-- Enterprise edition loggers -->
<Logger name="com.plutext.merge" level="OFF"/>
<Logger name="com.plutext.merge.CustomXmlPartHandler" level="OFF"/>
<Logger name="com.plutext.merge.altchunk" level="OFF"/>
<Logger name="com.plutext.merge.pptx" level="OFF"/>
<Logger name="com.plutext.cmis" level="OFF"/>
<Logger name="org.apache.jcp.xml.dsig.internal.dom" level="OFF"/>
<Logger name="com.plutext.crypt.dsig" level="OFF"/>
<Logger name="org.docx4j.openpackaging.parts.digitalsignature.XmlSignaturePart" level="OFF"/>
<Logger name="com.plutext.demo.diagnostics" level="debug"/>
<!-- Root logger -->
<Root level="OFF">
<AppenderRef ref="console"/>
</Root>
</Loggers>
</Configuration>
I've looked at the answers on this site (including their counterparts in places like Stack Overflow), and tried everything - and only succeeded in making a mess. I've tried subclassing log4j, I've tried configuration files - this is the closest that I've found to a solution that might be right - but it still doesn't work.
Ideally, I'd like a solution which allows me to turn on or off the level of logging using a parameter - but, failing that, I'd like to know what configuration I need to turn off logging.
Thank you in advance for any help that you can provide.