In my business web site, I have array with datas and I want a way to publish it in severeal format.
To achieve this, it exists the Grails export plugin.Here is what you will have under your array :
This tutorial will explain how to quickly use the export plugin and problems I met.
Plugin install
To install, the plugin I added in BuildConfig.groovy :
repositories { ... mavenRepo "http://repo.grails.org/grails/core" ... } plugins { ... compile ":export:1.6" ... }
If you don't add, the Maven Repositories, you will have this error :
Resolve error obtaining dependencies: Could not find artifact org.odftoolkit:odfdom-java:jar:0.8.5 in grailsCentral (http://repo.grails.org/grails/plugins) (Use --stacktrace to see the full trace)
Error |
Modify your GSP
<head> ... <r:require module="export"/> ... </head> <body> <export:formats /> </body>
The export tag will display icons and the require will had required css.
If you clic on pdf button,
Notes :
- You can put the export tag where you want in the GSP
- You can specify format you want :
<export:formats formats="['csv', 'excel', 'ods', 'pdf', 'rtf', 'xml']" />
Modify your controller
Here is my controller :class ToolsController { // Export service provided by Export plugin def exportService def grailsApplication //inject GrailsApplication def index() { if(!params.max) { params.max = 10 } if ((params.extension != null)) { log.info(params.get('zest')) def format=params.extension if ("xls".equals(params.extension)) { format="excel" } if(format && format != "html"){ response.contentType = grailsApplication.config.grails.mime.types[format] response.setHeader("Content-disposition", "attachment; filename=check.${params.extension}") List fields = ["machineName", "fileName", "confServerName"] Map labels = ["machineName": "Nom de machine", "fileName": "Nom de fichier", "confServerName":"Valeur du ServerName"] Map formatters = new HashMap() Map parameters = new HashMap() exportService.export(format, response.outputStream,Check.list(params), fields, labels, formatters, parameters) } } } }
I had a Check domain class with 3 fields : machineName, fileName, confServerName.These fields will be display in the pdf
Notes :
- I had some problem with params.format values.In my case, it was always null and I don't know why.That's why my code is not exactly the same of the documentation
- You can also format text and adjust column size with formatters and parameters.See documentation for more details.
If you want more details about plugin installation, you could see the documentation : http://grails.org/plugin/export
Here is my Grails configuration :
- Grails 2.3.11
- Plugin ; runtime ":resources:1.2.1"