使用openoffice+pdf.j实现在线预览office文档

一、准备
1、openoffice安装程序(下载地址:http://www.openoffice.org/download/index.html)、pdf.js压缩包(下载地址:http://mozilla.github.io/pdf.js/getting_started/#download),项目所需jar包

https://blog.csdn.net/qq_39403734/article/details/82020309

我上传的jodconvert2.2.2压缩包还在审核(链接:—)

2、安装openoffice,直接解压即可,记住安装路径(启动服务时需要),我这里是默认安装路径。进入cmd后cd C:\Program Files (x86)\OpenOffice 4\program,输入启动指令(windows)soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard即可ddd在这里插入图片描述
3、解压pdf.js后是一个build和web文件夹,直接放入到项目中
在这里插入图片描述
4、文档转换工具类,我在网上找的将office文档(word、excel、ppt)转换成pdf(修改的只转换成pdf)

package tjun.sem.task.util;
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;

import java.io.*;

public class DocConverter {
    private static final int environment = 1;// 环境1:windows,2:linux(涉及pdf2swf路径问题)
    private String fileString;
    private String outputPath = "";// 输入路径,如果不设置就输出在默认位置
    private String fileName;
    private File pdfFile;
    private File docFile;

    public DocConverter(String fileString) {
        ini(fileString);
    }

    /*
     * 重新设置 file @param fileString
     */
    public void setFile(String fileString) {
        ini(fileString);
    }

    /*
     * 初始化 @param fileString
     */
    private void ini(String fileString) {
        this.fileString = fileString;
        fileName = fileString.substring(0, fileString.lastIndexOf("."));
        docFile = new File(fileString);
        pdfFile = new File(fileName + ".pdf");
    }

    /*
     * 转为PDF @param file
     */
    private void doc2pdf() throws Exception {
        if (docFile.exists()) {
            if (!pdfFile.exists()) {
                OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
                try {
                    connection.connect();
                    DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
                    converter.convert(docFile, pdfFile);
                    // close the connection
                    connection.disconnect();
                    System.out.println("****pdf转换成功,PDF输出:" + pdfFile.getPath() + "****");
                } catch (java.net.ConnectException e) {
                    // ToDo Auto-generated catch block
                    e.printStackTrace();
                    System.out.println("****pdf转换异常,openoffice服务未启动!****");
                    throw e;
                } catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {
                    e.printStackTrace();
                    System.out.println("****pdf转换器异常,读取转换文件失败****");
                    throw e;
                } catch (Exception e) {
                    e.printStackTrace();
                    throw e;
                }
            } else {
                System.out.println("****已经转换为pdf,不需要再进行转化****");
            }
        } else {
            System.out.println("****pdf转换器异常,需要转换的文档不存在,无法转换****");
        }
    }


    static String loadStream(InputStream in) throws IOException {
        int ptr = 0;
        //把InputStream字节流 替换为BufferedReader字符流 2013-07-17修改
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        StringBuilder buffer = new StringBuilder();
        while ((ptr = reader.read()) != -1) {
            buffer.append((char) ptr);
        }
        return buffer.toString();
    }

    /*
     * 转换主方法
     */
    public boolean conver() {
        if (pdfFile.exists()) {
            System.out.println("****pdf转换器开始工作,该文件已经转换为swf****");
            return true;
        }

        if (environment == 1) {
            System.out.println("****pdf转换器开始工作,当前设置运行环境windows****");
        } else {
            System.out.println("****pdf转换器开始工作,当前设置运行环境linux****");
        }

        try {
            doc2pdf();
        } catch (Exception e) {
            // TODO: Auto-generated catch block
            e.printStackTrace();
            return false;
        }

        if (pdfFile.exists()) {
            return true;
        } else {
            return false;
        }
    }

    /*
     * 返回文件路径 @param s
     */
    public String getpdfPath() {
        if (pdfFile.exists()) {
            String tempString = pdfFile.getPath();
            tempString = tempString.replaceAll("\\\\", "/");
            return tempString;
        } else {
            return "";
        }
    }

    /*
     * 设置输出路径
     */
    public void setOutputPath(String outputPath) {
        this.outputPath = outputPath;
        if (!outputPath.equals("")) {
            String realName = fileName.substring(fileName.lastIndexOf("/"), fileName.lastIndexOf("."));
            if (outputPath.charAt(outputPath.length()) == '/') {
                pdfFile = new File(outputPath + realName + ".pdf");
            } else {
                pdfFile = new File(outputPath + realName + ".pdf");
            }
        }
    }

    public static void main(String s[]) {
        DocConverter d = new DocConverter("D:\09.pdf");
        d.conver();
    }
}  

@Controller
(卡住了。晚安宝贝)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值