一、安装office
1、http://www.openoffice.org/zh-cn/download/ 去官网链接下载linux版本的openOffice 以4.1.5 版本为例
2、将压缩包上传至服务器上,并进行解压安装(默认会安装在/opt目录下)
tar -zxvf 对应的压缩包名字
cd 进入解压后的 /zh-cn/RPMS
yum localinstall *.rpm
cd desktop-integration
rpm -ivh openoffice4.1.5-redhat-menus-4.1.5-9789.noarch.rpm
3、启动服务
/opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard 临时启动
nohup /opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard & 后台启动
二、进入贴代码阶段
首先引入jodconverter-2.2.2.jar包
/**
* 转换文件成pdf
* @param fromFileInputStream
* @param toFilePath 文件地址
* @param type 文件类型
* @return
* @throws IOException
*/
public static String file2pdf(InputStream fromFileInputStream, String toFilePath, String type) throws IOException {
log.info("进入PDF转换,toFilePath:{} ,type:{}", toFilePath, type);
String docFileName = null;
String htmFileName = null;
if ("doc".equals(type)) {
docFileName = "doc_" + DateUtil.dateNow("yyyyMMdd") + "_" + RandomUtil.uuId() + ".doc";
htmFileName = "doc_" + DateUtil.dateNow("yyyyMMdd") + "_" + RandomUtil.uuId() + ".pdf";
} else if ("docx".equals(type)) {
docFileName = "docx_" + DateUtil.dateNow("yyyyMMdd") + "_" + RandomUtil.uuId() + ".docx";
htmFileName = "docx_" + DateUtil.dateNow("yyyyMMdd") + "_" + RandomUtil.uuId() + ".pdf";
} else if ("xls".equals(type)) {
docFileName = "xls_" + DateUtil.dateNow("yyyyMMdd") + "_" + RandomUtil.uuId() + ".xls";
htmFileName = "xls_" + DateUtil.dateNow("yyyyMMdd") + "_" + RandomUtil.uuId() + ".pdf";
} else if ("xlsx".equals(type)) {
docFileName = "xlsx_" + DateUtil.dateNow("yyyyMMdd") + "_" + RandomUtil.uuId() + ".xlsx";
htmFileName = "xlsx_" + DateUtil.dateNow("yyyyMMdd") + "_" + RandomUtil.uuId() + ".pdf";
} else if ("ppt".equals(type)) {
docFileName = "ppt_" + DateUtil.dateNow("yyyyMMdd") + "_" + RandomUtil.uuId() + ".ppt";
htmFileName = "ppt_" + DateUtil.dateNow("yyyyMMdd") + "_" + RandomUtil.uuId() + ".pdf";
} else {
return null;
}
log.info("PDF存储路径 htmlOutputFile: {}", toFilePath + File.separatorChar + htmFileName);
File htmlOutputFile = new File(toFilePath + File.separatorChar + htmFileName);
log.info("doc等文件储存路径 docInputFile: {}", toFilePath + File.separatorChar + docFileName);
File docInputFile = new File(toFilePath + File.separatorChar + docFileName);
Boolean createHtmlOutputFile = htmlOutputFile.createNewFile();
log.info("创建PDF文件:createHtmlOutputFile:{}",createHtmlOutputFile);
Boolean createDocInputFile = docInputFile.createNewFile();
log.info("创建{}文件:createDocInputFile:{}",type ,createDocInputFile);
try {
OutputStream os = new FileOutputStream(docInputFile);
int bytesRead = 0;
byte[] buffer = new byte[1024 * 8];
while ((bytesRead = fromFileInputStream.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
fromFileInputStream.close();
} catch (IOException e) {
}
OpenOfficeConnection connection = new SocketOpenOfficeConnection(9016);
try {
connection.connect();
} catch (ConnectException e) {
log.error("异常:{}", e.getMessage());
log.error("文件转换出错,请检查OpenOffice服务是否启动。");
}
// convert
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
log.info("开始转换");
converter.convert(docInputFile, htmlOutputFile);
log.info("结束转换");
connection.disconnect();
// 转换完之后删除word文件
Boolean delDocInputFile = docInputFile.delete();
log.info("转换之后删除复制的{}文件:delDocInputFile:{}",type ,delDocInputFile);
return htmFileName;
}
public static void main(String[] args) throws IOException {
File file = null;
FileInputStream fileInputStream = null;
file = new File("/Users/xx/Desktop/CPC/template/zyrz_zpbg_template.docx");
fileInputStream = new FileInputStream(file);
String pdfFileName = ToHtmlPdfUtil.file2pdf(fileInputStream, "/Users/x x/Desktop/CPC/template/openoffice", "docx");
System.out.println(pdfFileName);
}
找不到jar包的私信我!
- 如果转换之后出现乱码:
将windows系统下的中文字体文件(C:\Windows\Fonts),放到/usr/share/fonts下,必须重启openoffice。