此项目根据企业真实需求制作而成,希望能帮助大家解决在线预览的问题!
此项目已开源,欢迎大家来STAR


软件 版本
SpringBoot 2.2.2.RELEASE
LibreOffice 6.3.2
unoconv 0.6

文章目录

  • 一、配置管理
    • ① pom
    • ② yml
    • ③ controller
    • ④ 文件格式转换工具类FileFormatConverToPDF
    • ⑤ 在线预览previewPDFUtils
    • ⑥ 启动类
  • 二、测试验证
    • ①测试链接
    • ②测试效果
  • 三、安装Unoconv
    • ①yum安装Unoconv
    • ②源码安装Unoconv
  • 四、安装LibreOffice

一、配置管理

① pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.gblfy</groupId><artifactId>business-online-preview</artifactId><version>0.0.1-SNAPSHOT</version><name>business-online-preview</name><url>https://gblfy.com</url><description>在线预览</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.0.0</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

② yml

③ controller

package com.gblfy.onlinepreview.controller;import com.gblfy.onlinepreview.utils.FileFormatConverToPDF;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletResponse;/*** @author gblfy* @ClassNme FileController* @Description 文件在在线预览* @Date 2020/01/08 8:09* @version1.0*/
@RestController
public class FileOnlinePreviewController {/*** 在线预览测试方法* 企业真实需求:* 文件的路径 文件名 都需要动态获取** @param response http响应网页来实现在线预览* @throws Exception*/@RequestMapping("/viewPDF")public void reviewWord(HttpServletResponse response) throws Exception {FileFormatConverToPDF linuxPageDIsplsyFileUtil = new FileFormatConverToPDF();//文件存储路径String fileStoragePath = "/app/ftpFileDir/testFileDir/businessLearning/";//转换前的文件名String beforeConversion = "知识库建设方案2019-11-11.docx";/*** 文件格式转换+在线预览*/linuxPageDIsplsyFileUtil.conversionFile(response, fileStoragePath, beforeConversion);}
}

④ 文件格式转换工具类FileFormatConverToPDF

package com.gblfy.onlinepreview.utils;import lombok.extern.slf4j.Slf4j;
import org.apache.poi.util.IOUtils;import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;/*** @Author:* @Date: 2019/1/15 0015 15:04* @describe 文档在线预览  (服务器环境为Linux环境) 目前文档类型 仅开放 Excel 03/07 word 03/07 ppt 03/07*/
@Slf4j
public class FileFormatConverToPDF {//libreoffice 文件格式转换shell命令public static final String LIBREOFFICE_SHELLCMD = "/usr/bin/soffice --headless --invisible --convert-to pdf ";//unoconv 文件格式转换shell命令public static final String UNOCONV_SHELLCMD = "/usr/bin/unoconv -f pdf ";//previewfile_dir 需要预览的pdf目录public static final String PREVIEWFILE_DIR = "/root/";private static FileFormatConverToPDF linuxPageDIsplsyFileUtil;public static synchronized FileFormatConverToPDF getSwitchUtil() {if (linuxPageDIsplsyFileUtil == null) {linuxPageDIsplsyFileUtil = new FileFormatConverToPDF();}return linuxPageDIsplsyFileUtil;}/*** 文档在线预览** @param response* @param fileStoragePath  文件存储路径 (前段获取文件存储路径返给后台)* @param beforeConversion 文件名(必须带文件后缀名,这里指的就是文件全名称)* @throws Exception*/public void conversionFile(HttpServletResponse response, String fileStoragePath, String beforeConversion) throws Exception {String fileNamePath = fileStoragePath + beforeConversion;log.info("文件路径====" + fileNamePath);File file = new File(fileNamePath);if (!file.exists()) {log.info("库存中没有指定文件。。。。");return;}//获取到文件名String interceptFileName = beforeConversion.substring(0, beforeConversion.lastIndexOf("."));//截取文件后缀名String fileNameSuffix = beforeConversion.substring(beforeConversion.lastIndexOf(".") + 1);String command = null;System.out.println("获取到文件名====" + interceptFileName);System.out.println("截取文件后缀名====" + fileNameSuffix);if ("doc".equals(fileNameSuffix)|| "docx".equals(fileNameSuffix)|| "xls".equals(fileNameSuffix)|| "xlsx".equals(fileNameSuffix)|| "ppt".equals(fileNameSuffix)|| "pptx".equals(fileNameSuffix)) {System.out.println("此文件属于" + fileNameSuffix + "开始进行转换");command = LIBREOFFICE_SHELLCMD + fileNamePath;executeLinuxCmd(command);} else {command = UNOCONV_SHELLCMD + fileNamePath;executeCommand(command);}System.out.println("openPDF的参数====" + fileStoragePath + interceptFileName);previewPDFUtils.openPdf(response, PREVIEWFILE_DIR + interceptFileName + ".pdf");}/*** 使用LibreOffice进行格式转换 to pdf** @param cmd* @return* @throws IOException*/public static List<String> executeLinuxCmd(String cmd) throws IOException {log.info("执行文件转换的命令:" + cmd);Runtime run = Runtime.getRuntime();Process process = run.exec(new String[]{"/bin/sh", "-c", cmd});InputStream in = process.getInputStream();BufferedReader bs = new BufferedReader(new InputStreamReader(in));List<String> list = new ArrayList<String>();String result = null;while ((result = bs.readLine()) != null) {log.info("job result [" + result + "]");list.add(result);}in.close();process.destroy();return list;}/*** 使用Unoconv进行格式转换 to pdf** @param command* @throws Exception*/private static void executeCommand(String command) throws Exception {log.info("执行文件转换的命令:" + command);StringBuffer output = new StringBuffer();Process process;InputStreamReader inputStreamReader = null;BufferedReader reader = null;try {process = Runtime.getRuntime().exec(command);process.waitFor();inputStreamReader = new InputStreamReader(process.getInputStream(), "UTF-8");reader = new BufferedReader(inputStreamReader);String line = "";while ((line = reader.readLine()) != null) {output.append(line + "\n");}//p.destroy();//这个一般不需要} catch (Exception e) {e.printStackTrace();} finally {IOUtils.closeQuietly(reader);IOUtils.closeQuietly(inputStreamReader);}}
}

⑤ 在线预览previewPDFUtils

package com.gblfy.onlinepreview.utils;import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;/*** @author gblfy* @ClassNme previewPDF* @Description TODO* @Date 2020/1/8 12:47* @version1.0*/
public class previewPDFUtils {/*** 在线预览pdf文件** @param response* @param previewFile 预览pdf文件的绝对路径* @throws Exception*/public static void openPdf(HttpServletResponse response, String previewFile) throws Exception {InputStream inputStream = null;OutputStream outputStream = null;System.out.println("进入openPDF=====" + previewFile);//String path ="/home/tubiao/桌面/优化数据库.pdf";inputStream = new FileInputStream(previewFile);response.setContentType("application/pdf");outputStream = response.getOutputStream();int a = 0;byte[] b = new byte[1024];while ((a = inputStream.read(b)) != -1) {outputStream.write(b, 0, a);}if (outputStream != null) {outputStream.close();}if (inputStream != null) {inputStream.close();}}
}

⑥ 启动类

package com.gblfy.onlinepreview;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** 在线预览统一入口*/
@SpringBootApplication
public class OnlinePreviewApplication {public static void main(String[] args) {SpringApplication.run(OnlinePreviewApplication.class, args);System.out.println("启动成功!!!");}
}

二、测试验证

①测试链接

浏览器测试链接:http://localhost:8888/viewPDF

②测试效果


三、安装Unoconv

①yum安装Unoconv

unoconv 在线预览 doc,doxc,xls,xlsx,ppt,pptx 文件功能环境搭建
https://gblfy.blog.csdn.net/article/details/102847276

②源码安装Unoconv

(企业内部)Linux环境_源码安装Unoconv实现文件在线预览doc,doxc,xls,xlsx,ppt,pptx 文件
https://gblfy.blog.csdn.net/article/details/103540694

四、安装LibreOffice

(企业内部)Linux环境_源码安装LibreOffice实现文件在线预览doc,doxc,xls,xlsx,ppt,pptx 文件
https://gblfy.blog.csdn.net/article/details/103861905

友情链接:
(企业内部)SpringBoot 使用LibreOffice 在线预览 doc,doxc,xls,xlsx,ppt,pptx 文件
https://gblfy.blog.csdn.net/article/details/103861607

使用Unoconv和LibreOffice进行格式转换实现在线预览 doc,doxc,xls,xlsx,ppt,pptx 文件相关推荐

