大家好,又见面了,我是你们的朋友全栈君。
package cn.keking.service.impl;
import cn.keking.hutool.StrUtil;
import cn.keking.render.AseKsTableRenderPolicy;
import cn.keking.render.CustomReplaceOptionalTextPictureRefRenderPolicy;
import cn.keking.service.CapsReportService;
import cn.keking.utils.FileUtils;
import cn.keking.utils.OfficeToPdf;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Map;
/**
* @program: filepreview
* @description: Caps AseKs报告
* @author: syngna
* @create: 2020-04-14 12:08
*/
@Service(“aseKs”)
public class AseKsReportServiceImpl implements CapsReportService {
@Autowired
private OfficeToPdf officeToPdf;
private static final Logger LOGGER = LoggerFactory.getLogger(AseKsReportServiceImpl.class);
@Override
public String processCapsReport(Map data, String picturePath, String templatePath, String outputPath) {
FileOutputStream out = null;
XWPFTemplate template = null;
String errorMsg = “”;
try {
// 计算图片比例
BufferedImage image = ImageIO.read(new FileInputStream(picturePath));
int width = image.getWidth();
int height = image.getHeight();
Configure configure = Configure.newBuilder()
.bind(“dynamic_table”, new AseKsTableRenderPolicy())
.referencePolicy(
new CustomReplaceOptionalTextPictureRefRenderPolicy(“picture-right”,
new FileInputStream(picturePath),
XWPFDocument.PICTURE_TYPE_PNG, BigDecimal.valueOf(width).divide(BigDecimal.valueOf(height), 6, BigDecimal.ROUND_HALF_UP)))
.build();
template = XWPFTemplate.compile(templatePath, configure).render(data);
FileUtils.createDirPath(outputPath.substring(0, outputPath.lastIndexOf(File.separator)));
out = new FileOutputStream(outputPath);
template.write(out);
officeToPdf.doc2PDF(outputPath, outputPath.substring(0, outputPath.lastIndexOf(“.”) + 1) + “pdf”);
} catch (Exception e) {
LOGGER.error(“”, e);
errorMsg += e.getMessage();
} finally {
try {
if (out != null) {
out.flush();
out.close();
}
if (template != null) {
template.close();
}
} catch (IOException e) {
LOGGER.error(“”, e);
}
}
return errorMsg;
}
}
一键复制
编辑
原始数据
按行查看
历史
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/156404.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...