您的位置:首页 > 运维架构 > Linux

CentOS 7 安装OpenOffice并实现WordToPDF(Java调用)

2018-06-08 17:25 549 查看
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。

<dependency>
<groupId>com.artofsolving</groupId>
<artifactId>jodconverter</artifactId>
<version>2.2.1</version>
</dependency>

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/ * <p>
* <pre>
* 方法示例:
* String sourcePath = "F:\\office\\source.doc";
* String destFile = "F:\\pdf\\dest.pdf";
* Converter.office2PDF(sourcePath, destFile);
* </pre>
*
* @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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: