我正在尝试使用libre/openpdf (https://github.com/LibrePDF/OpenPDF)和spring的路由器函数在内存中创建一个pdf。
我有一个流量的com.lowagie.text.Element包含pdf的内容。
使用的com.lowagie.text.pdf.PdfWriter包含一个com.lowagie.text.Document和一个OutputStream。将Element添加到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的通量?
发布于 2019-10-08 20:26:54
尝试then(...)-Operator,因为它让第一个单声道完成,然后播放另一个单声道。
...
content.doOnNext { element ->
document.add(element)
}
// .ignoreElements() // necessary?
.doFinally(signal -> document.close())
.then(byteArrayOutputStream.toByteArray().toMono())
...我认为没有.ignoreElements()它应该可以工作。
https://stackoverflow.com/questions/58260637
复制相似问题