This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

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

 
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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • 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.
 
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 VI Editor Ubuntu
  • 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.
 
The world's cheapest jedi mind trick: "Aw c'mon, why not read this tiny ad?"
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!