Page 1 of 1

java word to pdf converter

PostPosted: Fri Feb 02, 2018 5:49 pm
by Asttle
I am using docx4j for word to pdf conversion.My word document contains arabic and english characters.Will docx4j be able to reproduce the same arabic characters in pdf as in word document?I tried using the following code it shows exceptions

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?

Re: java word to pdf converter

PostPosted: Sat Feb 03, 2018 12:13 pm
by jason
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)


"apachefop.ApacheFOP.main" looks strange. What jars do you have and where did you get the FOP related ones?

Re: java word to pdf converter

PostPosted: Mon Feb 05, 2018 3:38 pm
by Asttle
ApacheFOP is my class name Did I want to change that?

Jars Included:
avalon-framework-4.1.5
commons-io-2.4
log4j-1.2.15
serializer-2.7.1
batik-util 1.6.1
commons-logging-1.1.3
docx4j-3.3.6
slf4j
logback-classic 1.2.3
logback-core-1.2.3
docx4j-export-fo-3.3.0
antlr-2.7.7
antlr-runtime-3.5.2
avalon-framework-api-4.3.1
avalon-framework-impl-4.3.1
commons-io-2.4
commons-lang-3.4
guava-19.0
httpclient-4.5.2
httpcore-4.4.4
jackson-annotations-2.7.0
jackson-core-2.7.3
jackson-databind-2.7.3
jaxb-svg11-1.0.2
jcl-over-slf4j 1.7.21
mbassador 1.2.4.2
slf4j-log4j12-1.7.21
stringtemplate-3.2.1
wmf2svg-0.9.8
xalan-2.7.2
xmlgraphics-commons-2.1
fop-2.1
slf4j-api-1.7.25
commons-codec-1.3

Did I want to add any additional jars or remove any existing jars?

Re: java word to pdf converter

PostPosted: Tue Feb 06, 2018 8:33 pm
by jason
Here are the dependencies I have:

Code: Select all
INFO] +- org.docx4j:docx4j:jar:3.3.6:compile
[INFO] |  +- org.plutext:jaxb-svg11:jar:1.0.2:compile
[INFO] |  +- net.engio:mbassador:jar:1.2.4.2:compile
[INFO] |  +- org.slf4j:slf4j-api:jar:1.7.21:compile
[INFO] |  +- org.slf4j:jcl-over-slf4j:jar:1.7.21:compile
[INFO] |  +- org.slf4j:slf4j-log4j12:jar:1.7.21:compile
[INFO] |  +- log4j:log4j:jar:1.2.17:compile
[INFO] |  +- org.apache.commons:commons-lang3:jar:3.4:compile
[INFO] |  +- commons-codec:commons-codec:jar:1.10:compile
[INFO] |  +- commons-io:commons-io:jar:2.4:compile
[INFO] |  +- org.apache.httpcomponents:httpclient:jar:4.5.2:compile
[INFO] |  |  \- org.apache.httpcomponents:httpcore:jar:4.4.4:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-core:jar:2.7.3:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.7.3:compile
[INFO] |  |  \- com.fasterxml.jackson.core:jackson-annotations:jar:2.7.0:compile
[INFO] |  +- xalan:xalan:jar:2.7.2:compile
[INFO] |  |  \- xalan:serializer:jar:2.7.2:compile
[INFO] |  +- net.arnx:wmf2svg:jar:0.9.8:compile
[INFO] |  +- org.antlr:antlr-runtime:jar:3.5.2:compile
[INFO] |  +- org.antlr:stringtemplate:jar:3.2.1:compile
[INFO] |  |  \- antlr:antlr:jar:2.7.7:compile
[INFO] |  +- com.google.guava:guava:jar:19.0:compile
[INFO] |  \- com.thedeanda:lorem:jar:2.0:compile
[INFO] +- org.plutext:jaxb-xslfo:jar:1.0.1:compile
[INFO] +- org.apache.xmlgraphics:xmlgraphics-commons:jar:2.1:compile
[INFO] +- org.apache.xmlgraphics:fop:jar:2.2:compile
[INFO] |  +- org.apache.xmlgraphics:batik-svg-dom:jar:1.9:compile
[INFO] |  |  +- org.apache.xmlgraphics:batik-css:jar:1.9:compile
[INFO] |  |  +- org.apache.xmlgraphics:batik-dom:jar:1.9:compile
[INFO] |  |  +- org.apache.xmlgraphics:batik-parser:jar:1.9:compile
[INFO] |  |  \- org.apache.xmlgraphics:batik-util:jar:1.9:compile
[INFO] |  |     +- org.apache.xmlgraphics:batik-constants:jar:1.9:compile
[INFO] |  |     \- org.apache.xmlgraphics:batik-i18n:jar:1.9:compile
[INFO] |  +- org.apache.xmlgraphics:batik-bridge:jar:1.9:compile
[INFO] |  |  +- org.apache.xmlgraphics:batik-anim:jar:1.9:compile
[INFO] |  |  +- org.apache.xmlgraphics:batik-script:jar:1.9:compile
[INFO] |  |  \- org.apache.xmlgraphics:batik-xml:jar:1.9:compile
[INFO] |  +- org.apache.xmlgraphics:batik-awt-util:jar:1.9:compile
[INFO] |  +- org.apache.xmlgraphics:batik-gvt:jar:1.9:compile
[INFO] |  +- org.apache.xmlgraphics:batik-transcoder:jar:1.9:compile
[INFO] |  |  \- org.apache.xmlgraphics:batik-svggen:jar:1.9:compile
[INFO] |  +- org.apache.xmlgraphics:batik-extension:jar:1.9:compile
[INFO] |  +- org.apache.xmlgraphics:batik-ext:jar:1.9:compile
[INFO] |  \- org.apache.pdfbox:fontbox:jar:2.0.4:compile
[INFO] +- org.apache.avalon.framework:avalon-framework-api:jar:4.3.1:compile
[INFO] +- org.apache.avalon.framework:avalon-framework-impl:jar:4.3.1:compile


You might also configure logging.