  1. SpringBoot 使用LibreOffice 在线预览 doc,doxc,xls,xlsx,ppt,pptx 文件

    接上一篇:linux环境源码安装unoconv Linux环境_源码安装Unoconv实现文件在线预览doc,doxc,xls,xlsx,ppt,pptx 文件 https://gblfy.blog. ...

  2. SpringBoot 使用unoconv 在线预览 doc,doxc,xls,xlsx,ppt,pptx 文件

    接上一篇:linux环境源码安装unoconv Linux环境_源码安装Unoconv实现文件在线预览doc,doxc,xls,xlsx,ppt,pptx 文件 https://gblfy.blog. ...

  3. Linux环境_源码安装Unoconv实现文件在线预览doc,doxc,xls,xlsx,ppt,pptx 文件

    因业务需求需要,用unoconv就可以轻松地实现利用LibOffice可以打开的文档的转换. 服务器版本 环境 系统版本 Linux Red Hat Enterprise Linux Server r ...

  4. unoconv 在线预览 doc,doxc,xls,xlsx,ppt,pptx 文件功能环境搭建

    接上一篇: SpringBoot 在线预览 doc,doxc,xls,xlsx,ppt,pptx 文件 https://blog.csdn.net/weixin_40816738/article/de ...

  5. 文档转换、在线预览的几种方式以及推荐

