使用openoffice转换ms_office to pdf
java源代码:
package com.jeecms.common.office2pdf; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties; import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager; /**
* 将Office文档转换为PDF文档
* @author BaiFL
*/
public class Office2PDF { /**
* 环境变量下面的url.properties的绝对路径
*/
private static final String URL_PATH = Thread.currentThread()
.getContextClassLoader().getResource("").getPath().replace("%20", " ")
+ "com/jeecms/common/office2pdf/url.properties"; /**
* 将Office文档转换为PDF
* @param sourceFile 源文件绝对路径
* 包括.doc, .docx, .xls, .xlsx, .ppt, .pptx, txt等
* @return result 目标文件绝对路径
*/
public synchronized static String office2PDF(String sourceFile) {
//返回值
String result = null;
try {
File inputFile = new File(sourceFile);
if (!inputFile.exists()) {
return null; //找不到源文件
} /**
* 目标文件为原文件路径及原文件名.pdf,如果目标路径不存在, 则新建该路径
* sourceFile:“source.doc”
* targetFile:“source.pdf”
*/
String targetFile = sourceFile.substring(0, sourceFile.lastIndexOf(".")) + ".pdf";
File outputFile = new File(targetFile);
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
} //从url.properties文件中读取OpenOffice的安装根目录
Properties prop = new Properties();
// 属性文件输入流
FileInputStream fis = new FileInputStream(URL_PATH);
// 将属性文件流装载到Properties对象中
prop.load(fis);
// 关闭流
fis.close(); //OpenOffice在windows/linux系统下的安装路径
String OPENOFFICE_HOME = prop.getProperty("OPENOFFICE_HOME"); if (OPENOFFICE_HOME == null){
return null; //找不到安装路径
} //检查安装路径是否以“/”结尾
if (OPENOFFICE_HOME.charAt(OPENOFFICE_HOME.length() - 1) != '/') {
OPENOFFICE_HOME += "/";
} DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
configuration.setOfficeHome(new File(OPENOFFICE_HOME)); // 设置OpenOffice.org安装目录
configuration.setPortNumbers(8100); // 设置转换端口,默认为8100
configuration.setTaskExecutionTimeout(1000 * 60 * 1L); // 设置任务执行超时为1分钟
configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L); // 设置任务队列超时为24小时 OfficeManager officeManager = configuration.buildOfficeManager();
//启动服务
officeManager.start();
OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
//执行转换
converter.convert(inputFile, outputFile);
if (officeManager != null) {
officeManager.stop();
}
result = targetFile;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
}
return result;
}
}
url.properties
#OpenOffice installation path in the windows/linux operating system #test in the windows change it for linux
#OPENOFFICE_HOME=C\:/Program Files (x86)/OpenOffice 4/
OPENOFFICE_HOME=/opt/openoffice4/
需要jar包:使用最新的 jodconverter-core-3.0-beta-4
使用openoffice转换ms_office to pdf的更多相关文章
- libreoffice转换文件为pdf文件乱码问题解决办法
最近系统需要一个office文件预览功能 解决方案为使用libreoffice将office文件转换为pdf文件,然后使用swftools将pdf文件转换为swf文件 最后在前台使用flexpaper ...
- openOffice转换的时候乱码在linux下使用openOffice的时候发现在转换后出现了乱码
openOffice转换的时候乱码 在linux下使用openOffice的时候发现在转换后出现了乱码,最后上网查了一下,按照网上的说法去试了试,最后也没有解决,也可能是我这边的linux的权限问题, ...
- 批量转换word为pdf
自己写的一个小工具,用于批量转换word为pdf,使用方式: 将完整代码拷贝到文档中,并修改名称为words2pdfs.py将该文件拷贝到需要转换的文档目录下在终端中输入python words2pd ...
- Linux系统下Java 转换Word到PDF时,结果文档内容乱码的解决方法
本文分享在Linux系统下,通过Java 程序代码将Word转为PDF文档时,结果文档内容出现乱码该如何解决.具体可参考如下内容: 1.问题出现的背景 在Windows系统中,使用Spire.Doc ...
- freemarker动态生成word并将生成的word转为PDF,openoffice转换word乱码
之前项目有个需求,需要先动态生成word内容,然后再预览生成word的内容(不能修改).整理一下,方便以后使用. 网上参考了好多大神的博客.具体也忘了参考谁的了,如有侵权,请告知修改. 思路一: 将目 ...
- openoffice转换pdf 异常问题查找处理 errorCode 525
could not save output document; OOo errorCode: 525 该问题是由于java程序和openoffice的启动所属用户不同导致.使用以下命令查看端口和进程 ...
- Java用OpenOffice将word转换为PDF
一. 软件安装以及jar包下载 官网的下载地址如下(英文): OpenOffice 下载地址http://www.openoffice.org/ JodConverter 下载地址http: ...
- 借助OpenOffice实现office转pdf(Java)的.exe小程序
原料:OpenOffice4.1.2(之所以选OpenOffice是因为可以跨平台,下载后直接安装),jodconverter-core-3.0-beta-4-dist.zip(可以搜博客园),Exe ...
- Rotativa 转换html 为pdf时遇到的问题
使用Rotativa,底层使用wkhtmltopdf 组件进行转换,使用过程中也遇到一些问题,记录下:首先,如果页面中有资源文件,需要使用的路径问题,必须使用全路径,http://xxxxx.其次,在 ...
随机推荐
- Onsctl 配置ONS服务(10G)
Onsctl Onsctl这个命令是用来管理ONS(Oracle Notification Service)是OracleClustser实现FAN Event Push模型的基础. 在RAC环境下. ...
- UVA - 10167 - Birthday Cake (简单枚举)
思路:简单枚举 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include &l ...
- HDOJ 5357 Easy Sequence DP
a[i] 表示以i字符开头的合法序列有多少个 b[i] 表示以i字符结尾的合法序列有多少个 up表示上一层的'('的相应位置 mt[i] i匹配的相应位置 c[i] 包括i字符的合法序列个数 c[i ...
- OpenStack_Swift源代码分析——ObjectReplicator源代码分析(1)
1.ObjectorReplicator的启动 首先执行启动脚本 swift-init object-replicator start 此执行脚本的执行过程和ring执行脚本执行过程差点儿相同.找到s ...
- android图片特效处理之怀旧效果
图片特效处理系列将介绍图片的像素点的特效处理,这些物资注重的是原理.也就是说只要你知道这些算法不管是C++,VB,C#,Java都可以做出相同的特效.下面将介绍图片怀旧效果的算法.算法如下: 上面公式 ...
- mysql 表设计时的update_time自动更新
11.3.5 Automatic Initialization and Updating for TIMESTAMP and DATETIME 原文地址:https://dev.mysql.com/d ...
- 设计模式六大原则(五):迪米特法则(Law Of Demeter)
定义: 一个对象应该对其他对象保持最少的了解. 问题由来: 类与类之间的关系越密切,耦合度越大,当一个类发生改变时,对另一个类的影响也越大. 解决方案: 尽量降低类与类之间的耦合. PS: 自从我们接 ...
- [TS] Swap two element in the array (mutation)
Shuffling is a common process used with randomizing the order for a deck of cards. The key property ...
- PHP: php_ldap.dll不能加载解决方案
PHP: php_ldap.dll不能加载解决方案 php.ini中开启 ldap的扩展后,重启服务:phpinfo();中没有ldap apache_error.log 提示:PHP Warning ...
- 早该知道的 7 个JavaScript 技巧[转]
简洁写法 对象的简写在过去,如果你想创建一个对象,你需要这样: var car = new Object(); car.colour = 'red'; car.wheels = 4; car.h ...