java openoffice 安装_CentOS 7 安装OpenOffice并实现WordToPDF(Java调用)

1. 下载OpenOffice的安装包。

wget https://sourceforge.net/projects/openofficeorg.mirror/files/4.1.5/binaries/zh-CN/Apache_OpenOffice_4.1.5_Linux_x86-64_install-rpm_zh-CN.tar.gz/download

2.将OpenOffice解压出来。

tar -xzvf Apache_OpenOffice_4.1.5_Linux_x86-64_install-rpm_zh-CN.tar.gz

3. 解压后会有一个文件夹生成  zh-CN 。

cd RPMS/

yum localinstall *.rpm

4.首先进入desktop-integration目录下。

cd desktop-integration/

yum localinstall openoffice4.1.5-redhat-menus-4.1.5-9789.noarch.rpm

5.启动OpenOffice服务。

cd /opt/openoffice4/program

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

6.服务启动验证。

netstat  -nlp | grep 8100  // linux

netstat -ano | findstr "3306" // window

6.下载JodConverter。

com.artofsolving

jodconverter

2.2.1

7.Java端调用代码。

package com.xnck.demo;

import com.artofsolving.jodconverter.DocumentConverter;

import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;

import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;

import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.net.ConnectException;

public class OfficeToPDF {

/**

* 将Office文档转换为PDF. 运行该函数需要用到OpenOffice, OpenOffice下载地址为

* http://www.openoffice.org/

*

*

 
 

* 方法示例:

* String sourcePath = "F:\\office\\source.doc";

* String destFile = "F:\\pdf\\dest.pdf";

* Converter.office2PDF(sourcePath, destFile);

*

*

* @param sourceFile 源文件, 绝对路径. 可以是Office2003-2007全部格式的文档, Office2010的没测试. 包括.doc,

* .docx, .xls, .xlsx, .ppt, .pptx等. 示例: F:\\office\\source.doc

* @param destFile 目标文件. 绝对路径. 示例: F:\\pdf\\dest.pdf

* @return 操作成功与否的提示信息. 如果返回 -1, 表示找不到源文件, 或url.properties配置错误; 如果返回 0,

* 则表示操作成功; 返回1, 则表示转换失败

*/

public static int office2PDF(String sourceFile, String destFile) throws FileNotFoundException {

OpenOfficeConnection connection = new SocketOpenOfficeConnection("211.149.224.37", 8100);

try {

File inputFile = new File(sourceFile);

if (!inputFile.exists()) {

return -1;// 找不到源文件, 则返回-1

}

// 如果目标路径不存在, 则新建该路径

File outputFile = new File(destFile);

if (!outputFile.getParentFile().exists()) {

outputFile.getParentFile().mkdirs();

}

connection.connect();

DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);

converter.convert(inputFile, outputFile);

connection.disconnect();

return 0;

} catch (ConnectException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (connection != null) {

connection.disconnect();

connection = null;

}

} catch (Exception e) {

}

}

return 1;

}

public static void main(String[] args) {

try {

System.out.println(office2PDF("C:\\Users\\Gibbons\\Desktop\\合同模板.doc", "C:\\Users\\Gibbons\\Desktop\\合同模板.pdf"));

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

}

8.OpenOffice格式转换中文乱码解决方案。

1、将字体拷贝到linux系统下 /usr/share/fonts

2、更新缓存 fc-cache

3、kill掉openoffice进程   ps -ef | grep openoffice     kill -9 3045

4、重启后台运行openoffice  soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &

9.GitHub项目实例地址:https://github.com/Fillavacancy/ExportWord

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值