0

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!

lucke84
  • 4,516
  • 3
  • 37
  • 58
DaveB
  • 1
  • 1
  • 1
  • Has `applicaion/pdf` been added as a MIME type as mentioned in the plugin docs (http://grails.org/plugin/export)? An error stacktrace will be helpful to see if you have faced any. Moreover, just as an option, if you need the whole GSP converted to PDF and not interested with any other MIME type conversion, I would suggest to use Grails Rendering Plugin which is pretty much simpler. – dmahapatro Apr 16 '13 at 17:13
  • Well, we also do some CSV exports as well, but this PDF export is what I really want to get working soon. Yes, app/pdf is registered MIME type. I am just wanting to know a little more about what the exportService expects in regard to List objects, Map formatters, Map parameters...I THINK I have the map I need, just not sure. Thanks for your input! – DaveB Apr 16 '13 at 23:44

1 Answers1

0

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, [:], [:])
}
  • Yes, it is PDF. I am not sure as to exactly what I need to pass as "list object", "map formatter", and "map parameters". I have pulled hair out today - finally have file save dialog that comes up and I can save the doc, but it seems I am missing some parameters that is being expected (as mentioned). I certainly appreciate the answers. The saga continues....guess you need to sweat it a little before you REALLY learn it - at least thats what I have heard...lol – DaveB Apr 16 '13 at 23:40
  • It seems that you can pass `map formatter` and `map parameters` as empty maps, the example use `[:]`. Regarding the list object, is a list of some domain class that will be in the file. –  Apr 16 '13 at 23:47
  • Also, can I dynamically create a 'labels' map dynamically, based upon my fields list? I think I do but not sure. – DaveB Apr 17 '13 at 13:26
  • Yes, you will have to use some reflection to check the properties of your domain class, but it's possible. Check [this example](http://stackoverflow.com/questions/4555150/retrieving-a-list-of-gorm-persistent-properties-for-a-domain). –  Apr 17 '13 at 13:35
  • Man, I am having some issues with this. Here's what I have: 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("pdf", response.outputStream, rows, headers, [:],[:]) ----- I tried to build this map of GORM persistent props, but no dice. Are there any other ways to do the dynamic labels prop? – DaveB Apr 17 '13 at 17:03
  • Update your question with the code that you tried to buid the labels map. –  Apr 17 '13 at 17:09
  • def pdf = { results-> def d = GrailsApplication.getDomainClass(Report.class) d.getDeclaredFields().each { println it.name } def newMap = [:] d.getPersistentProperties().each { newMap.put('What Key/Val Pair?') } – DaveB Apr 17 '13 at 18:20