|
@ -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(); |
|
|