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 ...
随机推荐
- 在mobaxten上使用scala报错
查看报错信息 [ERROR] Failed to construct terminal; falling back to unsupported java.io.IOException: Cannot ...
- rhce练习题容易错的地方
rhce练习题里容易错的地方 使用导航器的时候,ssh连接 因为导航器是一个工具,生成一个容器,在容器里面运行playbook 安装软件包的时候,多个软件包使用循环loop loop的格式 - hos ...
- 忘记root密码,破解root密码
破解root用户密码: 1.按e进入内核参数重置界面 2.找到开头Linux的段落,行尾输入rd.break 3.按ctrl+x 进入可选步骤 5.以读写方式挂载sysyroot 修改root密码要挂 ...
- python-一种去掉前后缀获取子串的方法
假设有一个字符串,其数据组成方式为:"mode_id1_str_id2",其中id1和id2为任意个数的数字,若存在mode,则id1必然也存在,否则都不存在:id2可有可没有. ...
- .net formwork WebApi 跨域问题
背景: ASP.NET Formwork Api / ASP.Net Core Api 做比较. 有关 Global.asax.FilterConfig.cs 和 RouteConfig.cs ...
- 自定义动画 jquery的结束动画
<button name="width">改变宽</button> <button name="height"> ...
- Nodejs静态资源管理 路由
express提供了一个非常好用的方法,叫做 express.static(),通过此方法,可以非常方便地创建一个静态web资源服务器 app.use(express.static('public') ...
- 支付宝APP支付 订单已付款成功,请勿重复提交 和 微信H5支付 INVALID_REQUEST 201 商户订单号重复
支付宝APP支付 返回请求给前端SDK 提示报错"订单已付款成功,请勿重复提交" 产生原因:存在商家订单号已经支付成功,重复再次请求的情况.每一笔的支付项目商家订单号是唯一的,如果 ...
- 使用selenium grid实现下发任务到远程机器,远程执行测试用例
背景: 1. UI自动化测试用例, 可能想要在不同版本.不同浏览器下执行 2. UI自动化测试用例较多的时候,耗时时间长,这个时候可以通过分散在不同的机器上执行,减少自动化测试时间 实现 通过sele ...
- 『vulnhub系列』Dripping-Blues-1
『vulnhub系列』Dripping-Blues-1 下载地址: https://www.vulnhub.com/entry/dripping-blues-1,744/ 信息搜集: 使用nmap进行 ...