Browse Source

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

master
zhousq 4 months ago
parent
commit
00e65f72cb
  1. BIN
      models/tag_model_001.pdf
  2. BIN
      models/wuliaotest01.pdf
  3. 2
      win-print/src/main/java/com/win/print/service/QRCodeService.java
  4. 34
      win-print/src/main/java/com/win/print/util/PdfModelUitl.java
  5. 4
      win-print/src/main/resources/templates/print/modelmanager/add.html
  6. 2
      win-print/src/main/resources/templates/print/modelmanager/edit.html

BIN
models/tag_model_001.pdf

Binary file not shown.

BIN
models/wuliaotest01.pdf

Binary file not shown.

2
win-print/src/main/java/com/win/print/service/QRCodeService.java

@ -27,7 +27,7 @@ public class QRCodeService {
// 设置错误纠正级别为L,表示较低的纠错能力。
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
// 设置边距为2,即二维码边缘与内容之间的距离为2个像素。
hints.put(EncodeHintType.MARGIN, 0);
hints.put(EncodeHintType.MARGIN, 1);
// 设置字符集为UTF-8,表示二维码支持UTF-8编码的字符。
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

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

@ -77,22 +77,25 @@ public class PdfModelUitl {
if(null!=field) {
Rectangle rtl = field.getWidgets().get(0).getRectangle().toRectangle(); // 获取表单域的xy坐标
field.setFont(defaultFont());
field.setFontSize(12);
if(Optional.ofNullable(field.getFontSize()).isEmpty()){
field.setFontSize(16);
}
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;
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(value, width, height);
com.itextpdf.layout.element.Image image = getImage(field, bufferedImage, rtl);
// 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(value, (int)rtl.getWidth(), (int)rtl.getHeight());
byte[] imgBytes = qrCodeService.getImgBuffer("png", bufferedImage);
com.itextpdf.layout.element.Image image =new com.itextpdf.layout.element.Image(ImageDataFactory.create(imgBytes));//getImage(field, bufferedImage, rtl);
image.setFixedPosition(rtl.getX(), rtl.getY(), rtl.getWidth()); // 设置图片固定位置
document.add(image);
} catch (IOException e) {
@ -201,19 +204,20 @@ public class PdfModelUitl {
}
}
///*pdf 模版图片压缩处理*/
private com.itextpdf.layout.element.Image getImage(PdfFormField formField, BufferedImage bufferedImage,Rectangle rtl) throws IOException {
byte[] imgBytes = qrCodeService.getImgBuffer("jpeg", bufferedImage);
private com.itextpdf.layout.element.Image getImage( BufferedImage bufferedImage,Rectangle rtl) throws IOException {
byte[] imgBytes = qrCodeService.getImgBuffer("png", bufferedImage);
if (Float.compare(bufferedImage.getWidth(), rtl.getWidth()) <= 0 && Float.compare(bufferedImage.getHeight(), rtl.getHeight()) <= 0) {// 不处理
return new com.itextpdf.layout.element.Image(ImageDataFactory.create(imgBytes));
} else {
// 压缩图片。计算得到图片放缩的最大比例
float scale = Math.max(bufferedImage.getWidth() / rtl.getWidth(), bufferedImage.getHeight() / rtl.getHeight());
float scale = Math.min(rtl.getWidth()/bufferedImage.getWidth() , rtl.getHeight() / bufferedImage.getHeight() );
// 压缩图片
Image scale1 = ImgUtil.scale(ImgUtil.toImage(imgBytes), scale);
return new com.itextpdf.layout.element.Image(ImageDataFactory.create(ImgUtil.toBytes(scale1, ImgUtil.IMAGE_TYPE_PNG)));
}
}
/**
* 根据表单域关键字查找当前关键字所在页对象PdfPage
*

4
win-print/src/main/resources/templates/print/modelmanager/add.html

@ -44,11 +44,11 @@
</div>
</div>
</div>
<div class="col-xs-12">
<div class="col-xs-12" style="display: none">
<div class="form-group">
<label class="col-sm-3 control-label is-required">文件名称:</label>
<div class="col-sm-8">
<input name="mFilename" id="mFilename" iclass="form-control" type="text" required>
<input name="mFilename" id="mFilename" iclass="form-control" type="text" required >
</div>
</div>
</div>

2
win-print/src/main/resources/templates/print/modelmanager/edit.html

@ -48,7 +48,7 @@
<div class="form-group">
<label class="col-sm-3 control-label is-required">文件名称:</label>
<div class="col-sm-8">
<input name="mFilename" th:field="*{mFilename}" class="form-control" type="text" required>
<input name="mFilename" th:field="*{mFilename}" class="form-control" type="text" required readonly>
</div>
</div>
</div>

Loading…
Cancel
Save