我正在使用 java spring arch、JSP、jQuery 和 Ajax 生成 PDF 文件。生成 PDF 文件但给出错误 - 文件已损坏,未正确解码。我面临如何按实体从数据库中设置 PDF 单元格值的问题。
这是代码
服务实现
在此我必须获取数据,即学生姓名、类(class)名称、使用 SQL 的标记。所有变量都在实体中,我需要使用实体在 pdf 单元格中设置数据,似乎我在这里遗漏了一些东西,请纠正我
@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
在此我们通过服务层的 spring data Repository 根据财政年度 id 和 class id 获取数据
@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文件(jQuery也在用)
<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);
}
});
}
最佳答案
您的 Controller 代码有问题。您没有写文件作为回应。
pdf 文件可能会在服务器上生成,但不会在响应中给出。我相信 "Student Result Details" 是创建文件的名称。
在您的 Controller 代码中执行如下操作:
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);
}
希望对您有所帮助。享受:)
关于java - 在java spring中生成pdf文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47881419/