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. [TypeScript] Shallow copy object by using spread opreator

    For example we have an object: const todo = { text: "Water the flowers", completed: false, ...

  2. js40---享元模式

    /** * 享元模式是一个为了提高性能(空间复杂度)的设计模式 * 他使用与程序会生产大量的相类似的对象是耗用大量的内存的问题 */ (function(){ /** * 制造商 * 型号 * 拥有者 ...

  3. 企业部署Linux应用将拥有更低的TCO

    650) this.width=650;" onclick='window.open("http://blog.51cto.com/viewpic.php?refimg=" ...

  4. AJAX - 封装AJAX GET 数组join( )方法 键值对取value POST请求参数注意点

    function objToStr(obj){ obj.t = new Date().getTime(); // 给obj动态增加了一个属性 // 这个给对象添加属性的方法, 会被直接加到键值对里?? ...

  5. unity-unet-同步各个player唯一标识

    Multiplayer Game 中所有 player 都有一个唯一标识.在unet中可以通过 Network Identity 组件获取到该 player 在整个网络整的 唯一 的连接 id 这里测 ...

  6. MySQLSocketPHPvimApache

    原文:http://www.blogjava.net/asenyifei/articles/82575.html 自己装了一个MYSQL客户端,但却发现出现这样的错误: Can't connect t ...

  7. 解决Cookie乱码

    在Asp.net的HttpCookie中写入汉字,读取值为什么全是乱码?其实这是因 为文字编码而造成的,汉字是两个编码,所以才会搞出这么个乱码出来!其实解决的方法很简单:只要在写入Cookie时,先将 ...

  8. CSS3常用属性及用法

    1.transition: 过渡属性,可以替代flash和javascript的效果 兼容性:Internet Explorer 9 以及更早的版本,不支持 transition 属性. Chrome ...

  9. 《社交红利》读书总结--如何从微信微博QQ空间等社交网络带走海量用户、流量与收入

    <社交红利--如何从微信微博QQ空间等社交网络带走海量用户.流量与收入>--徐志斌 著 <社交红利>这本书2013年9月才上市,卖的非常火. 我最初是在公司内部的期刊上,看到有 ...

  10. shell基础之符号与语法

            shell脚本如今已经成为了一种非常普遍的脚本语言,之所以如此广泛的被应用,毋庸置疑它是有它的独到之处的.shell脚本语言和其它的语言比方说c/c++有何不同呢?c/c++等语言属于 ...