赞
踩
工作需求:提供一个接口,页面上的文档不仅支持下载,还可以在浏览器上可以预览的功能。我们知道pdf是支持浏览器在线预览的功能的,所以只要把其他格式的文档转换为pdf是不是就间接的展示了呢?
OpenOffice.org 是一套跨平台的办公室软件套件,能在Windows、Linux、MacOS X (X11)和 Solaris 等操作系统上执行。它与各个主要的办公室软件套件兼容。OpenOffice.org 是自由软件,任何人都可以免费下载、使用及推广它。
看看官网对它的介绍
下载windows版本的Apache_OpenOffice_4.1.10_Win_x86_install_zh-CN.exe为例,这个是32位的
官方下载地址:https://www.openoffice.org/download/
下载完成,双击进行安装,安装路径自己指定,我的安装到了c盘,执行文件安装了两个文件夹,我们主要用的是sdk,安装后如下图:
可以是open jre 、oracle jre
window命令
cd C:\Program Files (x86)\OpenOffice 4\program
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
不报错,为启动成功。
<!--jodconverter 核心包 --> <!-- https://mvnrepository.com/artifact/org.jodconverter/jodconverter-core --> <dependency> <groupId>org.jodconverter</groupId> <artifactId>jodconverter-core</artifactId> <version>4.2.2</version> </dependency> <!--springboot支持包,里面包括了自动配置类 --> <!-- https://mvnrepository.com/artifact/org.jodconverter/jodconverter-spring-boot-starter --> <dependency> <groupId>org.jodconverter</groupId> <artifactId>jodconverter-spring-boot-starter</artifactId> <version>4.2.2</version> </dependency> <!--jodconverter 本地支持包 --> <!-- https://mvnrepository.com/artifact/org.jodconverter/jodconverter-local --> <dependency> <groupId>org.jodconverter</groupId> <artifactId>jodconverter-local</artifactId> <version>4.2.2</version> </dependency>
jodconverter:
local:
enabled: true
office-home: C:\Program Files (x86)\OpenOffice 4 #安装路径
max-tasks-per-process: 10
port-numbers: 8100 #端口号
@Autowired private DocumentConverter converter; @GetMapping("testPreview") public void toPdfFile(HttpServletResponse response) { /*需要转换的文件*/ // word文件 File file = new File("f:\\test.doc"); // 图片 // File file = new File("f:\\铁山靠.png"); // excel文件 // File file = new File("f:\\test.xlsx"); try { // 转换之后文件生成的地址 File newFile = new File("D:/testMyDoc"); if (!newFile.exists()) { newFile.mkdirs(); } // pdf文件生成保存的路径 String savePath="D:/testMyDoc/"; String fileName="JCccc"+ UUID.randomUUID().toString().replaceAll("-","").substring(0,6); // pdf文件后缀 String fileType=".pdf"; // 将这三个拼接起来,就是我们最后生成文件保存的完整访问路径了 String newFileMix=savePath+fileName+fileType; // 文件转化 converter.convert(file).to(new File(newFileMix)).execute(); // 使用response,将pdf文件以流的方式发送的前端浏览器上 ServletOutputStream outputStream = response.getOutputStream(); // 读取文件 InputStream in = new FileInputStream(new File(newFileMix)); // copy流数据,i为字节数 int i = IOUtils.copy(in, outputStream); in.close(); outputStream.close(); System.out.println("流已关闭,可预览,该文件字节大小:"+i); } catch (Exception e) { e.printStackTrace(); } }
@GetMapping("testPreview") public void toPdfFile(HttpServletResponse response) { /*需要转换的文件*/ // pdf String pdfpath ="F:\\test.pdf"; try { // 使用response,将pdf文件以流的方式发送的前端浏览器上 ServletOutputStream outputStream = response.getOutputStream(); // 读取文件 InputStream in = new FileInputStream(new File(pdfpath )); // copy流数据,i为字节数 int i = IOUtils.copy(in, outputStream); in.close(); outputStream.close(); System.out.println("流已关闭,可预览,该文件字节大小:"+i); } catch (Exception e) { e.printStackTrace(); } }
集成比较快速,但不知道是否满足我们的需求,上面的是个比较简单的demo吧。官网上把它说的这么好, 如果你从事IT行业,OpenOffice 对你来说是一个很好的业务。其灵活的文本编辑器、强大的电子表格、绘图、数据库访问等等可以满足办公软件的所有需求。 对于我们更好的发掘它提供了动力。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。