0

I am trying to convert a word docx file to a pdf file, all using java, and without any user interaction.

This is my code so far, I am using the docx4j library.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package richard.fileupload;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.docx4j.convert.out.pdf.PdfConversion;
import org.docx4j.convert.out.pdf.viaXSLFO.PdfSettings;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
/**
 *
 * @author Richard
 */
public class pdfConverter {
        public static void main(String[] args) {
        createPDF();
        createPDF();
    }

    private static void createPDF() {
        try {
            long start = System.currentTimeMillis();

            // 1) Load DOCX into WordprocessingMLPackage
            InputStream is = new FileInputStream(new File(
                    "D:/HelloWorld.docx"));
            WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
                    .load(is);

            // 2) Prepare Pdf settings
            PdfSettings pdfSettings = new PdfSettings();

            // 3) Convert WordprocessingMLPackage to Pdf
            OutputStream out = new FileOutputStream(new File(
                    "D:/HelloWorld.pdf"));
            PdfConversion converter = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(
                    wordMLPackage);
            converter.output(out, pdfSettings);

            System.err.println("Generate pdf/HelloWorld.pdf with "
                    + (System.currentTimeMillis() - start) + "ms");

        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

}

However I am getting this error when I try to run, it compiles just fine

run:
java.lang.NoClassDefFoundError: org/apache/log4j/Logger
    at org.docx4j.openpackaging.Base.<clinit>(Base.java:42)
    at richard.fileupload.pdfConverter.createPDF(pdfConverter.java:32)
    at richard.fileupload.pdfConverter.main(pdfConverter.java:21)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    ... 3 more
java.lang.NoClassDefFoundError: Could not initialize class org.docx4j.openpackaging.packages.WordprocessingMLPackage
    at richard.fileupload.pdfConverter.createPDF(pdfConverter.java:32)
    at richard.fileupload.pdfConverter.main(pdfConverter.java:22)
BUILD SUCCESSFUL (total time: 0 seconds)

any idea what the error is and what is the cause of this?

EDIT I am now getting this error

java.lang.NoClassDefFoundError: org/apache/fop/apps/FopFactory
    at org.docx4j.convert.out.pdf.viaXSLFO.Conversion.output(Conversion.java:231)
    at richard.fileupload.pdfConverter.createPDF(pdfConverter.java:43)
    at richard.fileupload.pdfConverter.main(pdfConverter.java:22)
JasonPlutext
  • 15,352
  • 4
  • 44
  • 84
user2065929
  • 1,065
  • 4
  • 21
  • 35
  • 2
    There is a dependency on Apache's Logger. Get the JAR file and put it in the CLASSPATH. http://logging.apache.org/log4j/1.2/download.html – Dave Jarvis Apr 17 '13 at 20:20
  • Check this post: http://stackoverflow.com/questions/5376311/troubleshoot-noclassdeffounderror-in-java I think it may help you – Barranka Apr 17 '13 at 20:20
  • 2
    I hope you won't run into jar hell. If this web app or enterprise app running in some application server (JBoss, GlassFish, WebSphere, etc). Wiht some app servers you can leverage the log4j which is supplied with the app server, although this may make you app server dependent. If you ship your own log4j, you may have to struggle to avoid collision with the other log4j. If your app is a desktop software, avoid my blurb. – Csaba Toth Apr 17 '13 at 20:29
  • Hi there yes this is a web app running on glassfish server, will this solution not work with this ? – user2065929 Apr 17 '13 at 20:36
  • Thanks i have solved that issue but have run into a new error – user2065929 Apr 17 '13 at 21:08

0 Answers0