Skip to content

Instantly share code, notes, and snippets.

@staticnull
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save staticnull/27a4a1aa472738181faf to your computer and use it in GitHub Desktop.
Save staticnull/27a4a1aa472738181faf to your computer and use it in GitHub Desktop.
Report Controller
package demo
class ReportController {
/**
* Generate PDF report
*
* phantomjs ./scripts/phantomjs/rasterizestdout.js <url of report> <name>.pdf <url of footer>
*
* http://stackoverflow.com/questions/12403817/phantomjs-pipe-input/17286691#17286691
* Need this link in rasterize.js:
* page.render('/dev/stdout', { format: 'pdf' });
*/
def index(Long profileId, Long evaluationId, Long groupId, String reportName) {
String serverURL = grailsApplication.config.grails.serverURL
String phantomPDFScript = grailsApplication.config.grails.phantomPDFScript
log.trace "Configured serverURL: ${serverURL}"
def reportUrl = "${serverURL}/index.html#/profile/${profileId}/evaluation/${evaluationId}/group/${groupId}/report"
log.trace "Report URL: ${reportUrl}"
def footImageUrl = "${serverURL}/images/footer-no-logo.jpg"
log.trace "Footer Image URL: ${footImageUrl}"
List cmd = ['/usr/local/bin/phantomjs', "${phantomPDFScript}", "${reportUrl}/${reportName}", "${reportName}.pdf", "${footImageUrl}"]
log.trace "PhantomJS command: ${cmd}"
// https://grails.org/FAQ#Q: Can I use the render method to return a binary file to the client?
response.contentType = "application/pdf"
def process = cmd.execute()
process.consumeProcessOutput(response.outputStream, response.outputStream)
process.waitFor()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment