1. 首页
  2. >
  3. 技术信息
  4. >
  5. 技术文章

基于OpenOffice、libreOffice实现文档转化

在很多管理系统中都要求上传相关的文档文件实现在线预览功能

本篇文章主要来讲解如何实现:

主要实现将文档转化为pdf预览 需要使用的组件有(openOffice和libreOffoce)


OpenOffice安装:

基于OpenOffice、libreOffice实现文档转化

图片来源于openOffice官网

安装openoffice组件(好处是支持win 和 liunx 根据所需安装 我们这里在win下面使用)

下载地址为:http://www.openoffice.org/download/

安装以后在安装目录执行启动:

1) Win:安装目录C:\Program Files (x86)\OpenOffice 4\program 则需要在此目录下执行cmd命令

soffice -headless -accept=“socket,host=127.0.0.1,port=8100;urp;” -nofirststartwizard

2) Liunx:安装目录为/opt/openoffice4/program 则需要执行以下命令

soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &

查看是否启动成功:ps -aux | grep oppenoffice


核心代码:

1.实现文件上传功能:(具体代码忽略)

2.实现文档转化核心代码部分

/**
* 文档转化核心代码
* @param convertFile 源文件
* @param convertFile 目标文件
*/
private void converterThread(File convertFile, File convertFile) {
// 启动线程进行服务转换
new Thread() {
public void run() {
// 建立openOffice链接 执行文件转换
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
try {
connection.connect();
DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
converter.convert(convertFile, convertFile);
if (connection != null) {
connection.disconnect();
connection = null;
}
} catch (ConnectException e) {
logger.error("建立openoffice链接错误:" + e.getMessage());
e.printStackTrace();
}
}
}.start();
}


在实际应用中openOffice 和 libreOffice 转化出现的问题大致问题有以下几点:

1.乱码问题 :主要是因为文档中使用的字体在你的本机或者是服务没有
解决办法就是将对应字体存放在系统字体中,liunx需要从新加载字体然后重启相关的转化服务

2.排版问题:  这个问题暂时没有好的解决方案,我们的做法是如果想要好的预览效果可以下载源文件

3.图片丢失问题 :这个目前我没有解决这个

4.PPT问题:支持不是很友好 PPT类型的相关转化质量很差 经常转化失败(主要是PPT的不标准引起) 达不到给客户交付 有其它班解决
我的解决办法是ppt类型直接用自己写的poi来实现转化(经过测试 目前效果还算可以)