简体   繁体   中英

iText 5: Create a single PDF "ByteArrayOutputStream()" from two streams in a Java "HttpServlet" servlet to display in browser window

I have a Java "HttpServlet" class that creates an iText 5 PDF document by calling a Java class that returns the PDF document as a "ByteArrayOuputStream." The stream gets written to the "HttpServletResponse" of the servlet and displayed in the browser window.

In the Java servlet:

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException{

// Other code.

ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();

// Java class call to create PDF document.

baosPDF = new InvoicePDF();

// Set up the return HTTP header.

response.setHeader("Cache-Control", "max-age=30");
response.setContentType("application/pdf");
response.setHeader("Content-disposition", "inline; filename=INV_"
                            + request.getParameter("invoiceNumber").toString()
                            + ".pdf");

// Set length HTTP header value.

response.setContentLength(baosPDF.size());

// Write all data to the browser.

ServletOutputStream sos = response.getOutputStream();

baosPDF.writeTo(sos);
           
sos.flush();
 
// Other code.

}

The above code works well.

In certain circumstances, I need to create another "ByteArrayOuputStream" PDF document in the servlet using another Java class and have that PDF document displayed first and then the other PDF document next in the browser window.

I tried concatenating the two "ByteArrayOutputStream" (write method) into one and writing to the "ServletOutputStream," but only the stream written on the end of the "ByteArrayOuputStream" displays in the browser window.

In the servlet, I need to create a single PDF "ByteArrayOuputStream," somehow merging the two PDF documents.

ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();

ByteArrayOutputStream baosCoverPDF = new ByteArrayOutputStream();

//Java class call to create PDF document

baosCoverPDF = new coverPDF();

baosPDF = new InvoicePDF(hosturl, invoiceNumber);  

I need a single "ByteArrayOuputStream" that contains both "baoCoverPDF" and "baosPDF" streams as a single PDF that will display both PDF streams in the browser? Any help on a direction such as a combination of a "PdfReader" and "PdfCopy"?

I was able to get it to work inside the Java servlet with the following code.

Document pdfDocument = new Document();
ByteArrayOutputStream pdfOutputStream = new ByteArrayOutputStream();
PdfCopy copy = new PdfCopy(pdfDocument, pdfOutputStream);

pdfDocument.open();

baosCoverPDF = new coverPDF();

PdfReader coverPageReader = new PdfReader(baosCoverPDF.toByteArray());

for (int i = 1; i <= coverPageReader.getNumberOfPages(); i++) {
   copy.addPage(copy.getImportedPage(coverPageReader, i));
}

baosInvoice = new InvoicePDF(); 

PdfReader invoiceReader = new PdfReader(baosInvoice.toByteArray());

for (int i = 1; i <= invoiceReader.getNumberOfPages(); i++) {
   copy.addPage(copy.getImportedPage(invoiceReader, i));
}

pdfDocument.close();

baosPDF.write(pdfOutputStream.toByteArray());

copy.close();
coverPageReader.close();
invoiceReader.close();

I then perform the rest as before to output to the browser window.

// Set up the return HTTP header.

response.setHeader("Cache-Control", "max-age=30");
response.setContentType("application/pdf");
response.setHeader("Content-disposition", "inline; filename=INV_"
                            + 
request.getParameter("invoiceNumber").toString()
                            + ".pdf");

// Set length HTTP header value.

response.setContentLength(baosPDF.size());

// Write all data to the browser.

ServletOutputStream sos = response.getOutputStream();

baosPDF.writeTo(sos);
           
sos.flush();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM