/**
 * 生成流程图
 *
 * @param processId 任务ID
 */
@RequestMapping("/diagram/{processId}")
public void genProcessDiagram(HttpServletResponse response,
                              @PathVariable("processId") String processId) {
    InputStream inputStream = flowTaskService.diagram(processId);
    OutputStream os = null;
    BufferedImage image = null;
    try {
        image = ImageIO.read(inputStream);
        response.setContentType("image/png");
        os = response.getOutputStream();
        if (image != null) {
            ImageIO.write(image, "png", os);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (os != null) {
                os.flush();
                os.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
/**
 * 获取流程过程图
 */
public InputStream diagram(String processId) {
    String processDefinitionId;
    // 获取当前的流程实例
    ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
    // 如果流程已经结束,则得到结束节点
    if (Objects.isNull(processInstance)) {
        HistoricProcessInstance pi = historyService.createHistoricProcessInstanceQuery().processInstanceId(processId).singleResult();

        processDefinitionId = pi.getProcessDefinitionId();
    } else {// 如果流程没有结束,则取当前活动节点
        // 根据流程实例ID获得当前处于活动状态的ActivityId合集
        ProcessInstance pi = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        processDefinitionId = pi.getProcessDefinitionId();
    }

    // 获得活动的节点
    List<HistoricActivityInstance> highLightedFlowList = historyService.createHistoricActivityInstanceQuery()
        .processInstanceId(processId).orderByHistoricActivityInstanceStartTime().asc().list();

    List<String> highLightedFlows = new ArrayList<>();
    List<String> highLightedNodes = new ArrayList<>();
    //高亮线
    for (HistoricActivityInstance tempActivity : highLightedFlowList) {
        if ("sequenceFlow".equals(tempActivity.getActivityType())) {
            //高亮线
            highLightedFlows.add(tempActivity.getActivityId());
        } else {
            //高亮节点
            highLightedNodes.add(tempActivity.getActivityId());
        }
    }

    //获取流程图
    BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId);
    ProcessEngineConfiguration configuration = processEngine.getProcessEngineConfiguration();
    //获取自定义图片生成器
    ProcessDiagramGenerator diagramGenerator = new CustomProcessDiagramGenerator();
    return diagramGenerator.generateDiagram(bpmnModel, "png", highLightedNodes, highLightedFlows, configuration.getActivityFontName(),
        configuration.getLabelFontName(), configuration.getAnnotationFontName(), configuration.getClassLoader(), 1.0, true);
}

Flowable 生成流程图-LMLPHP

01-18 05:40