This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Tim Nees
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Radhakrishna Bhat
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot forward after response has been commited.

That illegalStateException is because of that
 
Tim Nees
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic