Java Spring Boot Webfux(Reactive)-将数据导出为.pdf



我使用openPDF库通过HttpServlet响应将列表导出为pdf。然而,HttpServletResponse在Webflux中不受支持,所以有人能指导我如何以反应的方式写这篇文章吗?

这是我使用的代码:https://www.codejava.net/frameworks/spring-boot/pdf-export-example

我在Webflux中找到了一个将列表导出为.csv的示例,并试图将代码改编为该示例,但到目前为止我还没有遇到任何运气。。以下是该示例的链接:https://medium.com/@victortemitope95/如何编写和下载csv-file-in-spring-webflux-5df8d817a597

无论您使用的PDF库如何,最终您都可以将内容转换为类似java.io.ByteArrayOutputStream的内容。这样就可以创建一个org.springframework.http.ResponseEntity(在本例中使用Kotlin和suspend函数(:

@GetMapping("/pdf")
suspend fun pdf(): ResponseEntity<ByteArray> {
val pdf: ByteArrayOutputStream = // get the PDF from your library

return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=example.pdf")
.contentType(MediaType.APPLICATION_PDF)
.body(pdf.toByteArray())
}

这是一个完整的教程视频。

最新更新