调研了很多java可实现的转换方式,最终选择了大A开源的项目
这里环境使用的是windows ,linux差不多只是安装过后启动命令不同
1.先去安装openOffice
http://www.openoffice.org/download/index.html
直接下载
然后傻瓜式安装全部选择下一步
最后启动起来,我这边是远程服务器上安装的
启动命令:
soffice -headless -accept="socket,host=0.0.0.0,port=8100;urp;" -nofirststartwizard
//顺便提供下linux命令
nohup /opt/openoffice4/program/soffice -headless -accept="socket,host=0.0.0.0,port=8100;urp;" -nofirststartwizard &
//这里主要下远程使用0.0.0.0 本机使用可以直接127.0.0.1
编译环境:spring boot 2.1.0.RELEASE
com.artofsolving
jodconverter
2.2.1
只用引入这个即可
下面是转换工具类:
import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.DocumentFormatRegistry;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
import org.apache.commons.lang3.StringUtils;
import java.io.*;
import java.net.ConnectException;
/**
*
* @author zf
* @desc openoffice转换工具
*/
public class OpenOfficeUtils {
public static final String LOCAL_HOST = "localhost";
public static final int LOCAL_PORT = 8100;
// Format
public static DocumentFormatRegistry formatFactory = new DefaultDocumentFormatRegistry();
/**
*
* @param inputStream
* @param inputFileExtension 示例:pdf
* @param outputStream
* @param outputFileExtension 示例:pdf
* @param connectIp 远程调用ip
* @param connectPort 远程调用端口
*/
public static void convert(InputStream inputStream, String inputFileExtension, OutputStream outputStream,
String outputFileExtension, String connectIp, int connectPort) throws ConnectException {
if (inputStream == null || StringUtils.isEmpty(inputFileExtension) || outputStream == null
|| StringUtils.isEmpty(outputFileExtension) || StringUtils.isEmpty(connectIp)) {
throw new IllegalArgumentException("参数异常!!");
}
OpenOfficeConnection connection = new SocketOpenOfficeConnection(connectIp, connectPort);
connection.connect();
DocumentConverter converter = getConverter(connectIp, connection);
converter.convert(inputStream, formatFactory.getFormatByFileExtension(inputFileExtension), outputStream,
formatFactory.getFormatByFileExtension(outputFileExtension));
connection.disconnect();
}
private static DocumentConverter getConverter(String connectIp, OpenOfficeConnection connection) {
DocumentConverter converter = "localhost".equals(connectIp) || "127.0.0.1".equals(connectIp)
|| "0:0:0:0:0:0:0:1".equals(connectIp) ? new OpenOfficeDocumentConverter(connection)
: new StreamOpenOfficeDocumentConverter(connection);
return converter;
}
public static void main(String[] args) throws Exception {
File file = new File("H:/zf/index/test/2.xls");
FileInputStream fileInputStream = new FileInputStream(file);
File file1 = new File("H:/zf/index/test/2.1.pdf");
FileOutputStream fileOutputStream = new FileOutputStream(file1);
//这里端口是上面你启动服务设置的端口
OpenOfficeUtils.convert(fileInputStream, "doc", fileOutputStream, "pdf", "192.168.30.112", 8100);
}
}
目前转换office可以正常转换,excel因为本身长度,openOffice会以4A大小转换所以要覆写相关类调整
/**
* 自定义转换格式
* @author zf
*/
public class ConverterDocument extends StreamOpenOfficeDocumentConverter {
public ConverterDocument(OpenOfficeConnection connection) {
super(connection);
}
public final static Size A5, A4, A3;
public final static Size B4, B5, B6;
public final static Size paperSize;
static {
A5 = new Size(14800, 21000);
A4 = new Size(21000, 29700);
A3 = new Size(29700, 42000);
B4 = new Size(25000, 35300);
B5 = new Size(17600, 25000);
B6 = new Size(12500, 17600);
//最大限度 宽 1600000
paperSize = new Size(29700, 27940);
}
@Override
protected void refreshDocument(XComponent document) {
super.refreshDocument(document);
XPrintable xPrintable = (XPrintable) UnoRuntime.queryInterface(XPrintable.class, document);
PropertyValue[] printerDesc = new PropertyValue[2];
// 转换
printerDesc[0] = new PropertyValue();
printerDesc[0].Name = "PaperFormat";
printerDesc[0].Value = PaperFormat.USER;
// 纸张大小
printerDesc[1] = new PropertyValue();
printerDesc[1].Name = "PaperSize";
printerDesc[1].Value = paperSize;
try {
xPrintable.setPrinter(printerDesc);
} catch (Exception e) {
e.printStackTrace();
}
}
}
将工具类用到
StreamOpenOfficeDocumentConverter 改成我们实现的类即可,大小可以自己调整