直接上代码吧。
/** * 根据流程实例Id,获取实时流程图片 * * @param processInstanceId * @param outputStream * @return */ public static void getFlowImgByInstanceId(String processInstanceId, OutputStream outputStream) { try { if (StringUtils.isEmpty(processInstanceId)) { logger.error("processInstanceId is null"); return; } // 获取历史流程实例 HistoricProcessInstance historicProcessInstance = activitiUtil.historyService .createHistoricProcessInstanceQuery() .processInstanceId(processInstanceId).singleResult(); // 获取流程中已经执行的节点,按照执行先后顺序排序 List<HistoricActivityInstance> historicActivityInstances = activitiUtil.historyService .createHistoricActivityInstanceQuery() .processInstanceId(processInstanceId) .orderByHistoricActivityInstanceId() .asc().list(); // 高亮已经执行流程节点ID集合 List<String> highLightedActivitiIds = new ArrayList<>(); int index = 1; for (HistoricActivityInstance historicActivityInstance : historicActivityInstances) { // 用默认颜色 highLightedActivitiIds.add(historicActivityInstance.getActivityId()); index++; } ProcessDiagramGenerator processDiagramGenerator = null; // 使用默认的程序图片生成器 processDiagramGenerator = new DefaultProcessDiagramGenerator(); BpmnModel bpmnModel = activitiUtil.repositoryService .getBpmnModel(historicProcessInstance.getProcessDefinitionId()); // 高亮流程已发生流转的线id集合 List<String> highLightedFlowIds = getHighLightedFlows(bpmnModel, historicActivityInstances); // 使用默认配置获得流程图表生成器,并生成追踪图片字符流 InputStream imageStream = processDiagramGenerator.generateDiagram(bpmnModel, highLightedActivitiIds, highLightedFlowIds, "宋体", "微软雅黑", "黑体"); // 输出图片内容 Integer byteSize = 1024; byte[] b = new byte[byteSize]; int len; while ((len = imageStream.read(b, 0, byteSize)) != -1) { outputStream.write(b, 0, len); } } catch (Exception e) { logger.error("processInstanceId" + processInstanceId + "生成流程图失败,原因:" + e.getMessage(), e); } }