0

The task is to combine two different pdf files into one generated in one query. There is a method that generates a pdf file based on the received data and saves the result in HttpServletResponse. So far, for example, I want to make two ByteArrayOutputStreams from one response and eventually combine the results into one pdf file.

Getting an OutputStream

ServletOutputStream pdfOne =  response.getOutputStream();
ServletOutputStream pdfTwo = response.getOutputStream();

Transform it into a byte array

ByteArrayOutputStream outputOne = new ByteArrayOutputStream();
outputOne.writeTo(pdfOne);
byte[] resultByteOne = outputOne.toByteArray();

ByteArrayOutputStream outputTwo = new ByteArrayOutputStream();
outputTwo.writeTo(pdfTwo);
byte[] resultByteTwo = outputTwo.toByteArray();

And with the help of PDFBox, I want to connect exactly two byte arrays. Everywhere there are examples of how to make merge from ready-made pdf files from the repository

And in general, can I do that?

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • You already mentioned PDFBox and [`PDFMergerUtility` has a `addSource(InputStream)` method](https://pdfbox.apache.org/docs/2.0.0/javadocs/org/apache/pdfbox/multipdf/PDFMergerUtility.html#addSource(java.io.InputStream)). Have you tried using that? – Joachim Sauer Nov 21 '22 at 12:50

0 Answers0