java 使用openoffice将doc、docx、ppt、pptx等转换pdf格式文件
软件的安装与配置
链接: 官网 / C**N / 毒盘:提取码k47b
有能力的大佬可以选择c**n下载支持支持,官网下载可能有点慢
Windows下安装配置
下载安装包,安装到硬盘(这个就不截图了吧),记住你的安装路径,安装好后进入安装目录下的program文件夹(有soffice.exe文件的那个),运行以下命令开启服务
./soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;"
到这里就安装完成
Linux下安装配置
emmm…暂时没时间,所以没有测试等回头测试一次了再来发
项目配置
项目使用的是SpringBoot框架,下载jar文件C**N / 毒盘(提取码:23wu) (emmm…上传了再来加链接),解压出来,进入到项目目录下(有pom文件的目录),打开cmd窗口,依次运行
mvn install:install-file -Dfile=D:\jar文件目录,记得修改成自己的\jodconverter-core-3.0-beta-4.jar -DgroupId='org.artofsolving.jodconverter' -DartifactId=jodconverter-core -Dversion='3.0-beta-4' -Dpackaging=jar
mvn install:install-file -Dfile=D:\jar文件目录\jodconverter-2.2.2.jar -DgroupId='com.artofsolving' -DartifactId=jodconverter -Dversion='2.2.2' -Dpackaging=jar
引入pom依赖
<dependency>
<groupId>com.artofsolving</groupId>
<artifactId>jodconverter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>jurt</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>ridl</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>juh</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>unoil</artifactId>
<version>3.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.artofsolving.jodconverter/jodconverter-core -->
<dependency>
<groupId>org.artofsolving.jodconverter</groupId>
<artifactId>jodconverter-core</artifactId>
<version>3.0-beta-4</version>
</dependency>
代码实现
简单版
/**
* 文件转pdf
* @param inputFile 源文件
* @param OpenOfficeHost host地址 (127.0.0.1)
* @param OpenOfficePort port端口
* @return
*/
public static String file2PDF(File inputFile, String OpenOfficeHost , Integer OpenOfficePort) {
String outPath="";
try {
if (!inputFile.exists()) {
return outPath;
}
// 如果目标路径不存在, 则新建该路径
outPath=inputFile.toString().substring(0, inputFile.toString().indexOf("."))+".pdf";
File outputFile = new File(outPath);
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
//如果目标文件存在,则删除
if (outputFile.exists()) {
outputFile.delete();
}
OpenOfficeConnection connection = new SocketOpenOfficeConnection(OpenOfficeHost, OpenOfficePort);
connection.connect();
DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
converter.convert(inputFile, outputFile);
connection.disconnect();
return outPath;
} catch (Exception e) {
e.printStackTrace();
}
return outPath;
}
完整版
private static OfficeManager officeManager; private static boolean reconnect(Integer OpenOfficePort){
try {
ExternalOfficeManagerConfiguration externalProcessOfficeManager = new ExternalOfficeManagerConfiguration();
externalProcessOfficeManager.setConnectOnStart(true);
externalProcessOfficeManager.setPortNumber(OpenOfficePort);
officeManager = externalProcessOfficeManager.buildOfficeManager();
officeManager.start();
return true;
} catch (OfficeException e) {
e.printStackTrace();
return false;
}
}
// 开启新的openoffice的进程
private static void start(Integer OpenOfficePort,String OpenOfficeHome) {
try {
DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
configuration.setPortNumbers(OpenOfficePort);// 端口号
configuration.setOfficeHome(OpenOfficeHome);//安装位置
configuration.setTaskExecutionTimeout(1000 * 60 * 5);// 设置任务执行超时为5分钟
configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24);// 设置任务队列超时为24小时
officeManager = configuration.buildOfficeManager();
officeManager.start(); // 启动服务
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 文件转pdf
* @param input 输入文件
* @param OpenOfficePort 端口号
* @param OpenOfficeHome 安装地址
* @return
*/
public static File convertToPdf(String input,Integer OpenOfficePort,String OpenOfficeHome) {
File inputFile = null;
File outFile = null;
try {
// 如果已存在的服务不能连接或者不存在服务,那么开启新的服务
if(!reconnect(OpenOfficePort)){
start(OpenOfficePort , OpenOfficeHome);
}
String output = FilenameUtils.separatorsToSystem(FilenameUtils.getFullPath(input) + FilenameUtils.getBaseName(input) + ".pdf");
inputFile = new File(input);
outFile = new File(output);
OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
converter.convert(inputFile,outFile); // 转换文档
} catch (OfficeException o){
System.out.println("无法连接到外部办公流程!");
} catch (Exception e) {
outFile = null;
}finally{
stop();
}
return outFile;
}
// 关闭该进程
private static void stop() {
try {
if (officeManager != null)
officeManager.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
附一张转换后页面预览的图吧
java 使用openoffice将doc、docx、ppt、pptx等转换pdf格式文件的更多相关文章
- java中使用apache poi 读取 doc,docx,ppt,pptx,xls,xlsx,txt,csv格式的文件示例代码
java使用apache poi 读取 doc,docx,ppt,pptx,xls,xlsx,txt,csv格式的文件示例代码 1.maven依赖添加 在 pom 文件中添加如下依赖 <depe ...
- java读取txt/pdf/xls/xlsx/doc/docx/ppt/pptx
环境准备txt利用common-iopdf利用pdfbox剩下的用POI关于POI,读取xls没啥特别的,主要是读取doc和ppt,需要下载poi源代码,然后将poi-src-3.7-20101029 ...
- c/c++常用代码---doc,ppt,xls文件格式转PDF格式[转]
[转]doc,ppt,xls文件格式转PDF格式 http://blog.csdn.net/lee353086/article/details/7920355 确实好用. 需要注意的是#import文 ...
- Java使用Openoffice将word、ppt转换为PDF
最近项目中要实现WORD的文件预览功能,我们可以通过将WORD转换成PDF或者HTML,然后通过浏览器预览. OpenOffice OpenOffice.org 是一套跨平台的办公室软件套件,能在 W ...
- 前端实现docx、pdf格式文件在线预览
theme: vuepress highlight: atelier-heath-light 介绍 在业务中,如果遇到文档管理类的功能,会出现需要在线预览的业务需求,本文主要是通过第三方库来实现文档预 ...
- java 20 -2 递归之找特定目录下的特定格式文件
/* 需求:把C:\Users\Administrator\Desktop\记录目录下所有以.java结尾的文件的绝对路径输出到控制台 分析: A:封装该目录 B:获取该目录下的所有文件或文件夹的Fi ...
- Java通过openOffice实现word,excel,ppt转成pdf实现在线预览
Java通过openOffice实现word,excel,ppt转成pdf实现在线预览 一.OpenOffice 1.1 下载地址 1.2 JodConverter 1.3 新建实体类PDFDemo ...
- Java实现windows,linux服务器word,excel转为PDF;aspose-words,Documents4j
Java实现windows,linux服务器word,excel转为PDF:aspose-words,Documents4j 一.通过aspose-words将word,Excel文档转为PDF 1. ...
- asp.net将ppt文档转换成pdf
一.添加引用 using Microsoft.Office.Core;using Microsoft.Office.Interop.PowerPoint; 二.转换方法 C# 代码 复制 // ...
- office doc/xls/ppt 和 docx/xlsx/pptx 区别
经同事告诉,今天才真正明白两都区别: doc/xls/ppt 是office2007以前的扩展名: docx/xlsx/pptx 是office2007版本及以后的扩展名,是基于xml的文件格式,x ...
随机推荐
- itest work 开源接口测试&敏捷测试管理平台 9.5.0 GA_u3,优化及修复关键 BUG
(一)itest work 简介 itest work (爱测试) 一站式工作站让测试变得简单.敏捷,"好用.好看,好敏捷" ,是itest wrok 追求的目标.itest w ...
- Java中的包(Package)
# 包(Package) **为方便管理类(按照不同的功能管理类),解决同名问题的发生** - 使用`package关键字`修饰包 - **类名(全类名)=包名(地址)+类名简称** ```java ...
- 箭头函数 函数中的this指向
// 箭头函数 // 在匿名函数中,使用 => 箭头来替换 关键词 function // 箭头定义下 () 和 {} 之间 // 等于在使 ...
- WPS WORD EXCEL 不合并显示
WPS WORD EXCEL 不合并显示 版本:WPS 12 , 下载时间约是2023 年. 1.在开始菜单里找到 WPS OFFICE - 配置工具 2.点击"高级(A)". 3 ...
- EF INNER JOIN WHERE ORDER BY
同时使用 join,where,order by. UpdaterDbContext db = new UpdaterDbContext(); // 按 t_server 表的 seq ASC 排序, ...
- ftl生成模板并从前台下载
1.生成模板的工具类 package com.jesims.busfundcallnew.util; import freemarker.template.Configuration; import ...
- FreeRTOS简单内核实现5 阻塞延时
0.思考与回答 0.1.思考一 为什么 FreeRTOS简单内核实现3 任务管理 文章中实现的 RTOS 内核不能看起来并行运行呢? Task1 延时 100ms 之后执行 taskYIELD() 切 ...
- C++类与对象详解
什么是类和对象 类和对象的概念 类是对象的抽象,对象是对客观事物的抽象. 用通俗的话来说: 类是类别的意思,是数据类型. 对象是类别下的具体事物. 也就是说: 类是数据类型,对象是变量. 比如: 水果 ...
- Java常用的十大开源工具类库总结
以下是个人总结的Java常用的十大开源工具类库,根据具体需求和项目,可以选择合适的工具类库来提高开发效率.1. Apache Commons:Apache Commons是一个开源的工具类库,提供了大 ...
- 2019南京区域赛ABCHJK题解 & KM-bfs(O(n^3))板子
A.Hard Problem 题目大意:给你一个数n,然后让你计算一个子集大小,这个大小的子集要保证一定存在一个数是另一个数的约数,求出这个最小的数. 做法:显然后面的\(\frac{n}{2}\)个 ...