首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用反应堆熔剂和openpdf创建pdf

使用反应堆熔剂和openpdf创建pdf
EN

Stack Overflow用户
提问于 2019-10-07 03:38:08
回答 1查看 350关注 0票数 0

我正在尝试使用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的主体。

我尝试使用以下方法来解决这个问题:

代码语言:javascript
复制
//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的通量?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-08 20:26:54

尝试then(...)-Operator,因为它让第一个单声道完成,然后播放另一个单声道。

代码语言:javascript
复制
...
content.doOnNext { element ->
    document.add(element)
}
// .ignoreElements() // necessary?
.doFinally(signal -> document.close())
.then(byteArrayOutputStream.toByteArray().toMono())
...

我认为没有.ignoreElements()它应该可以工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58260637

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档