OpenOffice文件转PDF 实测有用
OpenOffice办公文件预览功能
Hello, every one ,这篇博客主要讲解如何实现Office文件预览的功能,大多数浏览器都支持PDF的一个预览但是却不能够实现Office文件的预览,因此我们要将Office文件抓换为pdf或html来实现预览。这里采用两种方法来实现转化功能。第一种使用OpenOfice来实现预览,第二种是使用aspose-words来实现。
OpenOffice操作
自行下载OpenOffice进行安装,Windows和Linux都可以进行安装,Windows和Linux的我都试了一遍,在转化过程中Windows上的OpenOffice转化时间比Linux上的时间短很多而且还清晰,Linux上的OpenOffice连接就用了30秒。。。加了服务器别名还是十几秒。这点原因不是很明白,有懂得小伙伴可以沟通沟通。
1.jar这里采用jodconverter-2.2.2.jar 网上说是可以转换docx的比2.2.1多出很多转换格式,不过maven仓库没有需要,官网下载再放到自己本地仓库中进行引用。
jodconverter-2.2.2 下载地址 https://sourceforge.net/projects/jodconverter/files/latest/download
private static final String converterIP = "127.0.0.1";
private static final int port = 8100;
// 将word格式的文件转换为pdf格式
public static boolean WordToPDF(String startFile, String overFile) throws IOException {
// 源文件目录
File inputFile = new File(startFile);
if (!inputFile.exists()) {
System.out.println("源文件不存在!");
return false;
}
// 输出文件目录
File outputFile = new File(overFile);
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().exists();
}
Date begin = new Date();
// 连接openoffice服务IP converterIP 端口 port 不填IP默认是lcoalhost
OpenOfficeConnection connection = new SocketOpenOfficeConnection(converterIP,port);
connection.connect();
System.out.println("连接office所需时间:"+(System.currentTimeMillis() - begin.getTime()));
// 转换
Date begin2 = new Date();
DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
converter.convert(inputFile, outputFile);
System.out.println("转化word所需时间:"+(System.currentTimeMillis() - begin2.getTime()));
// 关闭连接
connection.disconnect();
return true;
}
我看别人文章好像有关闭进程的操作 不是很懂 我没用到
OpenOfiice转化核心代码应该就这点 同理Excel的文件也是可以转化的 这点我也测试过了。
aspose-words来实现预览
第二种方法是使用破解版的aspose-words来实现转化,我主要是用来转化word的,转化速度很快而且清晰度比OpenOffice要高.,最重要的一点不用安装openOffice服务,很是方便。
链接:https://pan.baidu.com/s/16t7rfLWTK0bHoR4-TWWy5A
提取码:v9d7
下载里面jar包然后放进自己的项目中,附加调用代码
public final static String url= "src/main/resources/license.xml";
/**
* 获取license,验证去水印
*E:\ftp\license.xml
* @return
*/
public static boolean getLicense() {
boolean result = false;
try {
File file = new File(url);
InputStream is = new FileInputStream(file);
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* word文件转pdf文件
*
* @param inPath 源文件路径
* @param outPath 新文件pdf路径
*/
public static boolean doc2pdf(String inPath, String outPath) {
if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return false;
}
FileOutputStream os = null;
try {
File file = new File(outPath); // 新建一个空白pdf文档
os = new FileOutputStream(file);
Document doc = new Document(inPath); // Address是将要被转化的word文档
doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
// EPUB, XPS, SWF 相互转换
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (os != null) {
try {
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}
/**
* 测试
*/
public static void main(String[] args) {
Util.doc2pdf("C:/Users/xxx.docx","C:/xxxx.pdf");
}
预览操作
这里是转化完之后将PDF转化成流然后输出到前台页面,使用的插件是PDF.JS直接官网下载后就可以了
将下载的文件放在src/resource/static下面中,然后创建页面测试预览效果就OK了
$('#yulan2').click(function() {
var fileUrl="c:/xxxx.docx";
var url="http://localhost:5858/View?fileUrl="+fileUrl;
//encodeURIComponent必须要加上 负责后台接受不到参数
window.open("http://localhost:5858/html/web/viewer.html?file="+encodeURIComponent(url));
});
第一次写博客,如果大家遇到什么问题可以跟我沟通哦 帮助到的小伙伴们希望可以鼓励鼓励我,给我点个赞哦 谢谢