package com.ots.project.tool.libreoffice;
|
|
import lombok.extern.slf4j.Slf4j;
|
import org.jodconverter.core.DocumentConverter;
|
import org.jodconverter.core.office.OfficeException;
|
import org.jodconverter.core.office.OfficeManager;
|
import org.jodconverter.local.LocalConverter;
|
import org.jodconverter.local.office.LocalOfficeManager;
|
import org.jodconverter.local.process.WindowsProcessManager;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.stereotype.Component;
|
import org.springframework.util.StringUtils;
|
|
import javax.annotation.PostConstruct;
|
import javax.annotation.PreDestroy;
|
import java.io.File;
|
import java.util.concurrent.BlockingQueue;
|
import java.util.concurrent.LinkedBlockingDeque;
|
import java.util.concurrent.locks.Lock;
|
import java.util.concurrent.locks.ReentrantLock;
|
|
/**
|
* @version 1.0
|
* @Author linzhijie
|
* @Description TODO
|
* @Date 2022/10/21 10:03
|
*/
|
@Slf4j
|
@Component
|
public class Office2PDFUtils {
|
|
@Value("${jodconverter.local.office-home}")
|
private String officeHome;
|
|
@Value("${jodconverter.local.portNumbers}")
|
private String portNumbers;
|
|
private static BlockingQueue<OfficeManager> OfficeManagerQueue = new LinkedBlockingDeque<>();
|
|
private static Lock lock = new ReentrantLock();
|
|
/**
|
* @Description: 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件
|
* @method: openOfficeToPDF
|
* @Param: inputFilePath
|
* @Param: outputFilePath
|
* @return: boolean
|
* @auther: LHL
|
*/
|
public boolean openOfficeToPDF(String inputFilePath, String outputFilePath) {
|
return office2Pdf(inputFilePath, outputFilePath);
|
}
|
|
/**
|
* @Description: 根据操作系统的名称,获取OpenOffice.org 的安装目录
|
* @method: getOfficeHome
|
* @Param:
|
* @return: java.lang.String OpenOffice.org 的安装目录
|
* @auther: LHL
|
*/
|
/* private String getOfficeHome() {
|
String osName = System.getProperty("os.name");
|
log.debug("操作系统名称:" + osName);
|
if (Pattern.matches("Linux.*", osName)) {
|
return linuxPath;
|
} else if (Pattern.matches("Windows.*", osName)) {
|
return winPath;
|
} else if (Pattern.matches("Mac.*", osName)) {
|
return winPath;
|
}
|
return null;
|
}*/
|
|
/**
|
* @Description: 在bean创建实例时, 连接OpenOffice.org 并且启动OpenOffice.org
|
* @method: initOpenOfficeService
|
* @Param:
|
* @return: void
|
* @auther: LHL
|
*/
|
@PostConstruct
|
public void initOpenOfficeService() {
|
|
LocalOfficeManager.Builder builder = LocalOfficeManager.builder();
|
builder.officeHome(officeHome);
|
builder.portNumbers(2002);
|
builder.processTimeout(12000L);
|
builder.processRetryInterval(250L);
|
builder.maxTasksPerProcess(200);
|
builder.taskQueueTimeout(30000L);
|
for (String port : portNumbers.split(",")) {
|
builder.portNumbers(Integer.valueOf(port));
|
OfficeManager OfficeManager = builder.build();
|
try {
|
OfficeManager.start();
|
log.debug("***********************officeManager 启动!***********************");
|
} catch (OfficeException e) {
|
//打印日志
|
log.debug("***********************启动 openOffice 失败!***********************");
|
e.printStackTrace();
|
}
|
try {
|
OfficeManagerQueue.put(OfficeManager);
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
|
/**
|
* @Description: bean销毁时停止
|
* @method: destroyOpenOfficeService
|
* @Param:
|
* @return: void
|
* @auther: LHL
|
*/
|
@PreDestroy
|
public void destroyOpenOfficeService() {
|
for (OfficeManager manager : OfficeManagerQueue) {
|
try {
|
log.debug("关闭 officeManager");
|
manager.stop();
|
} catch (OfficeException e) {
|
log.debug("officeManager关闭失败!" + e.getMessage());
|
e.printStackTrace();
|
}
|
}
|
}
|
|
/**
|
* @Description: 转换文件
|
* @method: convertFile
|
* @Param: inputFile
|
* @Param: outputFilePath
|
* @Param: officeManager
|
* @return: java.io.File
|
* @auther: LHL
|
*/
|
private File convertFile(File inputFile, String outputFilePath, OfficeManager officeManager) throws OfficeException {
|
File outputFile = new File(outputFilePath);
|
// 假如目标路径不存在,则新建该路径
|
if (!outputFile.getParentFile().exists()) {
|
outputFile.getParentFile().mkdirs();
|
}
|
DocumentConverter converter = LocalConverter.builder()
|
.officeManager(officeManager)
|
.build();
|
// 锁
|
lock.lock();
|
//converter.convert(inputFile).to(outputFile).execute();
|
converter.convert(inputFile).to(outputFile).execute();
|
//释放锁
|
lock.unlock();
|
try {
|
OfficeManagerQueue.put(officeManager);
|
log.debug("blockingQueue puted OfficeManagerQueue size :" + OfficeManagerQueue.size());
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
}
|
log.debug("文件:" + inputFile.getAbsolutePath() + "\n转换为目标文件:" + outputFile + "\n成功!");
|
return outputFile;
|
}
|
|
/**
|
* 文件名需要唯一
|
*/
|
public boolean office2Pdf(String inputFilePath, String outputFilePath) {
|
OfficeManager officeManager = null;
|
try {
|
if (StringUtils.isEmpty(inputFilePath)) {
|
log.debug("输入文件地址为空,转换终止!");
|
return false;
|
}
|
File inputFile = new File(inputFilePath);
|
if (!inputFile.exists()) {
|
log.debug("输入文件不存在,转换终止!");
|
return false;
|
}
|
officeManager = OfficeManagerQueue.take();
|
log.debug("blockingQueue taked , OfficeManagerQueue size :" + OfficeManagerQueue.size());
|
File file = convertFile(inputFile, outputFilePath, officeManager);
|
if (!file.exists()) {
|
log.debug("转换文件不存在!转换失败!!");
|
return false;
|
}
|
return true;
|
} catch (Exception e) {
|
e.printStackTrace();
|
log.debug("转换异常");
|
return false;
|
}
|
}
|
}
|