0
votes

I am using docx4j 2.8.1 and I tried to convert several different docx file, but i have always the same issue. maybe the issue is coming from the version of the library or some dependency missing.

Code:

package test;

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;


public class pdfConverter {
    public static void main(String[] args) {
        createPDF();
    }

    private static void createPDF() {
        try {

            // 1) Load DOCX into WordprocessingMLPackage
            InputStream is = new FileInputStream(
                    new File("D:/TestDoc/Res.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:/TestDoc/Res.pdf"));
            PdfConversion converter = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(
                    wordMLPackage);
            converter.output(out, pdfSettings);

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

}

Error:

    org.docx4j.openpackaging.exceptions.Docx4JException: FOP issues
    at org.docx4j.convert.out.pdf.viaXSLFO.Conversion.output(Conversion.java:374)
    at test.pdfConverter.createPDF(pdfConverter.java:42)
    at test.pdfConverter.main(pdfConverter.java:21)
Caused by: java.lang.NullPointerException
    at org.docx4j.XmlUtils.transform(XmlUtils.java:842)
    at org.docx4j.XmlUtils.transform(XmlUtils.java:802)
    at org.docx4j.convert.out.pdf.viaXSLFO.Conversion.output(Conversion.java:349)
    ... 2 more
1
You'll have to post your input docx somewhere, pleaseJasonPlutext
I tired randomly docx files, all of them catch the same exception, that is why i haven't added the template i am working with.oueslatibilel
If it happens with even a simple "hello world" document, then yes, you have to look elsewhere for the problem. line 842 of github.com/plutext/docx4j/blob/VERSION_2_8_1/src/main/java/org/… suggests template is null. github.com/plutext/docx4j/blob/VERSION_2_8_1/src/main/java/org/… at line 98 should be throwing an exception. Check earlier in your logs.JasonPlutext
I changed the jar, I tried 2.8.0 and its working fine now.oueslatibilel
Where did you get your 2.8.1 jar from?JasonPlutext

1 Answers

0
votes

Solved by changing to jars. i used 2.8.0 and its fine now.