- Code: Select all
import java.io.File;
import java.io.OutputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.docx4j.Docx4J;
import org.docx4j.Docx4jProperties;
import org.docx4j.convert.out.FOSettings;
import org.docx4j.fonts.IdentityPlusMapper;
import org.docx4j.fonts.Mapper;
import org.docx4j.fonts.PhysicalFont;
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.samples.AbstractSample;
import apachefop.SampleDocument;
import org.docx4j.services.client.ConversionException;
public class ApacheFOP extends AbstractSample {
static {
inputfilepath = null;
inputfilepath = "dir\\word.docx";
saveFO = true;
}
static boolean saveFO;
public static void main(String[] args)
throws Exception {
try {
getInputFilePath(args);
} catch (IllegalArgumentException e) {
}
PhysicalFonts.setRegex(regex);
WordprocessingMLPackage wordMLPackage;
if (inputfilepath==null) {
System.out.println("No imput path passed, creating dummy document");
wordMLPackage = WordprocessingMLPackage.createPackage();
SampleDocument.createContent(wordMLPackage.getMainDocumentPart());
}
else {
System.out.println("Loading file from " + inputfilepath);
wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
}
FieldUpdater updater = new FieldUpdater(wordMLPackage);
updater.update(true);
String outputfilepath;
if (inputfilepath==null) {
outputfilepath = System.getProperty("user.dir") + "/OUT_FontContent.pdf";
} else {
outputfilepath = inputfilepath + ".pdf";
}
OutputStream os = new java.io.FileOutputStream(outputfilepath);
if (!Docx4J.pdfViaFO()) {
System.out.println("Using Plutext's PDF Converter; add docx4j-export-fo if you don't want that");
try {
Docx4J.toPDF(wordMLPackage, os);
}
catch (Docx4JException e) {
e.printStackTrace();
IOUtils.closeQuietly(os);
System.out.println(
FileUtils.readFileToString(new File(outputfilepath)));
if (e.getCause()!=null
&& e.getCause() instanceof ConversionException) {
ConversionException ce = (ConversionException)e.getCause();
ce.printStackTrace();
}
}
System.out.println("Saved: " + outputfilepath);
return;
}
System.out.println("Attempting to use XSL FO");
Mapper fontMapper = new IdentityPlusMapper();
wordMLPackage.setFontMapper(fontMapper);
PhysicalFont font
= PhysicalFonts.get("Arial Unicode MS");
FOSettings foSettings = Docx4J.createFOSettings();
if (saveFO)
{
foSettings.setFoDumpFile(new java.io.File(inputfilepath + ".fo"));
}
foSettings.setWmlPackage(wordMLPackage);
System.out.println("Saved: " + outputfilepath);
if (wordMLPackage.getMainDocumentPart().getFontTablePart()!=null) {
wordMLPackage.getMainDocumentPart().getFontTablePart().deleteEmbeddedFontTempFiles();
}
updater = null;
foSettings = null;
wordMLPackage = null;
}
}
(Note:As I couldnt find jar that holds SampleDocument I have explicitly added the code of that class in another class and imported it).
Exceptions:
java.lang.reflect.InvocationTargetException: null
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.createFopFactory(FORendererApacheFOP.java:308)
at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.getFopFactory(FORendererApacheFOP.java:253)
at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.render(FORendererApacheFOP.java:119)
at org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:168)
at org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:47)
at org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:82)
at org.docx4j.Docx4J.toFO(Docx4J.java:597)
at apachefop.ApacheFOP.main(ApacheFOP.java:216)
Caused by: java.lang.NoSuchMethodError: org.slf4j.spi.LocationAwareLogger.log(Lorg/slf4j/Marker;Ljava/lang/String;ILjava/lang/String;[Ljava/lang/Object;Ljava/lang/Throwable;)V
at org.apache.commons.logging.impl.SLF4JLocationAwareLog.debug(SLF4JLocationAwareLog.java:131)
at org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry.registerPreloader(ImageImplRegistry.java:113)
at org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry.discoverClasspathImplementations(ImageImplRegistry.java:91)
at org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry.<init>(ImageImplRegistry.java:70)
at org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry.getDefaultInstance(ImageImplRegistry.java:79)
at org.apache.xmlgraphics.image.loader.ImageManager.<init>(ImageManager.java:48)
java.lang.NoSuchMethodException: org.apache.fop.apps.FopFactory.newInstance()
at java.lang.Class.getDeclaredMethod(Class.java:2130)
at org.apache.fop.apps.FopFactoryBuilder$FopFactoryConfigImpl.<init>(FopFactoryBuilder.java:373)
at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.createFopFactory(FORendererApacheFOP.java:329)
at org.apache.fop.apps.FopFactoryBuilder.<init>(FopFactoryBuilder.java:89)
at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.getFopFactory(FORendererApacheFOP.java:253)
at org.apache.fop.apps.FopConfParser.<init>(FopConfParser.java:154)
at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.render(FORendererApacheFOP.java:119)
at org.apache.fop.apps.FopConfParser.<init>(FopConfParser.java:88)
at org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:168)
at org.apache.fop.apps.FopConfParser.<init>(FopConfParser.java:103)
... 12 common frames omitted
12:10:39.803 [main] ERROR org.docx4j.convert.out.common.AbstractExporter - Exception exporting package
java.lang.NullPointerException: null
at org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:47)
at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.render(FORendererApacheFOP.java:199)
at org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:82)
at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.render(FORendererApacheFOP.java:159)
at org.docx4j.Docx4J.toFO(Docx4J.java:597)
at apachefop.ApacheFOP.main(ApacheFOP.java:216)
Exception in thread "main" org.docx4j.openpackaging.exceptions.Docx4JException: Exception exporting package
at org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:168)
at org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:47)
at org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:109)
at org.docx4j.Docx4J.toFO(Docx4J.java:597)
at org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:82)
at apachefop.ApacheFOP.main(ApacheFOP.java:216)
Caused by: java.lang.NullPointerException
at org.docx4j.Docx4J.toFO(Docx4J.java:597)
at apachefop.ApacheFOP.main(ApacheFOP.java:216)
at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.render(FORendererApacheFOP.java:199)
at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.render(FORendererApacheFOP.java:159)
at org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:168)
at org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:47)
at org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:82)
... 2 more
I have included all the necessary jars.Am I lacking some jars?Or what may be the reason for these exceptions?
Can anyone help me out?