2017-12-19 7 views
2

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); 
     } 
    }); 
} 
+0

あなたは何を得ようとしていますか、どのようなエラーがありましたか?あなたのpdf生成方法はうまくいきます。あなたのレスポンスにpdfファイルを追加しようとしているのか、ディスクにダウンロードするだけですか? @bkumar – krezus

+0

データベースからpdfcellの名前とデータを取得していません – bkumar

+0

私が理解したところでは、テーブルを記入するためのデータを提供しようとしています。私はあなたの** pdfList **の代わりに模擬データを与えました。リポジトリ 'List pdfList = resultDeclarationRepository.findByFinancialYearIdAndClassId(financialYearId、classId);から取得しました。 – krezus

答えて

1

春データリポジトリによるとクラスIDは、お使いのコントローラのコードに問題があります。あなたは応答としてファイルを書いていません。

pdfファイルがサーバーで生成されている可能性がありますが、それは応答として与えられません。私は信じている"学生の結果の詳細"は作成されたファイルの名前です。

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); 
} 

・ホープ、このヘルプ:あなたのコントローラのコードで

のような何かを行います。あなたは、ファイルを生成する前に、春のMVCでPDFファイルをダウンロードするにはSpring MVC PDF Downloadを参照することができますが、もう一つ注意すべきは、応答content-typeheader設定されている:)

+0

このコードはファイル出力ストリームと応答として動作しません。両方ともtry -catchメソッドに囲まれている必要がありますが、ランタイムエラーが表示されます – bkumar

+0

試してみると@bkumarありがとうございます。その場所。私はそれが動作するように例外をキャッチすることができると信じています。 –

0

をお楽しみください、私は中に直面してそれ以外の場合は、あなたがgarblendページを得ることができますこの質問:garblend page in spring mvc

関連する問題