Browse Source

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

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

Loading…
Cancel
Save