openoffice2pdf

前言:

   功能介绍
   要求:预览本地上传的文件,在浏览器中预览。

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


步骤: 

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

1.安装OpenOffice


windows安装

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

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



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

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

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

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


Linux安装

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

  (1).rpm包-在红帽LINUX、SUSE、Fedora可以直接进行安装,但在Ubuntu中却无法识别;
 
  (2).deb包-是Ubuntu的专利,在Ubuntu中双击deb包就可以进入自动安装进程;

2.安装

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

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

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


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

     如图:
   
    

  (2)安装

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

  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目录.


2.启动OpenOffice服务

1.windwos启动命令

  (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服务是否启动,如果没有启动成功,检查命令,重新执行。

       成功如图所示:

      
        
      

2.linux启动命令
      
       进入到 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服务是否启动,如果没有启动成功,检查命令,重新执行。

3.转换代码

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

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);
	}

}

注意:代码自己优化吧,这里只是网上扒找的例子;


      

猜你喜欢

转载自yu-duo.iteye.com/blog/2115425