原文:https://blog.csdn.net/FANKYIXUN/article/details/80627339
问题:Java 操作 OpenOffice 将excel 转换为 pdf ,因多列出现折行;
原因:OpenOffice 默认输出为A4 大小,源excel 存在队列,总宽度超出 A4 宽度,所以出现折行;
解决方法:自定义一个类,继承OpenOfficeDocumentConverter 类 重写方法,与 refreshDocument 方法,设置 OpenOffice 输出 pdf 的宽度参数:
Windows系统参考代码如下:
package com.syni.app.utils;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import com.sun.star.awt.Size;
import com.sun.star.beans.PropertyValue;
import com.sun.star.lang.XComponent;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.view.PaperFormat;
import com.sun.star.view.XPrintable;
public class ConverterDocument extends OpenOfficeDocumentConverter {
public ConverterDocument(OpenOfficeConnection connection) {
super(connection);
}
public final static Size A5, A4, A3;
public final static Size B4, B5, B6;
public final static Size KaoqinReport;
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);
KaoqinReport = new Size(29700, 27940); //最大限度 宽 1600000
}
@Override
protected void refreshDocument(XComponent document) {
super.refreshDocument(document);
// The default paper format and orientation is A4 and portrait. To
// change paper orientation
// re set page size
XPrintable xPrintable = (XPrintable) UnoRuntime.queryInterface(XPrintable.class, document);
PropertyValue[] printerDesc = new PropertyValue[2];
// Paper Orientation
// printerDesc[0] = new PropertyValue();
// printerDesc[0].Name = "PaperOrientation";
// printerDesc[0].Value = PaperOrientation.PORTRAIT;
// Paper Format
printerDesc[0] = new PropertyValue();
printerDesc[0].Name = "PaperFormat";
printerDesc[0].Value = PaperFormat.USER;
// Paper Size
printerDesc[1] = new PropertyValue();
printerDesc[1].Name = "PaperSize";
printerDesc[1].Value = KaoqinReport;
try {
xPrintable.setPrinter(printerDesc);
} catch (Exception e) {
e.printStackTrace();
}
}
}
问题:把项目打包到Linux服务器上后进行使用,发现转换出来的pdf文件还是会出现折行问题,排查之后发现,由于公司Linux系统没有网络打印机,于是在Windows系统下调用OpenOffice服务,改了本地机器的ip地址后,发现连接被拒绝了。
原因:因为远程服务启动的时候,host=真实的对外IP,有远程协议,这是因调用带有协议的文档操作
解决办法:
1.因为远程服务启动的时候,host=真实的对外IP,不能写127.0.0.1。改成本地机器的IP地址即可。
2.更改自定义类ConverterDocument,不再继承OpenOfficeDocumentConverter这个类。改为继承 StreamOpenOfficeDocumentConverter这个类。
public class ConverterDocument extends StreamOpenOfficeDocumentConverter {...}