PDF生成器使用iText和java

时间:2013-09-23 10:27:40

标签: java ajax pdf-generation itext

我需要使用iText生成PDF,同时使用ajax。我给了一个按钮,当点击时,我必须弹出一个保存pdf文件的弹出窗口,因为我们通常会收到许多可下载的文件。我在代码中没有发现任何错误,请查看代码并帮助我。我必须打印一个包含行和列的简单表格,这是我无法做到的。

Ajax编码:

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

1 个答案:

答案 0 :(得分: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();
});