记录
记录java项目通过openoffice将word等office文档转换成pdf
maven依赖
<dependency>
<groupId>com.artofsolving</groupId>
<artifactId>jodconverter</artifactId>
<version>2.2.2</version>
</dependency>
保存文件工具类
/**
* 保存文件
*
* @param inputStream
* @param path 需要保存的路径
* @param fileName 保存的文件名
*/
public static void savePicWord(InputStream inputStream, String path, String fileName) {
OutputStream os = null;
try {
// 2、保存到临时文件
// 1K的数据缓冲
byte[] bs = new byte[1024];
// 读取到的数据长度
int len;
// 输出的文件流保存到本地文件
File tempFile = new File(path);
if (!tempFile.exists()) {
tempFile.mkdirs();
}
os = new FileOutputStream(path + File.separator + fileName);
// 开始读取
while ((len = inputStream.read(bs)) != -1) {
os.write(bs, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 完毕,关闭所有链接
try {
os.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
转化工具类工具类
public class DocConverter {
private static final int environment = 1;// 环境1:windows,2:linux(涉及pdf2swf路径问题)
private String srcFilePath;
private String outputPath = "";// 输入路径,如果不设置就输出在默认位置
private String srcFileName;
private File pdfFile;
private File docFile;
public DocConverter(String srcFilePath) {
ini(srcFilePath);
}
/*
* 重新设置 file @param srcFilePath
*/
public void setFile(String srcFilePath) {
ini(srcFilePath);
}
/*
* 初始化 @param srcFilePath 源文件路径
*/
private void ini(String srcFilePath) {
try {
this.srcFilePath= srcFilePath;
srcFileName= this.srcFilePath.substring(0, this.srcFilePath.lastIndexOf("."));
docFile = new File(this.srcFilePath);
pdfFile = new File(srcFileName+ ".pdf");//创建空文件
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 转为pdf格式
*
* @throws Exception
*/
private void doc2pdf() throws Exception {
if (docFile.exists()) {
/* if (!pdfFile.exists()) {*/
OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
try {
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(docFile, pdfFile);
// close the connection
connection.disconnect();
System.out.println("****pdf转换成功,PDF输出:" + pdfFile.getPath() + "****");
} catch (java.net.ConnectException e) {
// ToDo Auto-generated catch block
e.printStackTrace();
System.out.println("****swf转换异常,openoffice服务未启动!****");
throw e;
} catch (OpenOfficeException e) {
e.printStackTrace();
System.out.println("****pdf转换器异常,读取转换文件失败****");
throw e;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
/* } else {
System.out.println("****已经转换为pdf,不需要再进行转化****");
}*/
} else {
System.out.println("****pdf转换器异常,需要转换的文档不存在,无法转换****");
}
}
/**
* 把InputStream字节流 替换为BufferedReader字符流
*
* @param in
* @return
* @throws IOException
*/
static String loadStream(InputStream in) throws IOException {
int ptr = 0;
//把InputStream字节流 替换为BufferedReader字符流
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder buffer = new StringBuilder();
while ((ptr = reader.read()) != -1) {
buffer.append((char) ptr);
}
return buffer.toString();
}
/**
* 调用该方法执行pdf的转化
*
* @return
*/
public boolean conver() {
try {
if (environment == 1) {
System.out.println("****swf转换器开始工作,当前设置运行环境windows****");
} else {
System.out.println("****swf转换器开始工作,当前设置运行环境linux****");
}
doc2pdf();//将文件转换为pdf
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
*调试
*/
public static void main(String[] args) {
DocConverter d = new DocConverter("D:/text.doc");
d.conver();//调用方法,分别创建两个dpf和swf
}
}