【问题标题】:PDF Generator using iText and java使用 iText 和 java 的 PDF 生成器
【发布时间】:2013-09-23 10:27:39
【问题描述】:

我需要使用 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();
}

【问题讨论】:

  • 在什么方面不起作用?您收到错误消息吗?
  • @Klas:我既没有收到错误信息,也没有收到 pdf 文件。
  • 与问题无关:我发现 Jasper 报告比将 PDF 报告与 iText 拼凑在一起要好得多且更易于使用。你应该调查一下。
  • 我假设您已经尝试将 PDF 保存到磁盘。它有效吗?只需确保错误出现在下载部分即可。
  • @Winzu:保存是个问题。我没有弹出保存或打开文件的窗口...

标签: java ajax pdf-generation itext


【解决方案1】:

我认为您不能仅通过 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();
});

【讨论】:

  • @Winzu:我正在使用一个按钮来生成 pdf,因为我在页面中有一个报告,其中我提供了以 pdf 格式查看报告的选项。我将如何使用 .submit()???
  • 编辑了我的答案,也许现在更有意义了。您可能有一个 ID 或其他东西来识别您的报告。通过javascript设置输入。然后你只是提交一个表单。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-13
  • 2013-06-05
  • 2016-09-02
  • 1970-01-01
  • 2022-11-10
  • 2011-08-04
  • 2014-09-01
相关资源
最近更新 更多