user1652925
user1652925

Reputation: 131

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

I have the following code:

    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

In the above code I am using an existing pdf file. I want to use a dynamically created pdf instead. I know how to create a pdf using iText, but I DO NOT want to create a new file in the server. Is there a way to store the contents of the file and type cast it to type FileBody or File for use with MultipartEntityBuilder, without actually creating a file on the disk?

Any suggestions?

Thanks in advance!

Upvotes: 2

Views: 1523

Answers (1)

reynev
reynev

Reputation: 336

For creating PDF use:

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

and for building HttpEntity:

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

Upvotes: 3

Related Questions