2013-09-23 84 views
0

我需要使用iText生成PDF,同時使用Ajax。我已經給出了一個按鈕,當點擊時,我必須得到一個彈出窗口來保存PDF文件,因爲我們通常會得到很多可下載的文件。我在代碼中找不到任何錯誤,請查看代碼並幫助我。我必須打印一個簡單的表格,其中包含行和列,但我無法做到。使用iText和java的PDF生成器

阿賈克斯編碼:

function getFocusedPDF(){ 
    alert("Inside create PDF ajax"); 
    $.ajax({ 
     url : '/PreTestWeb/getFocusedPDF', 
     type : 'get', 
     dataType : 'json', 
     contentType : 'application/json', 

     success : function(map) { 
      console.log(map); 
     }, 

     error : function(map) { 
      alert(map); 
      alert("error occured!!!"); 
     }, 

    }); 


} 

家庭控制器:

@RequestMapping(value = "/getFocusedPDF", method = RequestMethod.GET) 
public void getRecentFocusGrpData(HttpServletRequest req, HttpServletResponse res) throws IOException, DocumentException { 
    String contType="application/pdf"; 
    res.setContentType(contType); 
    Gson json = new Gson(); 
    System.out.println("Inside home ctrlr"); 
    PdfGenerator pdf=new PdfGenerator(); 
    System.out.println("==== Before ==="); 

    byte[] b=pdf.createFirstTable(); 

    System.out.println("==== After ==="); 

    res.setHeader("Content-Disposition", 
      "attachment; filename=pdf.pdf"); 
    res.setContentLength(b.length); 
    res.getOutputStream().write(b); 
    res.getOutputStream().flush(); 
    res.getOutputStream().close(); 
    System.out.println("Last line in home ctrlr pdf generation"); 
} 

createFirstTable:(方法)

public static byte[] createFirstTable() throws DocumentException, FileNotFoundException { 
    System.out.println("=========Inside pdf generator =========="); 
    // a table with three columns 
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(1024);  
    Document document=new Document(); 
    PdfWriter writer = PdfWriter.getInstance(document, outputStream); 
    writer.setCloseStream(false);  
    document.open(); 
    PdfPTable table = new PdfPTable(2);// new PdfTable(periodList.size() + 1); 
    // the cell object 
    PdfPCell cell = new PdfPCell(new Paragraph ("Merry Moore")); 
    cell.setColspan(2); 
    table.addCell(cell); 
    System.out.println("Added new paragraph"); 
    // now we add a cell with rowspan 2 
    cell = new PdfPCell(new Phrase("Cell with rowspan 2")); 
    cell.setColspan(2); 
    table.addCell(cell); 
    System.out.println("Added new cell"); 
    // we add the four remaining cells with addCell() 
    table.addCell("row 1; cell 1"); 
    table.addCell("row 1; cell 2"); 
    table.addCell("row 2; cell 1"); 
    table.addCell("row 2; cell 2"); 
    document.add(table); 
    System.out.println("Added table to document"); 
    document.close(); 
    return outputStream.toByteArray(); 
} 
+0

以什麼方式不工作?你有錯誤信息嗎? –

+0

@ Klas:我既沒有收到錯誤也沒有收到PDF文件。 – madhu

+0

與這個問題無關:我覺得Jasper的報告比使用iText將PDF報告拼湊起來要好得多,也更容易使用。你應該看看它。 –

回答

0

我不認爲你只能通過Ajax調用來做到這一點。 Ajax只能以文本形式接收響應。

如果你想提示下載,我做的事情是這樣的,以填補可能隱藏表單,並通過AJAX提交:

<form id="form" action="/downloadPDF.do" method="POST"> 
    <fieldset> 
     <label for="something">Something :</label> 
     <input type="text" name="something" id="something"/> 
    </fieldset> 

$("#button").click(function(e) { 
    e.preventDefault(); 
    $('#form').submit(); 
}); 
+0

@ Winzu:我使用一個按鈕來生成pdf,因爲我有一個報告在其中我給出了一個選項,以PDF格式查看報告的頁面。我將如何使用.submit()? – madhu

+0

編輯我的答案,也許現在更有意義。你可能有一個ID或什麼東西來識別你的報告。通過javascript設置輸入。然後你只是提交一個表單。 – Winzu