5

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

OutsideCoder
  • 346
  • 3
  • 16

2 Answers2

7

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);
        }
    }
OutsideCoder
  • 346
  • 3
  • 16
  • This approach worked for me. But, seeing old code, what does the "portsNumbers (int ...)" method refer to? What "portsNumbers" means? – BicaBicudo Apr 04 '19 at 18:14
  • AFAIK this just defines which port the OfficeManager is going to use. Again AFAIK the Office Manager is the bit which utilises Libre Office. – OutsideCoder Apr 09 '19 at 14:53
  • But if I could'nt install libreoffice in my workstation, so I will need to install libreoffice on my JBoss server (remote machine) - passing home office? Is this doesn't looks like strange? – BicaBicudo Apr 11 '19 at 18:52
  • I'm no expert but if you can Ping the location and port then if it doesn't work after that maybe check your firewall settings. – OutsideCoder Apr 11 '19 at 21:51
6

Yes, this is my fault. The JODConverter documentation needs some major improvements. I created the static JodConverter.convert methods to ease the interaction with the jodconverter libraries, but there is no place in the documentation where I clearly state that this static class will use an office manager that would have been created as the default manager for all document converters.

This is done using the "install" function while creating an office manager.

So I thank you for being such a smart coder that figured it out, this stackoverflow post will help many developers for sure!!

sbraconnier
  • 465
  • 5
  • 11
  • Ah thanks for the 'smart coder' comment - I'm not sure about that one but thanks! People want to see the results of JODConverter quickly to assess its fit-for-purpose for their documents, so I think a great addition would be a full piece of copy and paste code and simple instruction on how to get started, that people can use to try out JODConverter quickly. Obvious place would be Getting Started section. Keep up the good work as its really coming together :) – OutsideCoder Sep 23 '18 at 11:53