Convert DOC to HTML with Spring Boot and OpenOffice: A Step-by-Step Guide
This guide walks through installing OpenOffice on a Windows server, launching its headless service, adding jodconverter dependencies to a Spring Boot project, configuring conversion settings, writing a controller to transform DOC files into HTML, and invoking the endpoint from a Vue front‑end, with troubleshooting tips.
Install OpenOffice
Download the Windows installer from the Apache OpenOffice site and install to a known directory, e.g. D:\OpenOffice.
Start OpenOffice headless service
Open a command prompt in the installation folder and run the following command to launch OpenOffice in headless mode:
soffice-headless-accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizardThe service listens on port 8100 if no error is reported.
Create Spring Boot project
Add JODConverter dependencies to pom.xml:
<!-- jodconverter core -->
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-core</artifactId>
<version>4.2.2</version>
</dependency>
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-spring-boot-starter</artifactId>
<version>4.2.2</version>
</dependency>
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-local</artifactId>
<version>4.2.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>Configure JODConverter
Add the following section to src/main/resources/application.yml. Ensure the office-home value matches the actual OpenOffice installation path and the port matches the one used when starting the service.
#jodconverter configuration
jodconverter:
local:
enabled: true
office-home: D:\OpenOffice
max-tasks-per-process: 10
port-numbers: 8100Prepare DOC file
Place a sample .doc file (e.g., badao.doc) under src/main/resources/static.
Implement conversion controller
package com.ruoyi.web.controller.system;
import com.ruoyi.common.core.controller.BaseController;
import org.jodconverter.DocumentConverter;
import org.jodconverter.office.OfficeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.File;
import java.io.FileNotFoundException;
@RestController
@RequestMapping("/openoffice")
public class OpenOfficeController extends BaseController {
@Autowired
private DocumentConverter converter;
@RequestMapping("/convertToHtml")
public void convertToHtml() throws OfficeException, FileNotFoundException {
File newFile = new File("D://badao.html");
File sourceFile = new File(ResourceUtils.getFile("classpath:static").getAbsolutePath() + "\\badao.doc");
converter.convert(sourceFile).to(newFile).execute();
}
}Front‑end call
Vue component with a button that triggers the conversion API:
<template>
<div class="component-upload-image">
<el-button @click="convertToHtmlRequest">doc转换成html</el-button>
</div>
</template>
<script>
import { convertToHtml } from "@/api/system/openOffice";
export default {
name: "openOffice",
methods: {
convertToHtmlRequest() {
convertToHtml().then(response => {
console.log(response);
});
}
}
};
</script>Utility function used by the component:
import request from '@/utils/request';
export function convertToHtml() {
return request({
url: '/openoffice/convertToHtml',
method: 'get'
});
}Troubleshooting
If the application logs an error such as invalid officeHome: it doesn't contain soffice.bin:D:..., verify that the office-home entry in application.yml exactly matches the OpenOffice installation directory.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
The Dominant Programmer
Resources and tutorials for programmers' advanced learning journey. Advanced tracks in Java, Python, and C#. Blog: https://blog.csdn.net/badao_liumang_qizhi
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
