Have any questions?
+44 1234 567 890
5.1. Basic Script Reports
By default script reports are supposed to generate HTML. Thus, all we really need is to return HTML code as in the following example (note that we are using Groovy's multiline string feature here).
return """\
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
"""
Let us store this script as /bin/reports/report1.groovy. The next step is to define a script report. For this go to the report manager and create a new object of type script report. Provide a name and select the previously create script as script reference. After submitting the data you can execute the report as usual by simply double clicking on the item from the report manager. You should see that the report simply displays the content as defined by the HTML code. In contrast to other reports, there is no button for scheduling the report or exporting it. This is because we have not yet defined output formats for the report. For this go back to the script report in the report manager. Here you can define output Formats as a comma separated list. For example we can define
HTML, Plain
Submit the changes and reopen the report. You see that you now have two export options: HTML and Plain. However, both options produce the same, somewhat unexpected result. The browser displays the HTML code rather than the interpreted website. This is, because we have not specified the return type. For this, we need to return an object of type net.datenwerke.rs.core.service.reportmanager.engine.CompiledReport which besides the report's content contains information on, for example, the resulting mime type. Several predefined and simple to use objects are available:
- {net.datenwerke.rs.core.service.reportmanager.engine.basereports.CompiledTextReportImpl} For simple text documents.
- {net.datenwerke.rs.core.service.reportmanager.engine.basereports.CompiledHtmlReportImpl} For html documents.
- {net.datenwerke.rs.core.service.reportmanager.engine.basereports.CompiledDocxReport} For Microsoft Word documents.
- {net.datenwerke.rs.core.service.reportmanager.engine.basereports.CompiledXlsxReport} For Microsoft Excel documents.
- {net.datenwerke.rs.core.service.reportmanager.engine.basereports.CompiledJsonReport} For JSON documents.
- {net.datenwerke.rs.core.service.reportmanager.engine.basereports.CompiledCsvReport} For comma separated value documents.
- {net.datenwerke.rs.core.service.reportmanager.engine.basereports.CompiledPdfReport} For PDF documents.
Thus, to make the browser render the output we could adapt our above script to
import net.datenwerke.rs.core.service.reportmanager.engine.basereports.CompiledHtmlReportImpl
def report = """\
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
"""
return new CompiledHtmlReportImpl(report)
While the browser now renders the exported report properly, it also does so when using the export option Plain. To differentiate between the two output formats scripts have access to a predefined variable outputFormat. This variable contains the selected output format. Consider the following adaption
import net.datenwerke.rs.core.service.reportmanager.engine.basereports.CompiledHtmlReportImpl
def report = """\
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
"""
if(outputFormat == 'plain')
return 'Hello World'
return new CompiledHtmlReportImpl(report)
Note that, although the output format was defined as Plain, the script is given the output format in lower case and with leading and trailing whitespace removed. Now if you export the report to Plain you get the expected plain Hello World response, while an export to HTML will lead to a rendered Hello World response.