Reputation: 51
I am generating a PDF file using java spring arch, JSP, jQuery and Ajax. PDF file is generated but giving error -file damaged, not correctly decoded. I am facing issue in how to set in PDF cell values from database by entity.
Here is the code
service implementation
In this I have to get data, i.e. student name, class name, marks using SQL. All variables are there in entity, I need to set data in pdf cell using entity, seems I am missing out something here, please correct me
@Override
public Document getPdfResultDetails(Long financialYearId, Long classId) {
Document document =new Document(PageSize.A4);
try {
PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("Student Result Details"));
document.open();
document.add(new Paragraph("Student Exam Result Details ") );
PdfPTable table=new PdfPTable(5);
table.setWidthPercentage(100.0f);
table.setWidths(new float[] {3.0f, 2.0f, 2.0f, 2.0f, 1.0f});
table.setSpacingBefore(10f);
table.setSpacingAfter(10f);
float[] colWidth={2f,2f,2f};
PdfPCell studentNameList=new PdfPCell(new Paragraph("Student Name"));
PdfPCell financialYearList=new PdfPCell(new Paragraph("Financial year"));
PdfPCell marksObtainedList=new PdfPCell(new Paragraph("Marks Obtained"));
PdfPCell resultList=new PdfPCell(new Paragraph("Result"));
PdfPCell classNameList=new PdfPCell(new Paragraph("Class Name"));
table.addCell(studentNameList);
table.addCell(financialYearList);
table.addCell(marksObtainedList);
table.addCell(resultList);
table.addCell(classNameList);
List<ResultDeclarationDTO> resultDeclarationDTO=new ArrayList<ResultDeclarationDTO>();
List<AdmissionResultDetailEntity> pdfList=resultDeclarationRepository.findByFinancialYearIdAndClassId(financialYearId, classId);
if (pdfList==null)
return null;
for (AdmissionResultDetailEntity admissionResultDetailEntity : pdfList){
ResultDeclarationDTO resultExamDeclarationDTO=new ResultDeclarationDTO();
table.addCell(admissionResultDetailEntity.getObtainMarks()+"");
}
document.add(table);
document.close();
writer.close();
} catch (DocumentException e) {
// TODO: handle exception
e.printStackTrace();
}catch (FileNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
}
return document;
}}
controller class
in this we are getting data on the basis of financial year id and class id by spring data Repository in service layer
@RequestMapping(value = "/downloadStudentResult", method = RequestMethod.GET)
public ModelAndView downloadStudentResult(HttpServletResponse response,@RequestParam(name = "financialYearId", required = false) Long financialYearId,
@RequestParam(name = "classId", required = false) Long classId) {
try {
Document document=resultDeclarationService.getPdfResultDetails(financialYearId, classId);
response.setHeader("Content-disposition", "attachment; filename=StudentResult.pdf");
response.flushBuffer();
}
catch (Exception e) {
// TODO: handle exception
}
return new ModelAndView();
}
}
JSP file (jQuery is also being used)
<div align="center">
<h1>Students Result Document</h1>
<h3><a href="/downloadStudentResult">Download Students Result Document</a></h3>
</div>
$(document).ready(function(){
callDataTableFunction();
callPdfFileFunction();
});
function callPdfFileFunction(){
//$('#dataTableDiv').show();
$.ajax({
type: "POST",
url: "/downloadStudentResult",
processdata: true,
success: function(data)
{
createPdfFile(data);
},
error: function(data){
showErrorAlert("#showAlert", data);
}
});
}
Upvotes: 2
Views: 1316
Reputation: 1475
You can refer to Spring MVC PDF Download to download a pdf file in spring mvc, but one thing to note is set the response content-type
and header
before generating the file, otherwise you may get a garblend page as I faced with in this question: garblend page in spring mvc.
Upvotes: 0
Reputation: 359
There is a problem with your controller code. You are not writing file in response.
The pdf file may be generated at server but not given in response. I believe "Student Result Details" is the name of file created.
In your controller code do something like:
File pdfFile = new File(<path to pdf>);
response.setContentType("application/pdf");
response.setHeader("Content-disposition", "attachment; filename=StudentResult.pdf");
response.setContentLength((int) pdfFile.length());
FileInputStream fileInputStream = new FileInputStream(pdfFile);
OutputStream responseOutputStream = response.getOutputStream();
int bytes;
while ((bytes = fileInputStream.read()) != -1) {
responseOutputStream.write(bytes);
}
Hope this help. Enjoy :)
Upvotes: 1