    互联网时代的繁荣期,在线教育犹如三国中的巴蜀之地,无论是各大巨头还是中小软件.都希望在此领域分的一口肥肉. 其中,关于文档转换.一直是开发在线教育软件让人头痛的事. 在这先说一下文档转换以及预览的几种 ...

  6. 文件在线预览doc,docx转换pdf(一)

    文件在线预览doc,docx转换pdf(一) 前言 文档转换是一个是一块硬骨头,但是也是必不可少的,我们正好做的知识库产品中,也面临着同样的问题,文档转换,精准的全文搜索,知识的转换率,是知识库产品的 ...

  7. [Asp.net]使用flexpaper+swftools大文件分页转换实现在线预览

    引言 之前总结了在线预览几种常见解决方案,可以戳这里: http://www.cnblogs.com/wolf-sun/p/3569960.html http://www.cnblogs.com/wo ...

  8. c# asp.net Pdf 转换图片 在线预览 发布到iis中问题 最终解决篇—_—!

    关于:excel和word 预览 请看我的博文: excel和word 在线预览  详细配置及代码 使用Adobe 组件 在本机vs中调试成功 发布到iis中  在  代码中涉及到  剪贴板的地方  ...

  9. Linux环境_源码安装LibreOffice实现文件在线预览doc,doxc,xls,xlsx,ppt,pptx 文件

    因业务需求需要,利用LibOffice可以打开的文档的转换. 服务器版本 环境 系统版本 Linux Red Hat Enterprise Linux Server release 7.6 (Maip ...

最新文章

  1. SAP Hybris电子商务最新功能
  2. C++骑士走棋盘Knight tour算法(附完整源码)
  3. Halcon和Opencv的区别?
  4. java类内部的变量
  5. 代码审计_md5()函数
  6. Kafka2.12安装与配置/生产与消费
  7. [导入]新手入门 Fedora Linux 7系统的安装指南
  8. java文件字节流和文件字符流的使用
  9. 汽车汽配行业DMS渠道商系统精准掌握渠道库存,提升市场响应能力
  10. mysql有rollup函数吗_MySQL-with rollup函数运用
  11. DHTMLX-Grid
  12. Bootstrap broker localhost:9092 (id: -1 rack: null) disconnected
  13. 史上最详细清样/校样(Proof)处理流程--Hindawi(二)
  14. hao123站长李兴平的成功史
  15. Linux学习笔记---阿里云
  16. 【人物专访】从12K到20+K,中间只差一个来学吧
  17. 入门c语言。(1建立开发环境)
  18. appstore上架助手
  19. Android4.4之后休眠状态下Alarm不准时的问题
  20. 音乐解锁工具v1.10.3,音乐格式转换,ncm转mp3,kgm转mp3,kgma转mp3,mgg转mp3,mflac转mp3,qmc转mp3,xm转mp3,kwm转mp3

热门文章

  1. 爱因斯坦和高中几何问题
  2. 第一次失效_特斯拉螺栓腐蚀失效分析_搜狐汽车
  3. NS2仿真分析无线网络的攻击防御(2)
  4. Libevent源码分析
  5. 「技术人生」第3篇:解决问题的规律总结
  6. 1024,阿里云惊喜 “加油包” 让你 “猿” 力觉醒!
  7. MaxCompute客户端在windows命令行下查询中文乱码怎么办?
  8. 流量隔离方案 Dpath 护航双十一新零售
  9. Michael Jordan:当下的AI其实都是伪“AI” 1
  10. Python API快餐教程(1) - 字符串查找API