问题:

目前遇到需求:将上传的文件生成一个缩略图,让用户能够直观的看到文件第一页,如Word、PPT、Excel、视频文件等。

  • 如果是视频文件可以使用ffmpeg进行截取视频开头的图片作为缩略图;
  • 如果是Word、PPT、Excel文件需要将文件转化为PDF再生成缩略图。

文件转PDF介绍:

  • yeokm1/docs-to-pdf-converter 不再维护,纯Java,开源
  • opensagres/xdocreport 纯Java,开放源代码
  • OpenOffice 需要安装OpenOffice结合Java使用,本文基于此介绍。

本文针对第三点进行说明。


使用OpenOffice进行文件的转化

1.准备工作:

  • OpenOffice官网下载

  • JODConverter官网下载

    或者为方便下载,我已将上面两个上传了百度云 百度云下载(提取码:ndiq)

2.介绍:

  • OpenOffice: OpenOffice是一套跨平台的办公室软件套件,功能非常强大,适用windows、linux、mac等各大平台,简单来说Office能做到的OpenOffice也基本都能做到。

  • JODConverter: 是一个Java的OpenDocument文件转换器,可以进行许多文件格式的转换。它依赖于OpenOffice.org或者LibreOffice提供的服务来进行转换,它能将Microsoft Office文档(Word,Excel,PowerPoint)转换为PDF格式。你可以将JODConverter内嵌在Java应用程序里,也可以单独作为命令行由脚本调用,更可以应用为网页程序或者Web Service以供网络应用。

3.启动OpenOffice服务

安装OpenOffice后:

//进入目录
cd C:\Program Files (x86)\OpenOffice 4\program
//启动OpenOffice服务
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
//查看是否启动成功
netstat -ano|findstr "8100"

4.代码

  • 4.1 解压下载的JODConverter并把lib目录下jar包导入到项目。
  • 4.2 转化代码如下:
    需要注意的是代码里的C:\Program Files (x86)\OpenOffice 4\program要根据自己的安装位置配置
