Linux下OpenOffice转换Office为PDF的java实现

1. OpenOffice 下载 http://www.openoffice.org/
下载deb和rpms


2. 安装
   -->安装deb
一、卸载LibreOffice
  由于 Ubuntu 默认集成了 LibreOffice,在正式安装 OpenOffice 之前建议大家先将其卸载掉。要卸载 LibreOffice 可以使用如下命令:

  1.sudo apt-get remove --purge libreoffice*
  2.sudo apt-get clean
  3.sudo apt-get autoremove


  二、OpenOffice 32 位版本安装步骤
  1.先切换到tmp临时文件夹
   cd /tmp/
  2.下载OpenOffice 32位版安装程序
  3.解压安装程序并进行安装
  1.tar -xzvf Apache_OpenOffice_4.1.1_Linux_x86_install-deb_zh-CN.tar.gz
  2.cd zh-CN/DEBS

  3.sudo dpkg -i *.deb

  4.cd desktop-integration
  5.sudo dpkg -i *.deb


  三、OpenOffice 64 位版本安装步骤
  1.先切换到tmp临时文件夹
  1.cd /tmp/
  2.下载OpenOffice 32位版安装程序
  3.解压安装程序并进行安装
  1.tar -xzvf Apache_OpenOffice_4.1.1_Linux_x86-64_install-deb_zh-CN.tar.gz
  2.cd zh-CN/DEBS
  3.sudo dpkg -i *.deb
  4.cd desktop-integration
  5.sudo dpkg -i *.deb



    -->安装RPMS
1.使用tar -xzvf OOo_3.2.0_LinuxIntel_install_wJRE_en-US.tar.gz解压缩后,会得到OOO320_m12_native_packed-1_en-US.9483

目录。
2.进入OOO320_m12_native_packed-1_en-US.9483目录: cd OOO320_m12_native_packed-1_en-US.9483
3.进入到RPMS目录: cd RPMS
4.执行 rpm –ivh *rpm(安装所有rpm文件)
5.进入到desktop-integration目录 cd desktop-integration
6.执行 rpm -ivh openoffice.org3.2-redhat-menus-3.2-9472.noarch.rpm
这时openOffice己经安装完成,默认会安装在/opt下


注:rpm安装有报rtld(GNU_HASH) is needed by openoffice-core04-4.1.2-9782.i586错误,
采有强制安装rpm -ivh -f --nodeps *.rpm

3.启动openOffice服务

  1. 进入opt目录:cd /opt
  2. 进入openoffice.org3目录: cd openoffice.org3
  3. 进入program目录
  4. 执行 soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard & 
  5. 完成启动


4.java调用openOffice服务

1.下载JodConverter http://download.csdn.net/detail/kerryzb/9529876

2.java调用

package com.kerryzb.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.ConnectException;

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 OfficeToPDF {
	
	/** 
     * 将Office文档转换为PDF. 运行该函数需要用到OpenOffice, OpenOffice下载地址为 
     * http://www.openoffice.org/ 
     *  
     * <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 {  
        try {  
            File inputFile = new File(sourceFile);  
            if (!inputFile.exists()) {  
                return -1;// 找不到源文件, 则返回-1  
            }  
  
            // 如果目标路径不存在, 则新建该路径  
            File outputFile = new File(destFile);  
            if (!outputFile.getParentFile().exists()) {  
                outputFile.getParentFile().mkdirs();  
            }  
              
            // connect to an OpenOffice.org instance running on port 8100  
            OpenOfficeConnection connection = new SocketOpenOfficeConnection(  
                    "127.0.0.1", 8100);  
            connection.connect();  
  
            // convert  
            DocumentConverter converter = new OpenOfficeDocumentConverter(  
                    connection);  
            converter.convert(inputFile, outputFile);  
  
            // close the connection  
            connection.disconnect();  
  
            return 0;  
        } catch (ConnectException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
  
        return 1;  
    }  

}

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 12
    评论
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kerryzb

你鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值