CentOs安装Libreoffice
1、去官网下载Libreoffice,选择rpm安装版本的,上传到服务器’
注意:linux rpm版本
tar -xvf LibreOffice_7.5.1_Linux_x86-64_rpm.tar.gz
1. cd LibreOffice_7.5.1_Linux_x86-64_rpm/RPMS
2. yum localinstall *.rpm
2、安装成功后会在 /opt/ 下生产一个 libreof
fice7.5 的文件夹,我们需要的启动命令是:/opt/libreoffice7.5/program/soffice
注意:访问端口,自行修改,如需远程使用,请打开防火墙
nohup /opt/libreoffice7.5/program/soffice -headless -accept="socket,host=0.0.0.0,port=8100;urp;" -nofirststartwizard &
1.通过 ps -ef |grep soffice 获取pid
2.通过kill -9 pid 关闭服务
3、 Linux本地测试文档转换命令
自行修改转换格式,注意文件路径和转换后路径
/opt/libreoffice7.5/program/soffice --headless --invisible --convert-to pdf 文件所在路径 --outdir 转换后路径
整合springboot
1、导入maven
jodconverter2.2.1:不支持docx等文件的转换
jodconverter2.2.2: Maven中央仓库只有到2.2.1的版本
jodconverter2.2.2jar包,提取码:2x79 ,请自行导入私有库。
<dependency>
<groupId>com.artofsolving</groupId>
<artifactId>jodconverter</artifactId>
<version>2.2.2</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/resources/lib/jodconverter-2.2.2.jar</systemPath> <!--项目根目录下的lib文件夹下-->
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>juh</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>ridl</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>unoil</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-spring-boot-starter</artifactId>
<version>4.2.0</version>
</dependency>
2、使用
配置安装服务器地址,以及启用到端口
@Value("${lesoft.libreoffce.location:127.0.0.1}")
private String libreoffceLocation;
@Value("${lesoft.libreoffce.port:8100}")
private Integer libreoffceProt;
HttpServletResponse response = SpringContext.currentResponse();、
// 设置头信息
String fileName = UUID.randomUUID().toString() + ".pdf";
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
response.setContentType("application/pdf");
OutputStream outputStream = response.getOutputStream();
// 获取文档输入流,根据业务自行修改
ByteArrayInputStream inputStream = getNiceXWPFDocByInputStream(doc);
doDocumentConvert(inputStream,outputStream, "docx","pdf");
/**
* 文档转换为输入流
* @param doc
* @return
* @throws IOException
*/
private ByteArrayInputStream getNiceXWPFDocByInputStream(NiceXWPFDocument doc) throws IOException {
// 获取文档对象
XWPFDocument document = doc.getXWPFDocument();
//二进制OutputStream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
document.write(baos);//文档写入流
//OutputStream写入InputStream二进制流
ByteArrayInputStream inputStream = new ByteArrayInputStream(baos.toByteArray());
return inputStream;
}
public void doDocumentConvert(InputStream inputStream, OutputStream outputStream, String form, String to) {
// 建立连接,根据配置文件获取
SocketOpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
try {
connection.connect();
System.out.println("获取连接成功!");
} catch (ConnectException e) {
System.out.println("获取连接失败!");
e.printStackTrace();
}
// 转换
StreamOpenOfficeDocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
// 转换格式
DocumentFormat docDocumentFormat = (new DefaultDocumentFormatRegistry()).getFormatByFileExtension(form);
DocumentFormat pdfDocumentFormat = (new DefaultDocumentFormatRegistry()).getFormatByFileExtension(to);
// 多种转换方式,文件方式,流方式
converter.convert(inputStream,docDocumentFormat, outputStream,pdfDocumentFormat);
// 关闭连接
connection.disconnect();
}