一、多余的话
java实现word转pdf可用的jar包不多,很多都是收费的。最近发现com.documents4j挺好用的,它支持在本机转换,也支持远程服务转换。但它依赖于微软的office。电脑需要安装office才能转换。鉴于没在linux中使用office,本文转换在windows中进行。
用途:主要是对word文件转换成pdf后,提供在线预览服务。也可以用于合同生成等。
支持文件格式:docx,doc,txt
二、前提条件
windows服务器或电脑需安装office软件。
三、代码实现
添加依赖:
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-local</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-transformer-msoffice-word</artifactId>
<version>1.1.6</version>
</dependency>
转换代码类:WordToPdfUtil.java
package com.lan.fts.util;
import com.documents4j.api.*;
import com.documents4j.job.LocalConverter;
import java.io.*;
import java.util.concurrent.Future;
public class WordToPdfUtil {
private IConverter getConverter(){
return LocalConverter.builder().build();
}
private void releaseConverter(IConverter converter){
converter.shutDown();
}
public boolean wordToPdf(String fromFilePath, String pdfFilePath){
boolean result = false;
File inputFile = new File(fromFilePath);
File outputFile = new File(pdfFilePath);
InputStream inputStream=null;
OutputStream outputStream = null;
IConverter converter = getConverter();
try {
inputStream = new FileInputStream(inputFile);
outputStream = new FileOutputStream(outputFile);
String wordFilePath_low=fromFilePath.toLowerCase();
if (wordFilePath_low.endsWith(".docx")) {
Future<Boolean> schedule = converter.convert(inputStream, true).as(DocumentType.DOCX).to(outputStream, true).as(DocumentType.PDF).schedule();
result = waitsShedule(schedule, 180000);
}else if(wordFilePath_low.endsWith(".doc")){
Future<Boolean> schedule = converter.convert(inputStream, true).as(DocumentType.DOC).to(outputStream, true).as(DocumentType.PDF).schedule();
result = waitsShedule(schedule, 180000);
}else if(wordFilePath_low.endsWith(".txt")){
Future<Boolean> schedule = converter.convert(inputStream, true).as(DocumentType.TEXT).to(outputStream, true).as(DocumentType.PDF).schedule();
result = waitsShedule(schedule, 180000);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
if(outputStream!=null)outputStream.close();
} catch (IOException e) {};
try {
if(inputStream!=null)inputStream.close();
} catch (IOException e) {};
releaseConverter(converter);
}
return result;
}
private boolean waitsShedule(Future<Boolean> schedule, int timeout){
int time=0;
while (!schedule.isDone()){
MyThread.sleep(500);
time+=500;
if(time>timeout){
schedule.cancel(true);
return false;
}
}
return true;
}
public static void main(String[] args) {
// new WordToPdfUtil().wordToPdf("D:\\data\\out\\ffec88b6ee26397bf99834acb059f7b0.docx", "D:\\data\\out\\ffec88b6ee26397bf99834acb059f7b0.docx.pdf");
}
}
说明:waitsShedule,是等待转换完成。如果超时,将取消转换任务
四、运行验证
public static void main(String[] args) {
new WordToPdfUtil().wordToPdf("D:\\data\\out\\lanhezhong文件转换.docx", "D:\\data\\out\\lanhezhong文件转换.docx.pdf");
}
运行结果:
***********************************************************************************************
author:蓝何忠
email:lanhezhong@163.com
***********************************************************************************************