getOutputStream() has already been called for this response while exporting to pdf file from java

StackOverflow https://stackoverflow.com/questions/12522680

  •  03-07-2021
  •  | 
  •  

Question

i have the following code section in my java class.

if(exportTo.equals("pdf"))
        {
        response.setHeader("Content-disposition", "attachment; filename=\"" + reportName + ".pdf\"");
        response.setContentType("application/pdf");
        PdfWriter.getInstance(document,response.getOutputStream());

        try {
            document.open();
            addTitlePage(document, reportName);

            Image image = Image.getInstance(path+"/"+"images/abi.png");
            image.setAbsolutePosition(40f, 770f);
            image.scaleAbsolute(70f, 50f);
            document.add(image);
            response.flushBuffer();

            //float[] colsWidth = {1.5f,3f,4f,4f,2f};

            List<Float> colsWidth = new ArrayList<Float>();
            int iterator = 1;
           while (iterator <= headerMap.size()) {
               if(iterator==1){
                   colsWidth.add(1.5f); 
               }else{
                colsWidth.add(3f); 
               }
                iterator++;
            }
           float[] floatArray = ArrayUtils.toPrimitive(colsWidth.toArray(new Float[0]), 0.0F);

           PdfPTable table = new PdfPTable(floatArray);
            table.setWidthPercentage(98);
            table.setHorizontalAlignment(Element.ALIGN_CENTER);

            PdfPCell c1 = new PdfPCell();
            for (Iterator it = headerMap.keySet().iterator(); it.hasNext();) {
                String headerName = (String) headerMap.get(it.next());
                c1 = new PdfPCell(new Phrase(headerName, headerFont));
                c1.setBackgroundColor(BaseColor.LIGHT_GRAY);
                table.addCell(c1);
            }
            table.setHeaderRows(1);
            table = custDAO.creadPDFTable(query, table);
            document.add(table);
            document.addAuthor(userViewModel.getUsername());
            document.addCreationDate();
            document.addCreator("POC");
            document.close();
            response.flushBuffer();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        }

when i remove the following lines from the code i don't get any exception but when i include these lines i get java.lang.IllegalStateException: getOutputStream() has already been called for this response exception.

 Image image = Image.getInstance(path+"/"+"images/abi.png");
                image.setAbsolutePosition(40f, 770f);
                image.scaleAbsolute(70f, 50f);
                document.add(image);
                response.flushBuffer();

i use the above code to add image to pdf file(itextpdflibrary)

what could be the solution for this, please advise me as i have been facing this for a long time.

Regards.

Was it helpful?

Solution 2

i had a separate method for adding image in to document and called it that is all about to solve my problem.

thanks for all your help.

Image image = Image.getInstance(path+"/"+"images/abi.png");
                image.setAbsolutePosition(40f, 770f);
                image.scaleAbsolute(70f, 50f);
                document.add(image);
                response.flushBuffer();

OTHER TIPS

Maybe a solution it will be:

Try to put all document stuff at end of try:

try{
// create all object here, but don't add it to document yet.

// add all to document:
PdfWriter.getInstance(document,response.getOutputStream()); 
document.open();
addTitlePage(document, reportName);
document.add(image);
document.add(table);
document.addAuthor(userViewModel.getUsername());
document.addCreationDate();
document.addCreator("POC");
document.close();
response.flushBuffer();

} catch (Exception ex) {
  ex.printStackTrace();
}

No other idea how to escape from the exception

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top