实现PPT的在线预览(动态,及转PDF)

实现PPT的在线预览(动态,及转PDF)

公司的新需求,需要在网页上动态预览PPT,此处记录下,防止忘记。
之前在网上找了很多资料,比如:用POI 实现等,这里写下自己的实现方法

1.PPT 转PDF 实现在线预览

方法一、使用openoffice对PDF进行转换

1.准备工具
1.1 openoffice 4.1.7
windows及linux安装包
提取码: xaed
linux安装教程
1.2. PPT转PDF工具类

package com.dfjg.tiku.tikubnczhomeworkassignment.utils;
import org.jodconverter.OfficeDocumentConverter;
import org.jodconverter.office.DefaultOfficeManagerBuilder;
import org.jodconverter.office.OfficeException;
import org.jodconverter.office.OfficeManager;
import org.springframework.beans.factory.annotation.Value;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.regex.Pattern;


public final class Office2PDF {
//    private static final String PDFFilePath="D:/mongodbDownload/mongo_ppt/";
    private Office2PDF(){}

    /**
     * 将office格式的文件转为pdf
     * @param sourceFilePath 源文件路径
     * @return
     */
    public static File openOfficeToPDF(String sourceFilePath){
        return office2pdf(sourceFilePath);
    }

    /**
     * 将office文档转换为pdf文档
     * @param sourceFilePath 原文件路径
     * @return
     */
    public static File office2pdf(String sourceFilePath){
        OfficeManager officeManager = null;
        try{
            if(StringUtil.isEmpty(sourceFilePath))
            {
                //打印日志...
                return null;
            }
            File sourceFile = new File(sourceFilePath);
            if(!sourceFile.exists())
            {
                //打印日志...
                return null;
            }

            String after_convert_file_path = getAfterConverFilePath(sourceFilePath );
            //启动openOffice
            officeManager = getOfficeManager();
            OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
            return convertFile(sourceFile,after_convert_file_path,sourceFilePath,converter);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("转换异常");
        }finally {
            if(officeManager != null){
                try {
                    officeManager.stop();
                } catch (OfficeException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * 转换文件
     * @param sourceFile 原文件
     * @param after_convert_file_path 转换后存放位置
     * @param sourceFilePath 原文件路径
     * @param converter 转换器
     * @return
     */
    public static File convertFile(File sourceFile,
                                   String after_convert_file_path,String sourceFilePath,OfficeDocumentConverter converter) throws OfficeException {
        File outputFile = new File(after_convert_file_path);
        if(!outputFile.getParentFile().exists()){
            //如果上级目录不存在也就是E:/pdfFile这个文件夹不存在则创建一个
            outputFile.getParentFile().mkdirs();
        }
        converter.convert(sourceFile,outputFile);
        return outputFile;
    }

    public static OfficeManager getOfficeManager(){
        DefaultOfficeManagerBuilder builder = new DefaultOfficeManagerBuilder();
        builder.setOfficeHome(getOfficeHome());
        OfficeManager officeManager = builder.build();
        try {
            officeManager.start();
        } catch (OfficeException e) {
            //打印日志
            System.out.println("start openOffice Fail!");
            e.printStackTrace();
        }
        return officeManager;
    }

    /**
     * 获取转换后文件存放的路径
     * @param sourceFilePath 源文件
     * @return
     */
    public static String getAfterConverFilePath(String sourceFilePath){
        //截取源文件文件名
        String sourceFileName = sourceFilePath.substring(sourceFilePath.lastIndexOf("//") + 1);
        //转化文件名编码为ISO8859-1
//        try {
//            sourceFileName = new String(sourceFileName.getBytes("UTF-8"),"ISO8859-1");
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
        return  sourceFileName.replaceAll("\\."+FileUtil.getFileSuffix(sourceFileName),".pdf");
    }

    /**
     * 获取openOffice的安装目录
     * @return
     */
    public static String getOfficeHome(){
        String osName = System.getProperty("os.name");
        if(Pattern.matches("Windows.*",osName))
        {
            return "C:/Program Files (x86)/OpenOffice 4";
        }
        else if(Pattern.matches("Linux.*",osName))
        {
            return "/opt/openoffice4";
        }
        else if (Pattern.matches("Mac.*",osName))
        {
            return "/Application/openOfficeSoft";
        }
        return null;
    }
}

此工具类 可以启动openoffice,不需要手动进行启动

方法二、使用kkfileviewt开源工具

此方法是我在找动态预览PPT的过程中发现的
官方网站
安装包
我看了他的官方网站,发现他也是基于OpenOffice或LiberOffice,将ppt转化为PDF或者图片进行在线预览。所以并没有进行尝试。
在我看来方法二和方法一的区别就是,方法一需要自己书写工具类,方法二只需要下载压缩包,启动脚本即可。

2实现PPT动态预览

至于动态预览PPT,我这边找到的方法是使用officeonline

1.调用微软的官方接口

官方效果

https://view.officeapps.live.com/op/view.aspx?src=http://video.ch9.ms/build/2011/slides/TOOL-532T_Sutter.pptx

注意点:
1.此处src需要跟的是你的文件地址,需要在公网的环境下(我这边采用的是花生壳进行内网穿透)
2.因为是调用的微软接口,所以有时会特别不稳定(比如我,正好在给领导演示的过程中他半天打不开~~~)领导让我找找别的办法。

2.自己搭建officeonline服务器

基于上述原因,可能会考虑这个方面,希望在公司现有的条件下可以接触到。
另外听说前端控件也可以实现动态预览,但是自己不大清楚,也没有找到相关的资料。

在springcloud使用中遇到的一些问题

比如文件的下载位置
有道云笔记

不足之处,请多多指教。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值