package com.xiaohaitang.somedemo.utils;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.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;public class FileConversionUtil {//启动OpenOffice服务static {List<String> command = new ArrayList<>();//OpenOffice的安装目录下的soffice路径C:\Program Files (x86)\OpenOffice 4\program\soffice.execommand.add("C:\\Program Files (x86)\\OpenOffice 4\\program\\soffice.exe");command.add("-headless");command.add("-accept=\"socket,host=127.0.0.1,port=8100;urp;\" -nofirststartwizard");command.add("-nofirststartwizard");command.forEach(v -> System.out.print(v + " "));System.out.println();ProcessBuilder builder = new ProcessBuilder();//正常信息和错误信息合并输出builder.redirectErrorStream(true);builder.command(command);//开始执行命令try {Process process = builder.start();} catch (IOException e) {e.printStackTrace();}}/*** office文件转换成pdf或html文件*** @param file         要转换文件的路径,要包含名称和后缀(如:D:\GoogleDownload\中国地理复习笔记归纳总结(特细).doc)* @param saveFilePath 转换完后文件的保存路径(如:F:/test)* @param finalType    最终要转换为的文件的后缀传pdfh或html (如:pdf)** @return             返回最后转换后的文件名* @throws             IOException*/public static String conversionPdfOrHtml(String file, String saveFilePath, String finalType) throws IOException {FileInputStream inputStream = null;try {inputStream = new FileInputStream(file);} catch (FileNotFoundException e) {e.printStackTrace();}String type = file.substring(file.lastIndexOf(".") + 1);return conversionPdfOrHtml(inputStream, saveFilePath, type, finalType);}/*** office文件转换成pdf或html文件*** @param fromFileInputStream 要转换文件的文件流* @param saveFilePath        转换完后文件的保存路径(如:F:/test)* @param fileType            原始文件的后缀(如:doc)* @param finalType           最终要转换为的文件的后缀 (如:pdf)** @return                    返回最后转换后的文件名* @throws                    IOException*/public static String conversionPdfOrHtml(InputStream fromFileInputStream, String saveFilePath, String fileType, String finalType) throws IOException {Date date = new Date();SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");String timesuffix = sdf.format(date);String docFileName = null;String resultFileName = null;finalType = "." + finalType;//识别文件类型if ("doc".equals(fileType)) {docFileName = "doc_" + timesuffix + ".doc";resultFileName = "doc_" + timesuffix + finalType;} else if ("docx".equals(fileType)) {docFileName = "docx_" + timesuffix + ".docx";resultFileName = "docx_" + timesuffix + finalType;} else if ("xlsx".equals(fileType)) {docFileName = "xlsx_" + timesuffix + ".xlsx";resultFileName = "xlsx_" + timesuffix + finalType;} else if ("xls".equals(fileType)) {docFileName = "xls_" + timesuffix + ".xls";resultFileName = "xls_" + timesuffix + finalType;} else if ("ppt".equals(fileType)) {docFileName = "ppt_" + timesuffix + ".ppt";resultFileName = "ppt_" + timesuffix + finalType;} else if ("pptx".equals(fileType)) {docFileName = "pptx_" + timesuffix + ".pptx";resultFileName = "pptx_" + timesuffix + finalType;} else {return "转换错误,文件后缀不是doc、docx、xls、ppt、pptx";}//如果saveFilePath路径下有同名的原始文件和转化后的文件则删除。然后在saveFilePath下创建空的原始文件和转化后的文件File resultOutputFile = new File(saveFilePath + File.separatorChar + resultFileName);File docInputFile = new File(saveFilePath + File.separatorChar + docFileName);if (resultOutputFile.exists()) {resultOutputFile.delete();}if (docInputFile.exists()) {docInputFile.delete();}resultOutputFile.createNewFile();docInputFile.createNewFile();//将待转文件拷贝一份写入到saveFilePath下创建的空原始文件里try {OutputStream os = new FileOutputStream(docInputFile);int bytesRead = 0;byte[] buffer = new byte[1024 * 8];while ((bytesRead = fromFileInputStream.read(buffer)) != -1) {os.write(buffer, 0, bytesRead);}os.close();fromFileInputStream.close();} catch (IOException e) {}//连接OpenOffice服务。需提前开启OpenOffice服务,否则会报错。参考https://www.cnblogs.com/of-course/p/10064874.htmlOpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);try {connection.connect();} catch (Exception e) {System.out.println("连接OpenOffice服务失败,请检查是否启动OpenOffice服务");}// 转化,saveFilePath下的拷贝的原始文件转化为pdfSystem.out.println("转换中......");DocumentConverter converter = new OpenOfficeDocumentConverter(connection);converter.convert(docInputFile, resultOutputFile);connection.disconnect();//转换完之后删除拷贝的原始文件docInputFile.delete();return resultFileName;}
}
  • 测试转pdf
        String file = "D:\\GoogleDownload\\中国地理复习笔记归纳总结(特细).doc";String fileSavePath = "F:/test";try {String name = FileConversionUtil.conversionPdfOrHtml(file, fileSavePath, "pdf");System.out.println(name);} catch (IOException e) {e.printStackTrace();}
  • 结果:
  • 测试转html
         String file = "D:\\GoogleDownload\\中国地理复习笔记归纳总结(特细).doc";String fileSavePath = "F:/test";try {String name = FileConversionUtil.conversionPdfOrHtml(file, fileSavePath, "html");System.out.println(name);} catch (IOException e) {e.printStackTrace();}
  • 结果:

其他说明:

如果你是想在网页中预览 word、excel、ppt 等类型的文件,其实最推荐的是使用 kkFileView 来预览,安装使用可以参考我这篇文章 kkFileView安装及使用——文件预览解决方案

另外不推荐使用 openoffice把 word 转 html ,因为 word 里有许多格式和文字效果转换后html不能很好展示。
如果非要用 openoffice ,可以把 word 转换为 pdf ,然后就可以使用 PDF.js 等开源前端组件来预览 pdf 啦。


参考文章:
Java实现在线预览–openOffice实现
Springboot 使用 OpenOffice 实现附件在线预览功能

