java spring arch、JSP、jQuery、Ajaxを使用してPDFファイルを生成しています。 PDFファイルが生成されますが、正しく破損していないエラーファイルが破損しています。エンティティごとにデータベースからPDFセル値を設定する方法の問題に直面しています。ここでjava springでpdfファイルを生成する際にエラーが発生する
は、私はSQLを使用したデータ、すなわち学生名、クラス名、マークを取得する必要があり、この中のコード
service implementation
です。すべての変数は、エンティティではありますが、私はエンティティを使用して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
を修正してください私たちは会計年度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 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);
}
});
}
あなたは何を得ようとしていますか、どのようなエラーがありましたか?あなたのpdf生成方法はうまくいきます。あなたのレスポンスにpdfファイルを追加しようとしているのか、ディスクにダウンロードするだけですか? @bkumar – krezus
データベースからpdfcellの名前とデータを取得していません – bkumar
私が理解したところでは、テーブルを記入するためのデータを提供しようとしています。私はあなたの** pdfList **の代わりに模擬データを与えました。リポジトリ 'List pdfList = resultDeclarationRepository.findByFinancialYearIdAndClassId(financialYearId、classId);から取得しました。 –
krezus