package com.lpo.web.controller.common;
import com.artofsolving.jodconverter.DocumentConverter;
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.poi.hssf.usermodel.HSSFPrintSetup;
import org.apache.poi.xssf.usermodel.XSSFPrintSetup;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
import java.net.ConnectException;
public class OfficeToPDF {
static final String host = "xxx.xxx.xxx.xxx";
static final int post = 8100;
public static void toPdf(String filePath, String pdfPath) {
try {
long old = System.currentTimeMillis();
System.out.println("open-开始:" + filePath);
File wordFile = new File(filePath);
File PDFFile = new File(pdfPath);
SocketOpenOfficeConnection connection = new SocketOpenOfficeConnection(host, post);
try {
connection.connect();
DocumentConverter converter = getConverter(host, connection);
converter.convert(wordFile, PDFFile);
long now = System.currentTimeMillis();
System.out.println("open-完成:" + filePath);
System.out.println("共耗时:" + (double) (now - old) / 1000.0D + "秒");
} catch (ConnectException var14) {
System.out.println("获取OpenOffice连接失败...");
var14.printStackTrace();
} finally {
connection.disconnect();
}
} catch (Exception var16) {
System.out.println("转换失败:" + filePath);
var16.printStackTrace();
}
}
/**
* excel设置 打印缩放比例
*
* @param inputPath
* @param outPath
*/
public static void setExcelScale(String inputPath, String outPath) throws IOException {
//读取excel文件
XSSFWorkbook workbook;
workbook = new XSSFWorkbook(new FileInputStream(inputPath));
OutputStream fos = null;
try {
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {//获取每个Sheet表
XSSFSheet sheet = workbook.getSheetAt(i);
//打印设置
XSSFPrintSetup print = sheet.getPrintSetup();
print.setLandscape(true); // 打印方向,true:横向,false:纵向(默认)
print.setFitHeight((short) 0);//设置高度为自动分页
print.setFitWidth((short) 1);//设置宽度为一页
print.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE); //纸张类型
// print.setScale((short)55);//自定义缩放①,此处100为无缩放
//启用“适合页面”打印选项的标志
sheet.setFitToPage(true);
}
// Excel文件生成后存储的位置。
File file = new File(outPath);
fos = new FileOutputStream(file);
workbook.write(fos);
} catch (Exception e) {
System.out.println("setExcelScale fail: 创建输出文件IO异常");
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
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 StreamOpenOfficeDocumentConverter(connection)
: new OpenOfficeDocumentConverter(connection);
return (DocumentConverter) converter;
}
}
1.安装openoffice,以windows系统为例,cmd进入openoffice安装文件目录program文件夹内执行,soffice -headless -accept="socket,host=xxx.xxx.xxx.xxx,port=8100;urp;" -nofirststartwizard
2.调用该方法即可实现转换pdf,且解决原生openoffice转换后文档走样问题。
jar包链接:https://pan.baidu.com/s/17-dyFq7PM9wE9rMVHVawBw
提取码:0oq5