grailsgrails-2.0print-css

Render a GSP page to a file in a web-app subfolder


I need a really stupid thing but I'm stuck. I need basically to render a gsp page and save the rendering locally on server side inside a folder I created under web-app/.

Basically the output of this:

    render(view: "report-test") 

must be saved in a file as example report-test.html inside a folder locate in web-app/report/

any good advice?

thanks a lot

UPDATE - EXPLANATION Thanks a lot to everyone. Let me explain what I'm trying to do, hopefully there is a better solution. I bet there is, I'm still a newbie in grails. I'm trying to print a report using print-css and Price software that helps me to create a pdf. So my idea was to create the HTML file dynamically using a gsp, and followinf the print-css rules then save it locally on the server side then execute a command (with Price) that create my pdf file and return back the pdf file to the browser.

UPDATE I need to use css but not to be used inside the html but as part of Price software command. http://www.princexml.com/ So basically the gps rendered is an html without any css applied, then when I run the command to create the pdf I specify the css file to apply. As example:

     prince -s pdf-styles.css book.html builds/book.pdf

UPDATE/2 - CLOSE

thanks to shutgunNinja see his pseudo code in his post below, here the code I'm going to use:

class YourController {
   def printHtml() {
       render(view: "report-test")
   }
   def buildReport() {
        String basePath = applicationContext.getResource("/").getFile().toString()
        def url = new URL("http://localhost:8080/PrjName/report/printHtml)
        def data = url.getText()
        def file = new File("${basePath}/reportFolder/report.html")
        file.createNewFile()
        FileUtils.writeStringToFile(file, data)
    }
}

So as wrote before by shutgunNinja, I call buildReport() which call the URL to get the html file. I'd like to add few advices:


Solution

  • Okay, I think I understand what you're trying to do, and I'm going to try to explain in more detail what I've said in the comments. I'm not sure if this is the best approach for this, but it's what I would try given no other options.

    Requirement:

    Take the output of a Controller method (say, report()) which renders a GSP page (via, say, render(view: "report-test")) and save it as an HTML file on the server.

    Approach:

    Pseudocode:

    class YourController {
        def report() {
            // Handle input, generate data for populating page
            render(view: "report-test")
        }
        def buildReport() {
            // Indirectly call report(), passing whatever data is needed as URL parameters manually
            def url = new URL("/path/to/action?arg1=" + params.arg1)
            def data = url.getText()
            def file = new File("/web-app/report/report.html")
            file.createNewFile()
            FileUtils.writeStringToFile(file, data)
        }
    }
    

    Keep in mind, I haven't tested this code to verify that it works or not. Modifications will almost absolutely need to be done, but it should get the point across.

    EDIT: As the OP mentioned in their edits, whatever security system you're using for login filtering will need to be set to ignore report() so that you don't end up with a login screen. The same goes for any redirecting filters; this approach doesn't generally fare well with HTTP redirects or forwards, regardless of language or toolkit.