A memo when using jodConverter with OnlineConverter. Since SpringBoot was used, it is implemented using JODConverter Spring Boot Starter.
application.yml
jodconverter:
online:
enabled: true
url:[URL of Online Converter server]
Add the following settings to dependencies of build.gradle
compile 'org.jodconverter:jodconverter-spring-boot-starter:4.1.0'
Set the LibreOffice installation destination in application.yml
application.yml
jodconverter:
enabled: true
officeHome:[LibreOffice installation destination]
@RestController
public class SampleController {
@Autowired
private DocumentConverter documentConverter;
@RequestMapping(path = "/api/jodconverter/lool/convert-to/{outputExtension}", method = RequestMethod.POST)
public Resource convert(
@RequestParam("data") MultipartFile file,
@PathVariable("outputExtension") String outputExtension
) throws IOException, OfficeException {
//Get file information before conversion
String inputExtension = FilenameUtils.getExtension(file.getOriginalFilename());
DocumentFormatRegistry registry = this.documentConverter.getFormatRegistry();
DocumentFormat inputFormat = registry.getFormatByExtension(inputExtension);
if (inputFormat == null) {
inputFormat = registry.getFormatByExtension("xlsx");
}
//Post-conversion extension setting
DocumentFormat outputFormat = registry.getFormatByExtension(outputExtension);
//Conversion process
try (
InputStream is = file.getInputStream();
ByteArrayOutputStream os = new ByteArrayOutputStream();
) {
this.documentConverter.convert(is).as(inputFormat).to(os).as(outputFormat).execute();
return new ByteArrayResource(os.toByteArray());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
I haven't had a chance to use OnlineConverter before, so I learned how to use it.
However, LocalConverter is easier to install because there is no server implementation when using OnlineConverter. I wonder if I will use it unless there is a good reason. .. ..
Recommended Posts