javaandroidodtxdocreport

Unable to convert ODT to PDF with xdocreport


I'm trying to use the xdocreport package to convert an ODT file into a PDF. I've tried 2 different variations. Here is the code and error for both variations:

Options options = Options.getFrom(DocumentKind.ODT).to(ConverterTypeTo.PDF);

InputStream raw = this.getResources().openRawResource(R.raw.timesheet_template);
IXDocReport xdocGenerator = XDocReportRegistry.getRegistry().loadReport(raw, TemplateEngineKind.Freemarker);

IContext context = xdocGenerator.createContext();
IConverter converter = ConverterRegistry.getRegistry().getConverter(options);

OutputStream out = new FileOutputStream(HOME_PATH+"/test.pdf");

xdocGenerator.convert(context, options, out);

raw.close();
out.close();

ERROR:

fr.opensagres.xdocreport.core.XDocReportException: Null template engine. Set template engine with IXDocReport#setTemplateEngine.

Other method:

Options options = Options.getFrom(DocumentKind.ODT).to(ConverterTypeTo.PDF);

IConverter converter = ConverterRegistry.getRegistry().getConverter(options);

InputStream raw = this.getResources().openRawResource(R.raw.timesheet_template);
OutputStream out = new FileOutputStream(HOME_PATH+"/test.pdf");

converter.convert(raw, out, options);

raw.close();
out.close();

ERROR:

java.lang.NullPointerException: Attempt to invoke interface method 'void fr.opensagres.xdocreport.converter.IConverter.convert(java.io.InputStream, java.io.OutputStream, fr.opensagres.xdocreport.converter.Options)' on a null object reference

From reading online and the errors themselves I believe there is an underlying dependency that is not available by default in android. I don't know what it is or how to include it though.

An important thing to note is I was able to accomplish this without issue using Aspose library. They have a water mark though which is why I want to use xdocreport.

I'm importing the following in gradle:

implementation 'fr.opensagres.xdocreport:fr.opensagres.xdocreport.document.odt:2.1.0'

Any help would be appreciated.


ANSWER: Based on the accepted answer, here's how I made it work. First the gradle dependencies are as fallows:

implementation 'fr.opensagres.xdocreport:fr.opensagres.xdocreport.document.odt:2.1.0'
implementation 'fr.opensagres.xdocreport:fr.opensagres.xdocreport.template.freemarker:2.1.0'
implementation 'fr.opensagres.xdocreport:fr.opensagres.xdocreport.converter.odt.odfdom:2.1.0'
implementation 'ro.andob.androidawt:androidawt:1.0.4'

You will also need the following in android section:

configurations {
    all { // You should exclude one of them not both of them
        exclude group: "com.sun.activation", module: "jakarta.activation"
    }
}

In settings.gradle under repositories for both pluginManagement and dependencyResolutionManagement add the following:

maven { url "https://andob.io/repository/open_source" }

I'm not sure if this is required, but I added the following in proguard-rules.pro:

-dontwarn org.bouncycastle.**
-dontwarn java.lang.invoke.**

The code is the same as in the question. This setup will resolve the duplicate class issue, add the converter for XDocReport to use and add the java.awt dependencies.


Solution

  • Your errors happen because required dependencies are missing. To fix it add these to your build.gradle

    implementation 'fr.opensagres.xdocreport:fr.opensagres.xdocreport.template.freemarker:2.1.0'
    implementation 'fr.opensagres.xdocreport:fr.opensagres.xdocreport.converter.odt.odfdom:2.1.0'
    

    These provide the Freemarker engine and ODT→PDF converter. Once added, your code should work unless it hits AWT-related classes not supported on Android, in which case XDocReport may not be usable on-device.