简体   繁体   中英

Correct Implementation of Export Plugin for Grails to export a GSP as PDF

I am a fairly new developer in Grails using STS 3.2 (grails version 2.2.0) and I have an application where I want to export data from a GSP to PDF format. I have installed the Export 1.5 plugin and have a reportController with the following:

def pdf = { results->
    def table = results['tables'][params.reportNum.toInteger()]
    def headers = table?.getAt(0).collect{ it.key }
    def rows = table*.collect{ cleanNull(it.value.toString()) } //data  
    exportService.export("$params.renderAs", response.outputStream, headers, rows, parameters)
}

Can someone help me get this wired up correctly? I do know that the exportService expects this, but not sure if I have everything collected that I need to to get this to work:

export(String type, OutputStream outputStream, List objects, Map formatters, Map parameters) }

I hope I have been clear with my question...Thanks in advance!

The example of the plugin have params.format as the export type. What's the content of your params.renderAs ? It seems that the value must be one of the keys of grails.mime.types .

if(params?.format && params.format != "html"){ 
    response.contentType = grailsApplication.config.grails.mime.types[params.format]
    response.setHeader("Content-disposition", "attachment; filename=books.${params.extension}")

    exportService.export(params.format, response.outputStream,Book.list(params), [:], [:]) 

}

So in your case renderAs should have pdf .


Ok, so looking in the ExportService, and your code example, I think that the signature of method that you want to use is:

export(String type, OutputStream outputStream, List objects, List fields, Map labels, Map formatters, Map parameters)
  • objects: list of objects that will be exported
  • fields: name of fields, that exists in the objects that will be exported
  • labels: A map of fields and his labels
  • formatters: can be an empty map
  • parameters: can be an empty map

Considering a book domain class:

class Book {
    String title
    String author
}

You can create an action like:

def pdf() {
    List fields = ["author", "title"]
    Map labels = [author: "Author", title: "Title"]
    exportService.export(params.format, response.outputStream, Book.list(params), fields, labels, [:], [:])
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM