【发布时间】:2020-02-04 05:20:57
【问题描述】:
我正在尝试使用 libre/openpdf (https://github.com/LibrePDF/OpenPDF) 和 spring 的路由器功能在内存中创建一个 pdf。
我有一个包含 pdf 内容的 com.lowagie.text.Element Flux。
使用的com.lowagie.text.pdf.PdfWriter 采用com.lowagie.text.Document 和OutputStream。将Elements 添加到Document,并将数据写入OutputStream。
我需要将Outputstream 中的输出写入org.springframework.web.reactive.function.server.ServerResponse 的正文。
我尝试使用以下方法解决此问题:
//inside the routerfunctionhandler
val content: Flux<Element> = ...
val byteArrayOutputStream = ByteArrayOutputStream()
val document = Document()
PdfWriter.getInstance(document, byteArrayOutputStream)
document.open()
content.doOnNext { element ->
document.add(element)
}
.ignoreElements()
.block()
document.close()
byteArrayOutputStream.toByteArray().toMono()
.let {
ok().body(it.subscribeOn(Schedulers.elastic()))
}
上述方法有效,但在弹性线程内有一个丑陋的块,不保证清理资源。
有没有一种简单的方法可以将OutputStream 的输出转换为DataBuffers 的通量?
【问题讨论】:
标签: spring-webflux project-reactor openpdf