0

I have an xml file already being created and rendered as a PDF sent over a servlet:

TraxInputHandler input = new TraxInputHandler(
   new File(XML_LOCATION+xmlFile+".xml"),
   new File(XSLT_LOCATION)
);
ByteArrayOutputStream out = new ByteArrayOutputStream();

//driver is just `new Driver()`
synchronized (driver) {
  driver.reset();
  driver.setRenderer(Driver.RENDER_PDF);
  driver.setOutputStream(out);
  input.run(driver);
}

//response is HttpServletResponse
byte[] content = out.toByteArray();
response.setContentType("application/pdf");
response.setContentLength(content.length);
response.getOutputStream().write(content);
response.getOutputStream().flush();

This is all working perfectly fine.

However, I now have another PDF file that I need to include in the output. This is just a totally separate .pdf file that I was given. Is there any way that I can append this file either to the response, the driver, out, or anything else to include it in the response to the client? Will that even work? Or is there something else I need to do?

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405

1 Answers1

2

We also use FOP to generate some documents, and we accept uploaded documents, all of which we eventually combine into a single PDF.

You can't just send them sequentially out the stream, because the combined result needs a proper PDF file header, metadata, etc.

We use the iText library to combine the files, starting off with

PdfReader reader = new PdfReader(/*String*/fileName);
reader.consolidateNamedDestinations();

We later loop through adding pages from each pdf to the new combined destination pdf, adjusting the bookmark / page numbers as we go.

AFAIK, FOP doesn't provide this sort of functionality.

Stephen P
  • 14,422
  • 2
  • 43
  • 67