Java使用OpenOffice将office文件转换为PDF相关推荐

  1. office文件转换为pdf文件

    office文件转换为pdf文件 首先安装openoffice,自行百度 导入jar包,自行百度 引入坐标: <!-- 转pdf配置 --><dependency><gr ...

  2. python office转pdf_python 如何将office文件转换为PDF

    在平时的工作中,难免需要一些 小Tip 来解决工作中遇到的问题,今天的文章给大家安利一个方便快捷的小技巧,将 Office(doc/docx/ppt/pptx/xls/xlsx)文件批量或者单一文件转 ...

  3. python office转pdf linux_python 如何将office文件转换为PDF

    在平时的工作中,难免需要一些 小Tip 来解决工作中遇到的问题,今天的文章给大家安利一个方便快捷的小技巧,将 Office(doc/docx/ppt/pptx/xls/xlsx)文件批量或者单一文件转 ...

  4. 使用Java代码将word、execl、ppt文件转换为pdf格式

    office文件转换为pdf格式 使用OpenOffice转换 前言:通过第三方工具openoffice,将word.excel.ppt等文件转换为pdf文件支持在线 预 览:官网地址:http:// ...

  5. PHP实现office文件转PDF功能

            之前因为业务需要接触过在线浏览office文件,用过一些接口,例如:I DOC View(收费).Office Web 365(有免费版,详情可点击查看).online doc(有免费 ...

  6. 通过Jacob调用WPS将office文件转为PDF文件

    访问https://sourceforge.net/projects/jacob-project/ 想要调启Windows里的程序需要对应的dll库,下载之后解压 将符合你电脑的dll文件复制到jdk ...

  7. Java使用OpenOffice实现文件转换为PDF文件(三)

    项目背景:开发中需要实现word.ppt文件的在线预览,而浏览器无法打开此类文件,于是需要上传文件的时候转PDF文件存储. 下面是具体操作步骤: 一.OpenOffice是跨平台的免费软件套件,下载地 ...

  8. OpenOffice+JodConverter实现Office文件到PDF的转换

    文章目录 1. OpenOffice 下载.安装.启动 2. JodConverter下载 3. 文件转化 4. 中文乱码 5. 解决中文乱码 1. OpenOffice 下载.安装.启动 openo ...

  9. 使用java程序将ceb文件转换为pdf文件

    要使用 Java 程序将 CEB 文件转换为 PDF 文件,你可以使用 Apache POI 库来读取 CEB 文件并解析其内容. Apache POI 是一个开源的 Java 库,可以用于读取和写入 ...

  10. java pdf 转txt文件_java – 使用iText将TXT文件转换为PDF(保持格式化)

    我正在尝试使用iText库将.txt文件转换为.pdf文件. 我面临的问题如下: 我在txt文件中有一个清晰的格式,类似于: TEXT ******************* Other text h ...

最新文章

  1. python编程语言是什么-Python是什么?可能是最受欢迎的编程语言
  2. mysql分组取出每组地一条数据_MySQL 分组后取每组前N条数据
  3. 网管日志-06.09.26
  4. 机器学习 对模型进行惩罚_使用Streamlit对机器学习模型进行原型制作
  5. HDU 1244 DP
  6. android camera滑动,Android怎么实现小米相机底部滑动指示器
  7. java - 人员分配组合
  8. 手写数字识别 随机森林 代码
  9. 【手机开发岗位职责|手机开发是做什么的】-看准网
  10. 聊一聊FPGA的片内资源相关知识
  11. 爬虫笔记1-Requests库的基本方法
  12. tuple list 结构结合record的应用实例
  13. Xcode9之折叠代码
  14. 小程序软件有必要申请软件著作权登记么?
  15. 将您重定向的次数过多什么意思_【linux二三轶事】重定向是啥?文件描述符是啥?...
  16. win10系统D盘出现莫名其妙的占用
  17. 《番茄工作法图解》全书笔记
  18. 用JS实现PC端淘宝查看商品图片放大镜效果
  19. 计算机带给我们的改变英语作文,技术正改变我们的生活(Technology Is Changing Our Lives)...
  20. 计算机网络-自顶向下(学习笔记)

热门文章

  1. 华为 2020暑期实习 面试回忆
  2. 智能制造,从smart到intelligent
  3. 群晖DSM桌面无法删除快捷方式(无法右键)解决方法
  4. 目前1KB文件夹快捷方式病毒扫清方法
  5. 利用word2007插入参考文献
  6. 桌面显卡和CPU性能天梯图
  7. voip 网络电话快速搭建
  8. 响应式H5图片网盘外链系统源码 自适应PC手机端
  9. 汽车如何打蜡 汽车打蜡有什么要注意的地方
  10. 《鬼武者3》全攻略宝典