aspose file tools*
The moose likes Other Application Frameworks and the fly likes Cannot forward after response has been committed? PDF save in Browser? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "Cannot forward after response has been committed? PDF save in Browser?" Watch "Cannot forward after response has been committed? PDF save in Browser?" New topic
Author

Cannot forward after response has been committed? PDF save in Browser?

Tim Nees
Greenhorn

Joined: Dec 26, 2007
Posts: 4
Hi,

i write an application in which a pdf is generated and send to the browser.
The PDF is generated with itext. Generate the pdf and save it works fine, but the following error message is thrown:



My code for the generate and forward of the pdf follows here:


public class MakePDF extends AbstractPdfView{

@Override
protected void buildPdfDocument(Map model, Document document, PdfWriter writer, HttpServletRequest request, HttpServletResponse response) throws Exception {

String linebreak = new String("\n");
Examination_data examination_data = null;
Patient_data patient_data = null;

String filename;

ByteArrayOutputStream baos = new ByteArrayOutputStream();

patient_data = (Patient_data) model.get("patient_data");
examination_data = (Examination_data) model.get("examination_data");

PdfWriter.getInstance(document, baos);

response.setContentType("application/pdf");

filename = (String) examination_data.getFilename();

String split[] = filename.split("\\.");

if (split.length == 1){
filename = filename + ".pdf";
}
else if (split.length == 2){

if (!split[1].equals("pdf")){
split[1] = ".pdf";
filename = split[0] + split[1];
}

}

response.setHeader( "Content-disposition", "attachment; filename=" + filename + "");

document.open();;

/**
* In das PDF Dokument einfuegen
*/
// Name
document.add(new Paragraph(linebreak));
document.add(new Paragraph("Name: "));
document.add(new Paragraph(patient_data.getName()));

// Firstname
document.add(new Paragraph(linebreak));
document.add(new Paragraph("Firstname: "));
document.add(new Paragraph(patient_data.getFirstname()));

// Date of Birth
document.add(new Paragraph(linebreak));
document.add(new Paragraph("Date of birth: "));
document.add(new Paragraph("test"));

// Gender
document.add(new Paragraph(linebreak));
document.add(new Paragraph("Gender: "));
document.add(new Paragraph(patient_data.getGender()));

// Bodysize
document.add(new Paragraph(linebreak));
document.add(new Paragraph("Bodysize: "));
document.add(new Paragraph(patient_data.getBodysize()));

// Weight
document.add(new Paragraph(linebreak));
document.add(new Paragraph("Weight: "));
document.add(new Paragraph(patient_data.getWeight()));
document.add(new Paragraph(linebreak));

// Indication
document.add(new Paragraph(linebreak));
document.add(new Paragraph("Indication:"));
document.add(new Paragraph(linebreak));
document.add(new Paragraph(examination_data.getIndication()));

document.add(new Paragraph(linebreak));
document.add(new Paragraph(linebreak));

// Finding
document.add(new Paragraph(linebreak));
document.add(new Paragraph("Finding:"));
document.add(new Paragraph(linebreak));
document.add(new Paragraph(examination_data.getFinding()));

document.add(new Paragraph(linebreak));
document.add(new Paragraph(linebreak));

// Comment
document.add(new Paragraph(linebreak));
document.add(new Paragraph("Comment:"));
document.add(new Paragraph(linebreak));
document.add(new Paragraph(examination_data.getComment()));

document.add(new Paragraph(linebreak));
document.add(new Paragraph(linebreak));

//Diagnosis
document.add(new Paragraph(linebreak));
document.add(new Paragraph("Diagnosis:"));
document.add(new Paragraph(linebreak));
document.add(new Paragraph(examination_data.getDiagnostics()));

document.close();

response.setContentLength(baos.size());

ServletOutputStream out = response.getOutputStream();

baos.writeTo(out);

out.flush();

}

}


Does anybody have an advice or knows a workaround for this error?

Thanks

Tim
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

I'm just curious. Are you using Spring ? If so, you should not use the response to write anything to it. The iText document will be output to the response automatically by Spring (which causes an IllegalStateException). Just write to the document. The response should only be used to set cookies or http headers.


[My Blog]
All roads lead to JavaRanch
Radhakrishna Bhat
Greenhorn

Joined: Dec 26, 2007
Posts: 10
You cannot forward after response has been commited.

That illegalStateException is because of that
Tim Nees
Greenhorn

Joined: Dec 26, 2007
Posts: 4
Hi Christophe,

thanks for your post.

Ok, if Spring manages the output of the PDF, which prerequest must be
made, that Spring do this?

Thanks

Tim
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

This would better be discussed in the Spring forum. If you don't know how to use Spring MVC views, take a look here. About the AbstractPdfView, here.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cannot forward after response has been committed? PDF save in Browser?