Browse Source

2024-07-31 pdf模版空指针bug修复

master
zhousq 4 months ago
parent
commit
e09d8b7d0d
  1. BIN
      models/wuliaotest01.pdf
  2. 4
      win-admin/src/main/java/com/win/web/controller/print/WinPrintServerControler.java
  3. 62
      win-print/src/main/java/com/win/print/util/PdfModelUitl.java

BIN
models/wuliaotest01.pdf

Binary file not shown.

4
win-admin/src/main/java/com/win/web/controller/print/WinPrintServerControler.java

@ -269,8 +269,6 @@ public class WinPrintServerControler extends BaseController {
} catch (IOException e) {
e.printStackTrace();
log.error("printOneByOne-error", e.toString());
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
}
}
@ -330,8 +328,6 @@ public class WinPrintServerControler extends BaseController {
tempFile.delete();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
}
});
pdfMerger.close();

62
win-print/src/main/java/com/win/print/util/PdfModelUitl.java

@ -2,6 +2,7 @@ package com.win.print.util;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.itextpdf.forms.PdfAcroForm;
@ -62,7 +63,7 @@ public class PdfModelUitl {
* 待填充的PDF模板文档
*/
public void pdfMdelFromRender(List<WinPrintModelParams> paramsList,File tempFile, JSONObject data, String modelName, String templatePath) throws IOException, NoSuchFieldException {
public void pdfMdelFromRender(List<WinPrintModelParams> paramsList,File tempFile, JSONObject data, String modelName, String templatePath) throws IOException {
PdfReader reader = new PdfReader(templatePath+"/"+modelName);
PdfWriter writer = new PdfWriter(tempFile);
PdfDocument pdf = new PdfDocument(reader, writer);
@ -72,50 +73,57 @@ public class PdfModelUitl {
if (ObjectUtil.isNotEmpty(data)) {
paramsList.forEach(item -> {
PdfFormField field = form.getField(item.getParamCode());
String value = Optional.ofNullable(data.getString(item.getParamCode())).orElse("");
if(null!=field) {
Rectangle rtl = field.getWidgets().get(0).getRectangle().toRectangle(); // 获取表单域的xy坐标
field.setFont(defaultFont());
field.setFontSize(12);
if ("QRIMG".equals(item.getParamType())) {
//渲染Qr图片
if(!value.isEmpty() && !value.isBlank()) {
try {
JSONObject jsonObject=JSONObject.parseObject(Optional.ofNullable(item.getParamDesc()).orElse("{\"width\":\"172\",\"height\":\"172\",\"type\":\"png\"}")) ;
int width=172;
JSONObject jsonObject = JSONObject.parseObject(Optional.ofNullable(item.getParamDesc()).orElse("{\"width\":\"172\",\"height\":\"172\",\"type\":\"png\"}"));
int width = 172;
int height = 172;
String type = "png";
if(null!=jsonObject){
width=Integer.valueOf(Optional.ofNullable(jsonObject.getString("width")).orElse("172"));
height=Integer.valueOf(Optional.ofNullable(jsonObject.getString("height")).orElse("172"));
type=Optional.ofNullable(jsonObject.getString("type")).orElse("png");
}
BufferedImage bufferedImage = qrCodeService.generateQRCode(Optional.ofNullable(data.getString(item.getParamCode())).orElse(""), width, height);
//byte[] pngs = qrCodeService.getImgBuffer("jpeg", bufferedImage);
//com.itextpdf.layout.element.Image image=new com.itextpdf.layout.element.Image(ImageDataFactory.create(pngs));
Rectangle rtl = field.getWidgets().get(0).getRectangle().toRectangle(); // 获取表单域的xy坐标
com.itextpdf.layout.element.Image image=getImage(field,bufferedImage,rtl);
image.setFixedPosition(rtl.getX(), rtl.getY(),rtl.getWidth()); // 设置图片固定位置
//image.setRelativePosition(rtl.getLeft(),rtl.getTop(), rtl.getRight(),rtl.getBottom());
if (null != jsonObject) {
width = Integer.valueOf(Optional.ofNullable(jsonObject.getString("width")).orElse("172"));
height = Integer.valueOf(Optional.ofNullable(jsonObject.getString("height")).orElse("172"));
type = Optional.ofNullable(jsonObject.getString("type")).orElse("png");
}
BufferedImage bufferedImage = qrCodeService.generateQRCode(value, width, height);
com.itextpdf.layout.element.Image image = getImage(field, bufferedImage, rtl);
image.setFixedPosition(rtl.getX(), rtl.getY(), rtl.getWidth()); // 设置图片固定位置
document.add(image);
} catch (IOException e) {
field.setValue("无图片");
}
}
} else if ("IMG".equals(item.getParamType())) {
//渲染普通图片
//PdfPage annotationPage = findAnnotationPage(item.getParamCode(),pdf);
//byte[] pngs = ImageIO.read("")
//doFillFieldImage(annotationPage,field,pngs);
//渲染普通图片,需要根据des属性获取图片的来源
Optional<String> paramDesc = Optional.ofNullable(item.getParamDesc());
String imagePath = DataUnitl.getSysemDir();
if(paramDesc.isPresent()){
if(FileUtil.exist(paramDesc.get())){
imagePath=paramDesc.get();
}else if(FileUtil.exist(imagePath+"/"+paramDesc.get())) {
imagePath=imagePath+"/"+paramDesc.get();
}else{
imagePath="";
}
}
byte[] imageBytes = ImageUtils.getImage(imagePath);
if(null!=imageBytes){
com.itextpdf.layout.element.Image image = new com.itextpdf.layout.element.Image(ImageDataFactory.create(imageBytes));
image.setFixedPosition(rtl.getX(), rtl.getY(), rtl.getWidth()); // 设置图片固定位置
document.add(image);
}
} else {
String s = Optional.ofNullable(data.getString(item.getParamCode())).orElse("");
field.setValue(s);
field.setValue(value);
}
}
});
} else {
throw new NoSuchFieldException("没有任何可渲染的数据内容!");
}
} else { //如果没有配置任何模版参数抛出异常
throw new NoSuchFieldException("没找到模版的参数配置信息!");
}
// 设置表单为只读(可选)
form.flattenFields();

Loading…
Cancel
Save