[Solved] Return as ByteArrayOutputStream instead of FileOutputStream


I am slightly confused to what you are asking.
I have not used Apache Fop, but will try answer this question.

If you want to read PDF file as byte array use input streams instead.

But if you want to use ByteArrayOutputStream that is writing bytes you basically answered your own question, try using existing BAOS that you created initially and you are using in FileOutputStream, that’s assuming the byte array output stream is reading bytes via some InputStream or some other source. Second assumption is that BAOS and FOS were able to properly write PDF file you were talking about.
You can simply do:

byte[] b = outStream.toByteArray();
String str = Base64.getEncoder().encodeToString(b); 

solved Return as ByteArrayOutputStream instead of FileOutputStream