private void renderPDF(OutputStream out, Request request, Response response) throws Exception { Map<?, ?> properties = Play.configuration; String uri = request.getBase() + request.url; if (docs.documents.size() == 1) { renderDoc(docs.documents.get(0), uri, properties, out); } else { // we need to concatenate them all Document resultDocument = new Document(); PdfCopy copy = new PdfCopy(resultDocument, out); resultDocument.open(); ByteArrayOutputStream os = new ByteArrayOutputStream(); for (PDFDocument doc : docs.documents) { os.reset(); renderDoc(doc, uri, properties, os); PdfReader pdfReader = new PdfReader(os.toByteArray()); int n = pdfReader.getNumberOfPages(); for (int i = 0; i < n; i++) { copy.addPage(copy.getImportedPage(pdfReader, i + 1)); } copy.freeReader(pdfReader); } resultDocument.close(); } }