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. hdoj-1289-A Bug's Life【种类并查集】

    A Bug's Life Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...

  2. POJ 3461 Oulipo KMP算法题解

    本题就是给出非常多对字符串,然后问一个字符串在另外一个字符串出现的次数. 就是所谓的Strstr函数啦. Leetcode有这道差点儿一模一样的题目. 使用KMP算法加速.算法高手必会的算法了. 另外 ...

  3. android图像处理系列之七--图片涂鸦,水印-图片叠加

    图片涂鸦和水印其实是一个功能,实现的方式是一样的,就是一张大图片和一张小点图片叠加即可.前面在android图像处理系列之六--给图片添加边框(下)-图片叠加中也讲到了图片叠加,里面实现的原理是直接操 ...

  4. lightSlider 好图片轮播插件 支持移动端

    http://jquery-plugins.net/jquery-lightslider-lightweight-responsive-content-slider https://github.co ...

  5. 35.Intellij IDEA设置忽略部分类编译错误

    转自:https://www.aliyun.com/jiaocheng/290360.html 有些时候我们的项目中有些错误,但这些错误并不影响项目的整体运行(或许是没有使用到),默认情况下idea是 ...

  6. Android NineGridLayout — 仿微信朋友圈和QQ空间的九宫格图片展示自定义控件

    NineGridLayout 一个仿微信朋友圈和QQ空间的九宫格图片展示自定义控件. GitHub:https://github.com/HMY314/NineGridLayout 一.介绍 1.当只 ...

  7. erroe:plot.new() : figure margins too large

    使用R时多次出现这个错误,plot.new() : figure margins too large,提示图片边界太大 解决方法,win.graph(width=4.875, height=2.5,p ...

  8. Android时间戳与字符串相互转换

    import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public cl ...

  9. golang encoding/json

    package main import ( "bytes" "encoding/json" "fmt" ) type ColorGroup ...

  10. Request、Response 之 Http 请求

    今天说些什么呢? 说说Request吧! Request是什么: 请求(Request):一个从客户端到服务器的请求信息包括应用于资源的方法.资源的标识符和协议的版本号 request这个对象不用事先 ...