How to create file on server using grails export plugin?

Grails export plugin is used for export functionality. Its supports several formats e.g., Excel, PDF, CSV, XML, etc.

Following is the simple code to download the file in PDF format using export plugin:

class BookController {

  def exportService

  def downloadBookList() {
    response.contentType = 'application/pdf'
    response.setHeader("Content-disposition", "attachment; filename=books.pdf")
    exportService.export(pdf, response.outputStream, Book.list(params), [:], [:])	
  }
}

When request comes to this action then a PDF will be downloaded on the user’s machine.

But what if I want to save this file on server for some purpose and don’t want the download facility, eg. if I want to sent this file via email as an attachment.

Export plugin needs output stream, it can be from response or from a file.

So above requirement can be achieved by the following code:

class BookController {

  def exportService

  def sendBookList() {
    File file = new File("/home/mkb/books.pdf")
    def fileOutputStream = new FileOutputStream(file)
    exportService.export(pdf, fileOutputStream, Book.list(params), [:], [:])
    ...
  }
}

This code create a pdf in the given path. Now you got the file do what ever you want with this file.

Hope this helps 🙂 .