使用OpenOffice文件转换PDF格式实现文件预览
Centos7安装OpenOffice
下载安装包。
点击下载
上传至linux服务器中并解压
tar -zxvf Apache_OpenOffice_4.1.3_Linux_x86-64_langpack-rpm_zh-CN.tar.gz
zh-CN为解压后的文件目录
cd zh-CN/RPMS
安装rpm文件
yum localinstall *.rpm
cd desktop-integration目录
rpm -ivh openoffice4.1.3-redhat-menus-4.1.3-9783.noarch.rpm
启动服务
nohup /opt/openoffice4/program/soffice -headless -accept="socket,host=192.168.247.92,port=8100;urp;" -nofirststartwizard &
使用OpenOffice转换PDF格式
文件转换工具类(直接上代码)
import java.io.*;
import java.net.ConnectException;
import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.DocumentFormat;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
public class Doc2HtmlUtil {
private static String host = "192.168.247.92";
private static Integer port = 8100;
/**
* @param is
* @param os
* @param suffix 文件类型
* @param type 转换的类型:html or pdf
* @throws Exception
*/
public static void fileConversion(InputStream is, OutputStream os, String suffix, String type) throws Exception {
DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry();
// 文件格式
DocumentFormat format = formatReg.getFormatByFileExtension(type);
if ("doc".equals(suffix) || "docx".equals(suffix)) {
format = formatReg.getFormatByFileExtension("doc");
} else if ("xls".equals(suffix) || "xlsx".equals(suffix)) {
format = formatReg.getFormatByFileExtension("xls");
} else if ("ppt".equals(suffix) || "pptx".equals(suffix)) {
format = formatReg.getFormatByFileExtension("ppt");
}
DocumentFormat htmlFormat = formatReg.getFormatByFileExtension(type);
OpenOfficeConnection connection = new SocketOpenOfficeConnection(host, port);
try {
connection.connect();
DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
converter.convert(is, format, os, htmlFormat);
} catch (ConnectException e) {
throw new Exception("OpenOffice连接失败,请检查服务是否启动!");
} finally {
connection.disconnect();
}
}
public static void main(String[] args) throws Exception {
File file = new File("E:\\file\\test.ppt");
InputStream is = new FileInputStream(file);
OutputStream os = new FileOutputStream(new File("E:\\file\\test.pdf"));
Doc2HtmlUtil.fileConversion(is, os, "ppt", "pdf");
os.close();
is.close();
}
}
读取原文件流,转换为PDF文件流
1. 通过url从fastdfs中读取文件流
2. 校验文件格式(根据自己的业务)
3. 调用工具类转换方法
@Override
public byte[] readFile(FilesParam param) throws Exception {
String url = param.getUrl();
// 获取文件流
ByteArrayOutputStream out = new ByteArrayOutputStream();
FileRW.downloadAttachment(out, url);
byte[] bytes = out.toByteArray();
String fileSuffix = url.substring(url.indexOf(".") + 1);
if (checkType(fileSuffix)) {
InputStream is = new ByteArrayInputStream(bytes);
ByteArrayOutputStream os = new ByteArrayOutputStream();
Doc2HtmlUtil.fileConversion(is, os, fileSuffix, "pdf");
return os.toByteArray();
}
return bytes;
}