SpringBoot + OpenOffice File Conversion Service: Upload, Convert, and Preview Documents
This article introduces a SpringBoot-based service that leverages OpenOffice to upload, convert (DOC/DOCX/XLS/PPT to PDF or HTML), and preview files directly in the browser, detailing built‑in features, configuration settings, and sample controller code for seamless integration.
The project provides a SpringBoot service built on OpenOffice that converts office files (DOC, DOCX, XLS, PPT) into PDF or HTML formats and renders them directly in a browser.
Overview
Based on the OpenOffice service, the application supports file upload, conversion, local preview, and remote preview functionalities.
Built‑in Features
File upload
File conversion
File preview
Remote file preview
Code Integration
Configuration in application.properties to enable the JODConverter local mode and specify OpenOffice ports:
# Disabled by default; set to true to enable
jodconverter.local.enabled=true
# Enable multiple OpenOffice processes, each port corresponds to a process
jodconverter.local.portNumbers=8100,8200
# Maximum tasks per OpenOffice process before restart
jodconverter.local.maxTasksPerProcess=100The application automatically starts two conversion processes. Below is a sample controller that handles file upload and performs real‑time conversion to PDF and HTML:
@RestController
@RequestMapping("document")
public class ConverterController {
@Autowired
private DocumentConverter documentConverter;
/**
* File upload
*/
@RequestMapping("/upload")
public Result upload(MultipartFile file) {
try {
if (file != null) {
File parentFile = createParentFile();
String fileName = file.getOriginalFilename();
String suffix = fileName.substring(fileName.lastIndexOf("."));
String uuid = IdUtil.simpleUUID();
fileName = uuid + suffix;
File docFile = new File(parentFile, fileName);
FileUtil.writeFromStream(file.getInputStream(), docFile);
// Date directory
String fileDay = DateUtil.thisYear() + "/" + (DateUtil.thisMonth() + 1) + "/" + DateUtil.thisDayOfMonth();
String imagePath = SystemConstant.FILE + "/document/" + fileDay + "/" + fileName;
// Real‑time conversion
logger.info("Starting PDF conversion...");
File toFile = new File(parentFile, uuid + ".pdf");
documentConverter.convert(docFile).to(toFile).execute();
logger.info("Starting HTML conversion...");
toFile = new File(parentFile, uuid + ".html");
converter(docFile, toFile);
return Result.ok(imagePath);
} else {
return Result.error();
}
} catch (Exception e) {
logger.error("Conversion exception {}", e);
return Result.error();
}
}
}Conclusion
Many older articles on Java integration with OpenOffice suffer from Linux startup issues; this example only requires the OpenOffice service to be installed, and the application will automatically spawn the necessary processes.
Project Links
Source code and additional resources can be obtained by replying "Online" to the associated public account.
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
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.