Grails:从GSP生成PDF [英] Grails : Generating PDF from GSP

查看:127
本文介绍了Grails:从GSP生成PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的GSP网页有多个表格,还有其他一些HTML元素,例如输入文字栏位和标题< h1> < h2> 文本。



我想要在生成的文本中捕获所有这些信息。

到目前为止,我所看到的例子只能用于生成一个表格。



有没有一种快速的方法可以从已经在GSP中填充的所有数据生成PDF?



I使用Export插件。

  compile':export:1.6'

pre>

下面是我到目前为止试过的示例代码。



它绘制了一个带有标题的表格,并且没有数据在行中。

  def myPDFexport(){
String userNumber = params.UserInfoInstance;

UserInfo userObj = UserInfo.findByNumber(userNumber);

params.format =pdf;
params.extension =pdf
列表字段= [用户名,用户名,用户密码]
地图标签= [用户ID:用户ID,用户名:用户名,userPW:用户密码]

if(params?.format& amp; amp; params.format!=html){
response.contentType = grailsApplication .config.grails.mime.types [params.format]
response.setHeader(Content-disposition,attachment; filename = $ {woNumber}。$ {params.extension})
exportService .export(params.format,response.outputStream,userObj.list(params),fields,labels,[:],[:])
}
}

由于我是Grails的新手,并且也没有以前的PDF生成经验,所以我几乎失去了这一点。



如果可能,请为我提供一些示例代码。

解决方案

有很多解决方案来创建PDF。



作为第一个简单的方法,您应该查看 Grails渲染插件。您可以使用任何视图或模板生成pdf(或图像) 。



将插件添加到您的项目后,您可以将如下操作添加到您的控制器中:

  def createPdfReport = {
renderPdf(template:'/ templates / pdf / yourtemplate',model:yourmodel,filename:yourTitle)
}

它将使用指定的模板生成pdf并将其作为响应发送给客户端。



有关更多信息,请参阅插件文档。


My GSP page has multiple tables and also some other HTML elements like input text fields and header <h1> <h2> texts.

I want all this info to be captured in the generated text.

So far the examples i have seen is only good for generating one table.

Is there a quick way of generating the PDF from all the data that's already populated in GSP?

I am using the Export plugin.

compile ':export:1.6'

And below is the sample code i tried so far.

It draws one table with headers and no data in the rows.

def myPDFexport () {
        String userNumber = params.UserInfoInstance;

        UserInfo userObj = UserInfo.findByNumber(userNumber);

        params.format = "pdf";
        params.extension = "pdf"
        List fields = ["User ID", "User Name", "User Password"]
        Map labels = [userID: "User ID", userName: "User Name", userPW: "User Password"]

        if(params?.format && params.format != "html"){
            response.contentType = grailsApplication.config.grails.mime.types[params.format]
            response.setHeader("Content-disposition", "attachment; filename=${woNumber}.${params.extension}")
            exportService.export(params.format, response.outputStream, userObj.list(params), fields, labels, [:], [:])
        }
    }

I am almost lost on this as i am new to Grails and also have no previous experience with PDF generation.

If possible, please provide me with some sample code.

解决方案

There are a lot of solutions to create PDFs.

As a first and easy approach you should have a look at the Grails rendering plugin.

You could use any view or template to generate a pdf (or image).

After adding the plugin to your project you could add an action like this to your controller:

def createPdfReport = {
    renderPdf(template: '/templates/pdf/yourtemplate', model: yourmodel, filename: "yourTitle")
}

It will use the specified template to generate a pdf and send it as response to the client.

For further information have a look at the plugin documentation.

这篇关于Grails:从GSP生成PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