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 service that converts Word, Excel, PowerPoint, and text files to PDF streams with OpenOffice and JODConverter, integrates the conversion utility into a Spring service and controller, and demonstrates browser‑based online preview of the documents.

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

The article introduces a common workplace requirement: previewing office documents (Word, Excel, PPT, TXT) online without third‑party paid services, and proposes a free solution based on Apache OpenOffice and the JODConverter library.

First, download and install Apache OpenOffice from the official website (https://www.openoffice.org/download) according to the operating system.

Then add the JODConverter Maven dependency to the project's pom.xml :

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

A utility class FileConvertUtil is provided to handle the conversion of local or remote files to PDF streams. It creates an OpenOffice connection on port 8100, selects the appropriate source and target formats, performs the conversion, and returns an InputStream of the PDF.

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;
    // convert local file
    public static InputStream convertLocaleFile(String sourcePath, String suffix) throws Exception { ... }
    // convert remote file
    public static InputStream convertNetFile(String netFileUrl, String suffix) throws Exception { ... }
    // common conversion logic
    public static InputStream covertCommonByStream(InputStream inputStream, String suffix) throws Exception { ... }
    public static ByteArrayInputStream outputStreamConvertInputStream(final OutputStream out) throws Exception { ... }
    public static void main(String[] args) throws IOException { /* test code */ }
}

The service layer method onlinePreview validates the file extension, calls FileConvertUtil.convertNetFile to obtain the PDF stream, and writes the stream to the HTTP response, enabling the browser to display the preview.

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 (!Arrays.asList("txt","doc","docx","xls","xlsx","ppt","pptx").contains(suffix)) {
        throw new Exception("文件格式不支持预览");
    }
    InputStream in = FileConvertUtil.convertNetFile(url, suffix);
    OutputStream out = response.getOutputStream();
    byte[] buff = new byte[1024];
    int n;
    while ((n = in.read(buff)) != -1) { out.write(buff, 0, n); }
    out.flush();
    out.close();
    in.close();
}

The controller exposes a POST endpoint /api/file/onlinePreview that receives the file URL and delegates to the service method.

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

When the endpoint is called, the document is converted to a PDF stream and displayed directly in the browser, as shown in the included screenshots of the preview results for Excel and other formats.

BackendJavaFileConversionOpenOfficeJODConverterOnlinePreview
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.