即时创建 PDF 而不将其存储在 drive/server 上,然后将其转换为 FileBody 类型

Create PDF on-the-fly without storing it on the drive/server and then convert it to type FileBody

我有以下代码:

    MultipartEntityBuilder builder = MultipartEntityBuilder.create();

    //Want this to be on-the-fly pdf creation instead of using an existing one
    File thePDF = new File("/path-to-pdf");

    FileBody fb = new FileBody(thePDF);
    builder.addPart("Body", fb);
    ....Code to use this builder with pdf in a rest callout

在上面的代码中,我使用了现有的 pdf 文件。我想改用动态创建的 pdf。我知道如何使用 iText 创建 pdf,但我不想在服务器中创建新文件。有没有一种方法可以存储文件的内容并将其类型转换为类型 FileBody 或 File 以与 MultipartEntityBuilder 一起使用,而无需实际在磁盘上创建文件?

有什么建议吗?

提前致谢!

创建 PDF 使用:

ByteArrayOutputStream baOut = new ByteArrayOutputStream();
PdfWriter writer = PdfWriter.getInstance(document, baOut);

并用于构建 HttpEntity:

builder.addBinaryBody(name, baOut.toByteArray(), contentType, filename);