文档在线预览方案–OpenOffice+swftools

新逸网络 882 0

 

  文档在线预览的方案已经有很多了,本文仅记录个人的一些经验

文档在线预览方案主要有以下几种:

  1. Office Online
  2. 各大厂商提供的在线预览产品,一般是商业收费产品
  3. 云厂商提供的在线文档转换服务,转换为PDF后即可在线预览
  4. jacob方案,直接调用word转换为PDF格式,亲测可用,但需要安装word等软件
  5. OpenOffice开源方案,LibreOffice也可以,用于将文档转为PDF格式

1、OpenOffice的使用

OpenOffice和Microsoft的Office功能类似,常见于Linux平台。OpenOffice可以调用命令将文档转换为PDF等格式,LibreOffice同样可以,视情况选择其一即可。

http://www.openoffice.org/
https://www.libreoffice.org/

安装后,启用服务,开启8100端口

# 默认端口:8100
# -nofirststartwizard 指定非首次运行,如果是首次运行需要手动操作
# & 后台运行

~# /opt/openoffice4/program/soffice -headle -accept="socket,host=localhost,port=8100;urp;" -nofirststartwizard &

检测端口号:

~# netstat -ano | grep 8100

2、Java调用OpenOffice执行转换操作

  • 判断操作系统类型
private boolean isWindowsOs() {
    String os = System.getProperty("os.name").toLowerCase();
    if (os.indexOf("windows") != -1) {
        return true;
    }
    return false;
}
  • 检测端口是否打开
private boolean checkPort() {
    String[] winCmd = new String[] { "cmd", "/c", "netstat", "-ano|findstr", "8100" };
    String[] linCmd = new String[] { "netstat", "-ano|grep", "8100" };
    try {
        Process process = null;
        if (isWindowsOs()) {
            process = Runtime.getRuntime().exec(winCmd);
        } else {
            process = Runtime.getRuntime().exec(linCmd);
        }
        BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String lineText = "";
        while ((lineText = br.readLine()) != null) {
            if (lineText.contains("8100")) {
                return true;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}
  • 转换文档为pdf格式,需要引入 JODConverter jar包
OpenOfficeConnection conn = new SocketOpenOfficeConnection("127.0.0.1", 8100);
try {
    conn.connect();
    DocumentConverter converter = new OpenOfficeDocumentConverter(conn);
    converter.convert(srcFile, outputFile);
    logger.info("---> 文档转换为PDF格式成功。");
} catch (ConnectException e) {
    logger.error("---> 端口连接失败!\n" + e.getMessage());
} finally {
    if (conn != null) {
        if (conn.isConnected()) {
            conn.disconnect();
        }
        conn = null;
    }
}

3、转换pdf为swf格式

使用 swftools 工具可以将pdf转换为 swf 等格式,软件同时支持 windows 和 linux 格式。
http://www.swftools.org/download.html

linux下安装步骤:

~# yum install gcc* automake zlib-devel libjpeg-devel giflib-devel freetype-devel
~# cd swftools-0.9.3
~# ./configure --prefix=/usr/swftools
~# make
~# make install

~# vim /etc/profile
~# export PATH=$PATH:/usr/swftools/bin/

swftools转换命令

pdf2swf.exe -B "./swfs/rfxview.swf" d:/1.pdf -o d:/1.swf -f -T 9

4、文档在线预览


发表评论 取消回复
表情 图片 链接 代码

分享