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的更多相关文章

  1. libreoffice转换文件为pdf文件乱码问题解决办法

    最近系统需要一个office文件预览功能 解决方案为使用libreoffice将office文件转换为pdf文件,然后使用swftools将pdf文件转换为swf文件 最后在前台使用flexpaper ...

  2. openOffice转换的时候乱码在linux下使用openOffice的时候发现在转换后出现了乱码

    openOffice转换的时候乱码 在linux下使用openOffice的时候发现在转换后出现了乱码,最后上网查了一下,按照网上的说法去试了试,最后也没有解决,也可能是我这边的linux的权限问题, ...

  3. 批量转换word为pdf

    自己写的一个小工具,用于批量转换word为pdf,使用方式: 将完整代码拷贝到文档中,并修改名称为words2pdfs.py将该文件拷贝到需要转换的文档目录下在终端中输入python words2pd ...

  4. Linux系统下Java 转换Word到PDF时,结果文档内容乱码的解决方法

    本文分享在Linux系统下,通过Java 程序代码将Word转为PDF文档时,结果文档内容出现乱码该如何解决.具体可参考如下内容: 1.问题出现的背景 在Windows系统中,使用Spire.Doc ...

  5. freemarker动态生成word并将生成的word转为PDF,openoffice转换word乱码

    之前项目有个需求,需要先动态生成word内容,然后再预览生成word的内容(不能修改).整理一下,方便以后使用. 网上参考了好多大神的博客.具体也忘了参考谁的了,如有侵权,请告知修改. 思路一: 将目 ...

  6. openoffice转换pdf 异常问题查找处理 errorCode 525

    could not save output document; OOo errorCode: 525 该问题是由于java程序和openoffice的启动所属用户不同导致.使用以下命令查看端口和进程 ...

  7. Java用OpenOffice将word转换为PDF

    一.      软件安装以及jar包下载 官网的下载地址如下(英文): OpenOffice 下载地址http://www.openoffice.org/ JodConverter 下载地址http: ...

  8. 借助OpenOffice实现office转pdf(Java)的.exe小程序

    原料:OpenOffice4.1.2(之所以选OpenOffice是因为可以跨平台,下载后直接安装),jodconverter-core-3.0-beta-4-dist.zip(可以搜博客园),Exe ...

  9. Rotativa 转换html 为pdf时遇到的问题

    使用Rotativa,底层使用wkhtmltopdf 组件进行转换,使用过程中也遇到一些问题,记录下:首先,如果页面中有资源文件,需要使用的路径问题,必须使用全路径,http://xxxxx.其次,在 ...

随机推荐

  1. 洛谷——P1314 聪明的质监员

    https://www.luogu.org/problem/show?pid=1314 题目描述 小T 是一名质量监督员,最近负责检验一批矿产的质量.这批矿产共有 n 个矿石,从 1到n 逐一编号,每 ...

  2. Codeforces Round #312 (Div. 2) E. A Simple Task 线段树 延时标记

    E. A Simple Task time limit per test5 seconds memory limit per test512 megabytes inputstandard input ...

  3. ubuntu下sudo命令不再输入密码

    ubuntu下普通用户是没有root权限,很多命令在使用时都需要使用命令sudo 'cmd',但系统需要user验证自己,即需要输入普通用户的密码.但普通用户是否有执行该cmd的权限,需要到系统文件/ ...

  4. thinkphp5 left join

    thinkphp5 left join 一.总结 1.作用:left join就是即使不匹配也返回左表中的数据 2.join使用通式:object join ( mixed join [, mixed ...

  5. count 变量的使用

    count:统计出现的次数,当某种情况发生时,执行 +1 的动作,+1 的动作常置于循环体内.基本结构如下: count = 0 while count < vnum and **: ... c ...

  6. thinkphp动态注册路由

    thinkphp动态注册路由 一.总结 1.thinkphp使用路由步骤:a.config配置文件中开启路由  b.Route类的rule方法创建路由(在Routephp中)Route::rule(' ...

  7. loadrunner--设置代理录制

    为了解决loadrunner录制不到脚本或录制经常无响应等情况,可以选用代理录制方式,步骤如下. 打开配置好代理的浏览器,输入要录制的服务器ip,开始操作,录制完成后点击Shutdown

  8. Eclipse如何从导入SVN上导入项目

    1.右键单击,选择 Import,进入导入项目窗口 2.点击选择从SVN检出项目,点击Next下一步 3.选择创建新的资源库位置,点击Next,如果项目之前已经导入过删除掉了,重新导入的时候,只需勾选 ...

  9. 图像处理 Mine

    1)中值滤波:排序取中间值.作用:去噪点 1.1)均值滤波; 1.2)高斯模糊:执行高斯模糊,然后改混合模式,改成叠加.柔光或者深色.就能得到平滑而不模糊的效果. 2)腐蚀.膨胀:开运算(腐蚀后膨胀) ...

  10. Sending e-mail with Spring MVC--转载

    原文地址:http://www.codejava.net/frameworks/spring/sending-e-mail-with-spring-mvc Table of contents: 1.S ...