利用openoffice与jobcover完成在线预览功能

整体思路:

利用openoffice转文件为pff格式的,在引入pom时,有两种类型的包,一种是org的,一种是com的,org与com相比,org可转换的文件后缀类型比com多

1.引入pom

<!--转pdf-->
<dependency>
    <groupId>org.artofsolving.jodconverter</groupId>
    <artifactId>jodconverter-core</artifactId>
    <version>3.0-beta-4</version>
</dependency>


  <dependency>

    <groupId>org.openoffice</groupId>

    <artifactId>jurt</artifactId>

    <version>3.2.1</version>

  </dependency>

  <dependency>

    <groupId>org.openoffice</groupId>

    <artifactId>ridl</artifactId>

    <version>3.2.1</version>

  </dependency>

  <dependency>

    <groupId>org.openoffice</groupId>

    <artifactId>juh</artifactId>

    <version>3.2.1</version>

  </dependency>

  <dependency>

    <groupId>org.openoffice</groupId>

    <artifactId>unoil</artifactId>

    <version>3.2.1</version>

  </dependency>

 

2.编写工具类

package com.topsec.tsm.util;

  

  

  

  import org.apache.commons.lang.StringUtils;

  import org.artofsolving.jodconverter.OfficeDocumentConverter;

  import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;

  import org.artofsolving.jodconverter.office.OfficeManager;

  import org.artofsolving.jodconverter.office.OfficeUtils;

  import org.artofsolving.jodconverter.util.PlatformUtils;

  import org.slf4j.Logger;

  import org.slf4j.LoggerFactory;

  

  import java.io.File;

  

  import java.util.regex.Pattern;

  

  /**

 *

 * 这是一个工具类,主要是为了使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx)

 * 转化为pdf文件<br>

 * created by zjx on 2018/6/19

 */

  public class Office2PDF {

  

    private static final Logger LOG = LoggerFactory.getLogger(Office2PDF.class);

  

    /**

     * 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件<br>

     *

     * @param inputFilePath

     *            源文件路径,如:"e:/test.docx"

     * @return

     */

    public static File openOfficeToPDF(String inputFilePath) {

        return office2pdf(inputFilePath);

    }

  

    /**

     * 根据操作系统的名称,获取OpenOffice的安装目录<br>

     *     根据系统修改其返回路径

     *

     * @return OpenOffice.org 3的安装目录

     */

    public static String getOfficeHome() {

        String osName = System.getProperty("os.name");

        System.out.println("操作系统名称:" + osName);

  

        String programFiles = System.getenv("ProgramFiles(x86)");

        if (programFiles == null) {

            programFiles = System.getenv("ProgramFiles");

        }

        File officeName= findOfficeHome(programFiles + File.separator + "OpenOffice.org 3", programFiles + File.separator + "LibreOffice 3");

        System.out.println("officeName:" + officeName);

        if (Pattern.matches("Linux.*", osName)) {

            return "/opt/openoffice.org3";

        } else if (Pattern.matches("Windows.*", osName)) {

            return "C:\\Program Files (x86)\\OpenOffice.org 3";

        } else if (Pattern.matches("Mac.*", osName)) {

            return "/Applications/OpenOffice.org.app/Contents/";

        }

        return null;

    }

    private static File findOfficeHome(String... knownPaths) {

        String[] arr$ = knownPaths;

        int len$ = knownPaths.length;

  

        for(int i$ = 0; i$ < len$; ++i$) {

            String path = arr$[i$];

            File home = new File(path);

            if (getOfficeExecutable(home).isFile()) {

                return home;

            }

        }

  

        return null;

    }

    public static File getOfficeExecutable(File officeHome) {

        return PlatformUtils.isMac() ? new File(officeHome, "MacOS/soffice.bin") : new File(officeHome, "program/soffice.bin");

    }

    /**

     * 连接OpenOffice.org 并且启动OpenOffice.org

     *

     * @return

     */

    public static OfficeManager getOfficeManager() {

        DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();

        // 设置OpenOffice.org 3的安装目录

        config.setOfficeHome(getOfficeHome());

        // 启动OpenOffice的服务

        OfficeManager officeManager = config.buildOfficeManager();

        officeManager.start();

        return officeManager;

    }

  

    /**

     * 转换文件

     *

     * @param inputFile

     * @param outputFilePath_end

     * @param inputFilePath

     * @param converter

     */

    public static File converterFile(File inputFile, String outputFilePath_end, String inputFilePath,

                                     OfficeDocumentConverter converter) {

        File outputFile = new File(outputFilePath_end);

        // 假如目标路径不存在,则新建该路径

        if (!outputFile.getParentFile().exists()) {

            outputFile.getParentFile().mkdirs();

        }

        converter.convert(inputFile, outputFile);

        System.out.println("文件:" + inputFilePath + "\n转换为\n目标文件:" + outputFile + "\n成功!");

        return outputFile;

    }

  

    /**

     * 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件<br>

     *

     * @param inputFilePath

     *            源文件路径,如:"e:/test.docx"

     * @param outputFilePath

     *            目标文件路径,如:"e:/test_docx.pdf"

     * @return

     */

    public static File office2pdf(String inputFilePath) {

        OfficeManager officeManager = null;

        try {

            if (StringUtils.isEmpty(inputFilePath)) {

                LOG.info("输入文件地址为空,转换终止!");

                return null;

            }

  

            File inputFile = new File(inputFilePath);

            // 转换后的文件路径

            String outputFilePath_end = getOutputFilePath(inputFilePath);

  

            if (!inputFile.exists()) {

                LOG.info("输入文件不存在,转换终止!");

                return null;

            }

  

            // 获取OpenOffice的安装路劲

            officeManager = getOfficeManager();

            // 连接OpenOffice

            OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);

  

            return converterFile(inputFile, outputFilePath_end, inputFilePath, converter);

        } catch (Exception e) {

            LOG.error("转化出错!", e);

        } finally {

            // 停止openOffice

            if (officeManager != null) {

                officeManager.stop();

            }

        }

        return null;

    }

  

    /**

     * 获取输出文件

     *

     * @param inputFilePath

     * @return

     */

    public static String getOutputFilePath(String inputFilePath) {

        String outputFilePath = inputFilePath.replaceAll("." + getPostfix(inputFilePath), ".pdf");

        return outputFilePath;

    }

  

    /**

     * 获取inputFilePath的后缀名,如:"e:/test.pptx"的后缀名为:"pptx"<br>

     *

     * @param inputFilePath

     * @return

     */

    public static String getPostfix(String inputFilePath) {

        return inputFilePath.substring(inputFilePath.lastIndexOf(".") + 1);

    }

  

    public static void main(String[] args) {

        Office2PDF.openOfficeToPDF("D:\\idea_workspace\\leaks\\tsap-web\\target\\ROOT\\WEB-INF\\classes\\wordFile\\网络安全风险日报\\2018年06月20日_网络安全风险日报_1529570355504.doc");

    }

  

}

 

3.安装openoffice

4.controller中去预览

//实现预览

  File file = Office2PDF2.openOfficeToPDF(apath.getAbsolutePath());

BufferedInputStream br = new BufferedInputStream(new FileInputStream(file));

  byte[] buf = new byte[1024];

  int len = 0;

response.reset(); // 非常重要

  response.setContentType("application/pdf");

response.setHeader("Content-Disposition",

        "inline; filename=" + java.net.URLEncoder.encode(file.getName(), "UTF-8"));

  

OutputStream out = response.getOutputStream();

  while ((len = br.read(buf)) != -1)

    out.write(buf, 0, len);

br.close();

out.close();

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值