金蝶云苍穹 根据打印模板生成PDF文件,并保存到附件中

生成PDF所用方法 PrintServiceHelper.createSinglePdf

 //获取附件控件
            AttachmentPanel attachmentPanel = this.getView().getControl("attachmentpanel");
            List<Map<String, Object>> attachmentData = attachmentPanel.getAttachmentData();
            //附件数量
            int size = attachmentData.size();
            if (size >= 2) {
    
    
                this.getView().showErrorNotification("超过附件数量最大值2,请删除后再次生成!");
                return;
            }
            String pageId = this.getView().getPageId();
            Object pkValue = this.getModel().getValue("id");
            //参数说明 : 当前页的id 单据标识 打印模板标识 当前单据标识
            byte[] createPdf = PrintServiceHelper.createSinglePdf(pageId, KEY_FORMID, KEY_PRT_MODELID, pkValue);
            //限制生成的附件大小
            int attachSize = 2;
            //大于10M,提示信息 1048576 = 1M
            if(createPdf.length >= 1048576 * attachSize){
    
    
                this.getView().showErrorNotification("附件大小不能超过"+attachSize+"M !");
                return;
            }
            //拿到字节输入流
            InputStream pdfIs = new ByteArrayInputStream(createPdf);
            //附件缓存
            TempFileCache tfc = CacheFactory.getCommonCacheFactory().getTempFileCache();
            int timeout = 7200;
            //上传到附件
            StringBuffer uid = new StringBuffer("rc-upload-");
            uid.append((new Date()).getTime());
            uid.append("-");
            int index = (int) (1.0D + Math.random() * 10.0D);
            uid.append(index);
            Map<String, Object> attachmentInfo = new HashMap();
            List<Map<String, Object>> attachments = new ArrayList();
            String pkid = pkValue.toString();
            String formid = KEY_FORMID;
            String billno = (String) this.getModel().getValue("billno");
            String appId = this.getModel().getDataEntityType().getAppId();
            String neddPaths = tfc.saveAsUrl(billno + ".pdf", pdfIs, timeout);
            Map<String, Object> tempAttachment = new HashMap();
            SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
            String nowTime = sdf.format(new Date());
            tempAttachment.put("uid", uid);
            tempAttachment.put("url", neddPaths);
            tempAttachment.put("size", createPdf.length);
            tempAttachment.put("description", "生成pdf文件");
            tempAttachment.put("name", billno+"-"+nowTime + ".pdf");
            attachments.add(tempAttachment);
            //构造附件数据
            attachmentInfo.put("attachmentpanel", attachments);
            //AppId 应用的编码
            DynamicObjectCollection collection = AttachmentServiceHelper.saveTempAttachments(formid, pkid, appId, attachmentInfo);
            if (collection != null && collection.size() != 0) {
    
    
                this.getView().showSuccessNotification("附件已生成!");
                this.getView().updateView("attachmentpanel");
            }
            try {
    
    
                pdfIs.close();
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }

猜你喜欢

转载自blog.csdn.net/Evain_Wang/article/details/112847531