openoffice2pdf

[size=large][b]前言:[/b][/size]

[size=small][b]功能介绍[/b][/size]
[b]要求:[/b]预览本地上传的文件,在浏览器中预览。

[b]实现:[/b]1.使用Apache OpenOffice中转换工具将office转换为pdf;
2.将pdf中浏览模式设置为在浏览器中打开,即可实现此功能。
[b] 注意:[/b]改服务在linux中可用。


[size=large][b]步骤:[/b] [/size]

1.安装OpenOffice;
2.启动OpenOffice服;
3.写转换的代码;

[size=small][b]1.安装OpenOffice
[/b][/size]

[b]windows安装[/b]

1.在[url]http://www.openoffice.org/download/index.html[/url]下载最新版本openoffice;

选择系统和版本号,并且选择完整安装;

[img]http://dl2.iteye.com/upload/attachment/0101/0430/ae63331e-6b84-3a4f-a719-ec39e29be236.jpg[/img]

2.直接在windows系统下安装,并记住安装路径;

[b] 注:[/b]安装到默认路径下即可,一般为C:\Program Files (x86)\OpenOffice 4;

如果自定义路径,可能会找不到安装包的问题,可以删除后多安装几遍,实在解

决不了,就安装到默认路径下(我在安装时,就波折百出)。


[b]Linux安装[/b]

1.linux基础--deb,rpm文件

(1).rpm包-在红帽LINUX、SUSE、Fedora可以直接进行安装,但在Ubuntu中却无法识别;

(2).deb包-是Ubuntu的专利,在Ubuntu中双击deb包就可以进入自动安装进程;

2.安装

有了1的解释我们可以根据系统知道下载那个包了。

在[url]http://openoffice.apache.org/downloads.html[/url]地址中下载Linux安装包;

redhat32位包下载[url]http://dl.iteye.com/topics/download/8fc745ef-e99e-309b-be93-a437c38855ce[/url]


(1)我用的redhat所以下载.rpm格式的包。

如图:

[img]http://dl2.iteye.com/upload/attachment/0101/0440/4fb8755e-897a-34ec-b9f3-855d2162065a.jpg[/img]

(2)安装

完整步骤查看[url]http://www.openoffice.org/zhcn/download/common/instructions.html[/url]

1.使用下面的命令:tar -xvzf “linux包的名字”.tar.gz

"linux包的名字"应该替换你刚刚下载的tar.gz包的前一部分,也就是除了tar.gz以外的那

一串字符.这将创建一个安装目录。

安装目录的名称很可能会有语言名的缩写,例如,en-US.

su到 root 用户,如果有必要,浏览到Apache OpenOffice的安装目录(解压缩后的档案的位置).

您可能需要以root身份运行rpm命令来安装软件。

cd到安装目录下RPMS子目录.

你应该能看到许多rpm包和一个名为"desktop-integration"的子目录.

2.输入rpm -Uvih *rpm,安装新版本.

3.默认将会安装/升级Apache OpenOffice到/opt目录.


[size=small][b] 2.启动OpenOffice服务[/b][/size]

[b]1.windwos启动命令[/b]

(1)打开dos命令窗口

进入到 openoffice 安装目录

进入${openoffice}/program

执行:soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

其中8100为openoffice服务端口号;

执行:netstat -ano|findstr 8100

检查8100服务是否启动,如果没有启动成功,检查命令,重新执行。

成功如图所示:

[img]http://dl2.iteye.com/upload/attachment/0101/0446/337e829d-f8ec-31e4-91d5-977138e846fe.jpg[/img]

[img]http://dl2.iteye.com/upload/attachment/0101/0448/89972476-b490-392a-9707-8bb0c98beb47.jpg[/img]

[b]2.linux启动命令[/b]

进入到 openoffice 安装目录 | # cd /opt/openoffice4

进入${openoffice}/program | # cd /opt/openoffice4/program

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

其中8100为openoffice服务端口号;

执行:netstat -ano | grep 8100

检查8100服务是否启动,如果没有启动成功,检查命令,重新执行。

[size=large][b] 3.转换代码[/b][/size]

(网上很多资料,自己查,我也是拔下来的)


package com.test.util;

import java.io.File;

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.OpenOfficeDocumentConverter;


public class OfficeCoverPdfSwf {

public static int add(int a , int b){

return a+b;
}

/**
* 转为PDF
*
* @param file
*/
public static void doc2pdf(File originFilePath, File pfdFilePath) throws Exception {
if (originFilePath.exists()) {
if (!pfdFilePath.exists()) {
OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1",8100);
try {
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(originFilePath, pfdFilePath);
// close the connection
connection.disconnect();
System.out.println("****pdf转换成功,PDF输出:" + pfdFilePath.getPath()+ "****");
} catch (java.net.ConnectException e) {
e.printStackTrace();
System.out.println("****swf转换器异常,openoffice服务未启动!****");
throw e;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
} else {
System.out.println("****已经转换为pdf,不需要再进行转化****");
}
}
}

// static String loadStream(InputStream in) throws IOException {
//
// int ptr = 0;
// in = new BufferedInputStream(in);
// StringBuffer buffer = new StringBuffer();
//
// while ((ptr = in.read()) != -1) {
// buffer.append((char) ptr);
// }
//
// return buffer.toString();
// }
/**
* 转换主方法
*/
// @SuppressWarnings("unused")
// public boolean conver(File originalString, File pdfString) {
// PropertiesUtil propertiesUtil = new PropertiesUtil("config.properties");
// final int ENVIRONMENT = Integer.valueOf(propertiesUtil.readProperty("openoffice.environment"));
//
// if (ENVIRONMENT == 1) {
// System.out.println("****swf转换器开始工作,当前设置运行环境windows****");
// } else {
// System.out.println("****swf转换器开始工作,当前设置运行环境linux****");
// }
// try {
// doc2pdf(originalString,pdfString);
// } catch (Exception e) {
// e.printStackTrace();
// return false;
// }
// return true;
// }


/**
* 设置输出路径
*/

public static void main(String args[]) throws Exception{
File originFile = new File("D://Java//fileConverTest//original//aaa.docx");
File pdfFile = new File("D://Java//fileConverTest//swf//test2222.pdf");
OfficeCoverPdfSwf officeCoverPdfSwf = new OfficeCoverPdfSwf();
officeCoverPdfSwf.doc2pdf(originFile, pdfFile);
}

}

注意:代码自己优化吧,这里只是网上扒找的例子;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值