From e09d8b7d0db969724aa2f56d87f869010039bea3 Mon Sep 17 00:00:00 2001 From: zhousq Date: Wed, 31 Jul 2024 16:31:34 +0800 Subject: [PATCH] =?UTF-8?q?2024-07-31=20pdf=E6=A8=A1=E7=89=88=E7=A9=BA?= =?UTF-8?q?=E6=8C=87=E9=92=88bug=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/wuliaotest01.pdf | Bin 57074 -> 57076 bytes .../print/WinPrintServerControler.java | 4 - .../java/com/win/print/util/PdfModelUitl.java | 72 ++++++++++-------- 3 files changed, 40 insertions(+), 36 deletions(-) diff --git a/models/wuliaotest01.pdf b/models/wuliaotest01.pdf index 5e3a4bf7bd3b1a4020904b0e7835737726e9eba3..270b652c1406b1aed28aa044d75cc5b0e74eb346 100644 GIT binary patch delta 843 zcmZXTK}rKb5Jib27zYF|AO*~`QYV?CD$Ik?oIR0$Nk2QU~xwGJNfcrg$Yz9 zENK=XSe1G4NydOaWiXKWt8IF47Y0(rhSO#kETqkJnbQ!av_S@zbw&fg;H-wYpdp?$ zB(Q=Y;5wvZAb7<iieHSF1k& delta 760 zcmZvZO-chn5Jrg;{25e)Ie-Dl!j)CsT|HHpEG9D}h={n+14OrqUO+rUZ(Vr;B?}SB z0bIH^33vigJb-EPldLKqNn~ga@>)6Pk*EpPM71AqgR_0Pj;Q({a zv)mN`8E{GDB-}^a(;ExT>$}CX^Zx14@%qN@PNx&DnlM}8QpbbWhuhVC*MhIrI$4Up zH;xHeWlX-F35Zcy$`sz7lWv_#U~K(#l?l`c1l6P=8zmR@J_6@8m5inmYpS>PnFze- zj+A~nmZw4pgiLrDC0&I@@Cu7)6c&+Pz;V)5k40Ol|N8-imNeC&50@1|DL~R paramsList,File tempFile, JSONObject data, String modelName, String templatePath) throws IOException, NoSuchFieldException { + public void pdfMdelFromRender(List 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图片 - 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"); + 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); + image.setFixedPosition(rtl.getX(), rtl.getY(), rtl.getWidth()); // 设置图片固定位置 + document.add(image); + } catch (IOException e) { + field.setValue("无图片"); } - 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()); - 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 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();