使用OpenOffice实现在浏览器上预览Office文件

因为是在Ubuntu上部署,所以遇到了很多坑,记录一下。

主要是用jodconverter+openOffice实现

  1. jodconverter-core-3.0-beta-4-dist
  2. openOffice4
  3. 服务器:阿里云 + Ubuntu14 64bit

安装OpenOffice

  1. 下载OpenOffice,上面有链接

  2. 解压 tar -zxvf Apache_OpenOffice_4.1.3_Linux_x86-64_install-deb_zh-CN.tar.gz

  3. 进入目录 cd zh-CN

  4. 安装 dpkg -i *.deb

  5. 安装库,这里是个坑,程序一直报

     org.artofsolving.jodconverter.office.OfficeException: failed to start and connect
    

后来发现是少了两个库,所以这里要安装

    apt-get install 
    apt-get isntall 

主要代码:

//openoffice的安装地址,这里是Linux里的默认路径
String openOpeniceHome = "/opt/openoffice4/"
DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();
config.setOfficeHome(openOfficeHome);
OfficeManager officeManager = config.buildOfficeManager();
officeManager.start();

OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
//获取要用来生成PDF的Office文件,获取文件的真实地址,这里是javaweb中的获取方法
String relFilePath = System.getProperty("web.rootDir") + "/" + inputFilePath;
File inputFile = new File(relFilePath);
if (inputFile.exists()) {    
//获取新文件,也就是PDF文件
File outputFile = (File) re.get(SaveFile.NEWFILE);  
//转换  
converter.convert(inputFile, outputFile);
}
officeManager.stop();

转换的时候会出现乱码问题,主要是因为ubuntu上没有中文字体,从win上复制字体到/usr/share/fonts文件夹内,输入

mkfontscalesudo 
mkfontdirsudo 
fc-cache -fv

三个命令,可能需要安装,用 apt-get install 安装就好

你可能感兴趣的:(使用OpenOffice实现在浏览器上预览Office文件)