Appearance
getProcessDiagram
Flowable 7.1.0 摘要:获取流程定义的流程图输入流(PNG格式)。
方法签名与说明
InputStream getProcessDiagram(String processDefinitionId)
获取流程定义的流程图图片输入流。流程图以PNG格式返回,包含中文节点名称的渲染。
Parameters:
- processDefinitionId - 流程定义ID,不能为null
Returns:
- InputStream - 流程图的PNG图片输入流,如果没有流程图则返回null
常见使用场景
1. 流程预览
在流程管理后台展示流程定义的可视化流程图。
2. 流程文档导出
将流程图导出为图片,用于文档编写和培训材料。
3. 流程审批页面
在审批页面展示完整的流程图,帮助审批人了解流程走向。
Kotlin + Spring Boot 调用示例
示例1:下载流程图
kotlin
import org.flowable.engine.RepositoryService
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import javax.servlet.http.HttpServletResponse
@RestController
@RequestMapping("/api/processes")
class ProcessDiagramController(
private val repositoryService: RepositoryService
) {
/**
* 下载流程图
* 企业场景:流程管理后台查看流程图
*/
@GetMapping("/{processDefinitionId}/diagram", produces = [MediaType.IMAGE_PNG_VALUE])
fun getProcessDiagram(@PathVariable processDefinitionId: String): ResponseEntity<ByteArray> {
val inputStream = repositoryService.getProcessDiagram(processDefinitionId)
?: return ResponseEntity.notFound().build()
val bytes = inputStream.readBytes()
return ResponseEntity.ok()
.contentType(MediaType.IMAGE_PNG)
.body(bytes)
}
}示例2:保存流程图到文件
kotlin
import org.flowable.engine.RepositoryService
import org.springframework.stereotype.Service
import java.io.File
@Service
class ProcessDiagramExportService(
private val repositoryService: RepositoryService
) {
/**
* 导出流程图到文件
* 企业场景:批量导出所有流程图用于文档归档
*/
fun exportProcessDiagram(processDefinitionId: String, outputPath: String) {
val processDefinition = repositoryService.getProcessDefinition(processDefinitionId)
val inputStream = repositoryService.getProcessDiagram(processDefinitionId)
?: throw IllegalStateException("该流程没有流程图")
val fileName = "${processDefinition.key}_v${processDefinition.version}.png"
val file = File(outputPath, fileName)
file.outputStream().use { output ->
inputStream.copyTo(output)
}
println("流程图已保存: ${file.absolutePath}")
}
}示例3:在网页中展示流程图
kotlin
import org.flowable.engine.RepositoryService
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import java.util.Base64
@Controller
class ProcessViewController(
private val repositoryService: RepositoryService
) {
/**
* 流程详情页面
* 企业场景:在网页中展示流程图
*/
@GetMapping("/processes/{processDefinitionId}/view")
fun viewProcess(
@PathVariable processDefinitionId: String,
model: Model
): String {
val processDefinition = repositoryService.getProcessDefinition(processDefinitionId)
val inputStream = repositoryService.getProcessDiagram(processDefinitionId)
// 将图片转为Base64,直接在HTML中展示
val imageBase64 = inputStream?.use {
Base64.getEncoder().encodeToString(it.readBytes())
}
model.addAttribute("processName", processDefinition.name)
model.addAttribute("processKey", processDefinition.key)
model.addAttribute("version", processDefinition.version)
model.addAttribute("diagramImage", imageBase64)
return "process-detail"
}
}注意事项
1. 流程图存在性
- 不是所有流程定义都有流程图
- 如果没有流程图,方法返回null,需要判空处理
2. 中文支持
- 如果流程图中有中文乱码,检查Flowable配置中的字体设置
3. 资源释放
- InputStream使用完毕后要关闭,避免资源泄漏
- 建议使用Kotlin的
use函数自动管理资源
相关 API
RepositoryService.getProcessDiagram()- 获取流程图RepositoryService.getBpmnModel()- 获取BPMN模型RepositoryService.getProcessDiagramLayout()- 获取流程图布局RepositoryService.getResourceAsStream()- 获取资源流
本文档说明
- 基于 Flowable 7.1.0 版本编写
- 所有示例均可直接在 Spring Boot + Kotlin 项目中使用