Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JODConverter telling me office manager is required in order to build a converter

I'm trying to get JODConverter to work on Window 10 with Jdk 1.8.0_144. As you can see from the code I thought it might be a timing issue hence the delay. As you can see JODConverter thinks the OfficeManager is running. I'm using the follow code:

import java.io.File;
import org.jodconverter.JodConverter;
import org.jodconverter.office.LocalOfficeManager;
import org.jodconverter.office.OfficeException;
import org.jodconverter.office.OfficeManager;
import org.jodconverter.office.OfficeUtils;
import org.jodconverter.process.ProcessManager;

public class JodConverterTest {

    public static void main(String[] args) throws OfficeException, InterruptedException {

        OfficeManager officeManager
                = LocalOfficeManager.builder()
                        .officeHome("C:\\Program Files\\LibreOffice")
                        .portNumbers(2372)
                        .build();
        officeManager.start();

        File inputFile = new File("c:\\test\\rtf.rtf");
        File outputFile = new File("c:\\test\\rtf.pdf");

        try {

            System.out.println("officeManager.isRunning()="+officeManager.isRunning());
            Thread.sleep(10000);
            System.out.println("officeManager.isRunning()="+officeManager.isRunning());
            JodConverter.convert(inputFile).to(outputFile).execute();
        } finally {
            // Stop the office process
            OfficeUtils.stopQuietly(officeManager);
        }
    }
}

I get the following error when I run it:-

officeManager.isRunning()=true
officeManager.isRunning()=true
Exception in thread "main" java.lang.IllegalStateException: An office manager is required in order to build a converter.
    at org.jodconverter.job.AbstractConverter.<init>(AbstractConverter.java:57)
    at org.jodconverter.LocalConverter.<init>(LocalConverter.java:93)
    at org.jodconverter.LocalConverter.<init>(LocalConverter.java:49)
    at org.jodconverter.LocalConverter$Builder.build(LocalConverter.java:202)
    at org.jodconverter.LocalConverter.make(LocalConverter.java:73)
    at org.jodconverter.JodConverter.convert(JodConverter.java:48)
    at ZPlaying.JodConverterTest.main(JodConverterTest.java:30)
------------------------------------------------------------------------
BUILD FAILURE

Things I've tried:- - Changing the port number - Researching to see if I can find the classpath of the java Process Manager and adding the following but I couldn't find the classpath of the ProcessManager as I don't really know much about that:- .processManager("com.example.foo.CustomProcessManager") - also wondered whether it is something to do with running via Netbeans?

Here is the applicable maven dependency:-

        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-local</artifactId>
            <version>4.1.1</version>
        </dependency>

I've installed Libre (fresh install) in C:\Program Files\LibreOffice

like image 472
OutsideCoder Avatar asked Dec 14 '22 14:12

OutsideCoder


1 Answers

Got it to work. Here is a solution:-

package ZPlaying;

import java.io.File;
import org.jodconverter.JodConverter;
import org.jodconverter.office.LocalOfficeManager;
import org.jodconverter.office.OfficeException;
import org.jodconverter.office.OfficeManager;
import org.jodconverter.office.OfficeUtils;

public class JodConverterTest {

    public static void main(String[] args) throws OfficeException, InterruptedException {
        OfficeManager officeManager = LocalOfficeManager.builder()
                .install()
                .officeHome("C:\\Program Files\\LibreOffice")
                .build();
        File inputFile = new File("c:\\test\\rtf.rtf");
        File outputFile = new File("c:\\test\\rtf.pdf");
        try {
            // Start an office process and connect to the started instance (on port 2002).
            officeManager.start();
            // Convert
            JodConverter
                    .convert(inputFile)
                    .to(outputFile)
                    .execute();
        } finally {
            // Stop the office process
            OfficeUtils.stopQuietly(officeManager);
        }
    }
like image 200
OutsideCoder Avatar answered Mar 16 '23 01:03

OutsideCoder