Backend Development 8 min read

Implementing Online Preview of Office Documents in Java Using OpenOffice and JODConverter

This article explains how to build a Java backend that converts Word, Excel, PowerPoint, and text files to PDF streams with OpenOffice and JODConverter, integrates the conversion utility into a service layer, and provides controller endpoints for online document preview in browsers.

Top Architect
Top Architect
Top Architect
Implementing Online Preview of Office Documents in Java Using OpenOffice and JODConverter

Online preview of office documents (Word, Excel, PowerPoint, txt) is a common requirement; this guide shows how to achieve it for free using Apache OpenOffice as a conversion engine and the JODConverter library in a Java backend.

First, download and install Apache OpenOffice from the official site. After installation, add the JODConverter dependency to your Maven pom.xml :

<!--openoffice-->
<dependency>
    <groupId>com.artofsolving</groupId>
    <artifactId>jodconverter</artifactId>
    <version>2.2.1</version>
</dependency>

Next, create a utility class FileConvertUtil that handles conversion of local and remote files to PDF streams. The class establishes a socket connection to the OpenOffice service (default port 8100), uses StreamOpenOfficeDocumentConverter to convert the input stream, and returns the resulting PDF as an InputStream . It also includes a helper method to convert an OutputStream back to an InputStream .

import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.DocumentFormat;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class FileConvertUtil {
    private static final String DEFAULT_SUFFIX = "pdf";
    private static final Integer OPENOFFICE_PORT = 8100;

    public static InputStream convertLocaleFile(String sourcePath, String suffix) throws Exception {
        File inputFile = new File(sourcePath);
        InputStream inputStream = new FileInputStream(inputFile);
        return covertCommonByStream(inputStream, suffix);
    }

    public static InputStream convertNetFile(String netFileUrl, String suffix) throws Exception {
        URL url = new URL(netFileUrl);
        URLConnection urlconn = url.openConnection();
        urlconn.connect();
        HttpURLConnection httpconn = (HttpURLConnection) urlconn;
        int httpResult = httpconn.getResponseCode();
        if (httpResult == HttpURLConnection.HTTP_OK) {
            InputStream inputStream = urlconn.getInputStream();
            return covertCommonByStream(inputStream, suffix);
        }
        return null;
    }

    public static InputStream covertCommonByStream(InputStream inputStream, String suffix) throws Exception {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        OpenOfficeConnection connection = new SocketOpenOfficeConnection(OPENOFFICE_PORT);
        connection.connect();
        DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
        DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry();
        DocumentFormat targetFormat = formatReg.getFormatByFileExtension(DEFAULT_SUFFIX);
        DocumentFormat sourceFormat = formatReg.getFormatByFileExtension(suffix);
        converter.convert(inputStream, sourceFormat, out, targetFormat);
        connection.disconnect();
        return outputStreamConvertInputStream(out);
    }

    public static ByteArrayInputStream outputStreamConvertInputStream(final OutputStream out) throws Exception {
        ByteArrayOutputStream baos = (ByteArrayOutputStream) out;
        return new ByteArrayInputStream(baos.toByteArray());
    }
}

In the service layer, expose a method onlinePreview that validates the file extension, calls FileConvertUtil.convertNetFile to obtain the PDF stream, and writes the stream to the HTTP response.

public void onlinePreview(String url, HttpServletResponse response) throws Exception {
    String[] str = SmartStringUtil.split(url, "\\.");
    if (str.length == 0) {
        throw new Exception("文件格式不正确");
    }
    String suffix = str[str.length - 1];
    if (!suffix.matches("txt|doc|docx|xls|xlsx|ppt|pptx")) {
        throw new Exception("文件格式不支持预览");
    }
    InputStream in = FileConvertUtil.convertNetFile(url, suffix);
    OutputStream outputStream = response.getOutputStream();
    byte[] buff = new byte[1024];
    int n;
    while ((n = in.read(buff)) != -1) {
        outputStream.write(buff, 0, n);
    }
    outputStream.flush();
    outputStream.close();
    in.close();
}

The controller simply forwards the request to the service:

@ApiOperation(value = "系统文件在线预览接口")
@PostMapping("/api/file/onlinePreview")
public void onlinePreview(@RequestParam("url") String url, HttpServletResponse response) throws Exception {
    fileService.onlinePreview(url, response);
}

After deploying the application and ensuring OpenOffice is running on port 8100, accessing the endpoint with a file URL returns a PDF stream that browsers can display directly, providing seamless online preview for various office formats.

Images in the original article demonstrate the preview results for Excel and Word files.

backendJavaPDFdocument conversionOpenOfficeonline preview
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.