5
votes

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.

2

2 Answers

8
votes

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.

0
votes

In Your grails-app>BuildConfig>add the following

compile ':pdf:0.6'