【问题标题】:Generate PDF from gsp page从 gsp 页面生成 PDF
【发布时间】:2018-05-25 10:16:37
【问题描述】:

我正在使用 grails 2.5.2。 我创建了一个表格,显示了从数据库到 gsp 页面的所有数据,现在我需要通过单击按钮将显示的数据保存为 pdf 格式。将它们显示为 PDF 并将其保存到我的最佳方法是什么目录。请帮忙

【问题讨论】:

标签: pdf grails


【解决方案1】:

您可以使用itext 使用以下代码将 HTML 转换为 pdf:

public void createPdf(HttpServletResponse response, String args, String css, String pdfTitle) {
    response.setContentType("application/force-download")
    response.setHeader("Content-Disposition", "attachment;filename=${pdfTitle}.pdf")
    Document document = new Document()
    Rectangle one = new Rectangle(900, 600)
    document.setPageSize(one)
    PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream())
    document.open()
    ByteArrayInputStream bis = new ByteArrayInputStream(args.toString().getBytes())
    ByteArrayInputStream cis = new ByteArrayInputStream(css.toString().getBytes())
    XMLWorkerHelper.getInstance().parseXHtml(writer, document, bis, cis)
    document.close()
}

【讨论】:

    【解决方案2】:

    虽然回答这个问题晚了,但是看看 grails 导出插件。如果你想将你的数据导出到 excel 和 pdf,它会很有用(只有在没有预定义的模板导出时才有用)。

    【讨论】:

    • 感谢您的回答,我使用了一些预定义的模板。所以我确实使用了 itext 。
    【解决方案3】:

    从 itext 中得到想法。使用 itext 2.1.7 并将所有值从控制器方法发布到 pdf。使用图像作为背景以及段落和短语来显示数据库中的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-13
      相关资源
      最近更新 更多