javalibreofficejodconverter

Convert RTF to PDF jodconverter


I'm trying to convert RTF to PDF, but I get this error:

Exception in thread "main" java.lang.NullPointerException
    at isc.libreoffice.JODRTFtoPDF.rtfToPdf(JODRTFtoPDF.java:24)
    at isc.libreoffice.JODRTFtoPDF.rtfToPdf(JODRTFtoPDF.java:15)

Here's how I'm trying to convert:

package isc.libreoffice;

import org.jodconverter.core.DocumentConverter;
import org.jodconverter.core.office.OfficeException;

import java.io.File;


public class JODRTFtoPDF
{

    private DocumentConverter documentConverter;

    public String rtfToPdf(String filePath) {
        return rtfToPdf(filePath, null);
    }

    public String rtfToPdf(String filePath, String outputFilePath) {
        if (outputFilePath == null) {
            outputFilePath = filePath.substring(0, filePath.lastIndexOf(".")) + ".pdf";
        }

        try {
            documentConverter.convert(new File(filePath)).to(new File(outputFilePath)).execute();
        } catch (OfficeException e) {
            throw new RuntimeException();
        }

        return outputFilePath;
    }
}

I understand that the error is that I didn't initialise the DocumentConverter class object. But I haven't found an example of how someone would do it.

I tried to use the code from the wiki on github:

final LocalOfficeManager officeManager = LocalOfficeManager.install();

File inputFile = new File("1.rtf");
File outputFile = new File("1.pdf");
try {

    officeManager.start();

    JodConverter
            .convert(inputFile)
            .to(outputFile)
            .execute();
} catch (OfficeException e) {
    throw new RuntimeException(e);
} finally {
    OfficeUtils.stopQuietly(officeManager);
}

But there's an error there too:

Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
    at java.net.URLClassLoader.findClass(URLClassLoader.java:387)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    ... 2 more

Solution

  • The following works for me:

    package com.technojeeves.jodconvert;
    
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.nio.file.Files;
    import java.nio.file.Path;
    
    import org.jodconverter.core.document.DefaultDocumentFormatRegistry;
    import org.jodconverter.core.office.OfficeUtils;
    import org.jodconverter.local.JodConverter;
    import org.jodconverter.local.office.LocalOfficeManager;
    
    public class App {
        public static void main(String[] args) throws Exception {
            final LocalOfficeManager officeManager = LocalOfficeManager.install();
            try {
    
                // Start an office process and connect to the started instance (on port 2002).
                officeManager.start();
                try (InputStream in = Files.newInputStream(Path.of(args[0]));
                        OutputStream out = Files.newOutputStream(Path.of(args[1]))) {
                    JodConverter.convert(in).as(DefaultDocumentFormatRegistry.RTF).to(out)
                            .as(DefaultDocumentFormatRegistry.PDF).execute();
                }
            } finally {
                // Stop the office process
                OfficeUtils.stopQuietly(officeManager);
            }
        }
    }
    

    I created a Maven project with the following pom:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.technojeeves.jodconvert</groupId>
        <artifactId>jodconvert</artifactId>
        <name>jodconvert</name>
        <version>1.0-SNAPSHOT</version>
        <dependencies>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-api</artifactId>
                <version>2.23.1</version>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-core</artifactId>
                <version>2.23.1</version>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-slf4j2-impl</artifactId>
                <version>2.23.1</version>
            </dependency>
                <dependency>
                    <groupId>org.jodconverter</groupId>
                    <artifactId>jodconverter-local-lo</artifactId>
                    <version>4.4.7</version>
                </dependency>
            </dependencies>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-assembly-plugin</artifactId>
                        <executions>
                            <execution>
                                <phase>package</phase>
                                <goals>
                                    <goal>single</goal>
                                </goals>
                                <configuration>
                                    <archive>
                                        <manifest>
                                            <mainClass>${exec.mainClass}</mainClass>
                                        </manifest>
                                    </archive>
                                    <descriptorRefs>
                                        <descriptorRef>jar-with-dependencies</descriptorRef>
                                    </descriptorRefs>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
            <properties>
                <maven.compiler.release>11</maven.compiler.release>
                <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
                <logback.version>1.5.6</logback.version>
                <exec.mainClass>com.technojeeves.jodconvert.App</exec.mainClass>
            </properties>
        </project>