从 gsp 页面生成 PDF
Generate PDF from gsp page
我正在使用 grails 2.5.2。
我创建了一个 table,它显示了从数据库到 gsp 页面的所有数据,现在我需要使用按钮 click.What 将显示的数据保存为 pdf 格式,这将是将它们显示为PDF 并将其保存到我的目录。请帮忙
从 itext 得到灵感。使用 itext 2.1.7 并将所有值从控制器方法发布到 pdf。使用图像作为背景以及段落和短语来显示数据库中的值。
虽然回答这个问题晚了,但如果您想将数据导出到 excel 和 pdf,请查看 grails export plugin.It 将很有用(只有在没有预先准备好的情况下才有用已定义要导出的模板)。
您可以使用 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()
}
我正在使用 grails 2.5.2。 我创建了一个 table,它显示了从数据库到 gsp 页面的所有数据,现在我需要使用按钮 click.What 将显示的数据保存为 pdf 格式,这将是将它们显示为PDF 并将其保存到我的目录。请帮忙
从 itext 得到灵感。使用 itext 2.1.7 并将所有值从控制器方法发布到 pdf。使用图像作为背景以及段落和短语来显示数据库中的值。
虽然回答这个问题晚了,但如果您想将数据导出到 excel 和 pdf,请查看 grails export plugin.It 将很有用(只有在没有预先准备好的情况下才有用已定义要导出的模板)。
您可以使用 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()
}