6.9. Script Reports

In this section we give you a short introduction to script reports. A detailed documentation on scripts and script reports will be given in the ReportServer scripting guide. This section builds upon the introduction of scripts given in Chapters 12. and 13. On a first read it might thus be advisable to skip this section.

Script reports allow to create complex and interactive reports. Moreover, they are suited for the generation of documentation reports that directly process metadata from ReportServer. So, for instance, the report documentation supplied with the demo data has been realized in form of a script report. To run a script report you only need a script. Create the following Hello World script in the directory bin/tmp (in the File system).

"Hello World"

Now, in the Administration module switch to Report management, create a new script re- port and assign the newly created script. If you run the report, you will see the output "Hello World". Note that for the execution of a script report, the user does not only need to have the (execute) right to execute the report, but also needs execute rights on the underlying script. Script reports will be displayed in an IFrame by default, and therefore, the HTML output is appropriate. In the following we will accordingly give our "Hello World" script a makeover.

Basically, you could simply return HTML. Instead of displaying "Hello World", for instance

"<html><body><h1>Hello World</h1></body></html>"

In the following we will use Groovy's Markup Builder to create HTML comfortably:

import groovy.xml.*
def writer = new StringWriter()

new MarkupBuilder(writer).html { 
	head {
		title( "Hello World" )
	}
	body {
		h1("Hello World")
		p("This is a hello world script") 
		p("The time is: " + new Date())
	} 
}
writer.toString()
6.9.1. Arguments

While configuring the script report you can add arguments to it that you can process via the variable args just like command line arguments.

import groovy.xml.*
def writer = new StringWriter()

new MarkupBuilder(writer).html { 
	head {
		title ( "Hello World" )
	}
	body {
		h1("Hello World")
		p("This is a hello world script") 
		p("The time is: " + new Date()) 
		p("Arguments:" + args.join(", "))
	} 
}
writer.toString()
6.9.2. Parameters

Parameters specified at the report will be passed to the script via the parameterMap. If there is, for instance, the parameter with the key param, you can access it by using the following statement.

parameterMap['param']
6.9.3. Output Formats

Beside arguments and parameters you can define multiple output formats with the report. They can be entered separated by commas, e.g. "HTML,PDF,DOCX". If you now execute the report, you will see that the output formats were changed to export options. In the script itself, you can query the output format by using the variable outputFormat. The output format for preview is "preview":

import groovy.xml.*
def writer = new StringWriter()

new MarkupBuilder(writer).html { 
	head {
		title ( "Hello World" )
	}
	body {
		h1("Hello World")
		p("This is a hello world script") 
		p("The time is: " + new Date()) 
		p("Arguments:" + args.join(", "))
		p("Output format:" + outputFormat)
	} 
}
writer.toString()

By using the open source library Flying-Saucer you can easily create PDF documents from HTML. ReportServer supports you here by providing a pre-set renderer for PDF creation. In the following example a PDF object will be returned provided you have selected pdf (please ensure to use the format in lower case letters inside the script).

The script report below supports PDF, HTML and DOCX outputs.

import groovy.xml.*
def writer = new StringWriter()

new MarkupBuilder(writer).html { 
	head {
		title ( "Hello World" )
	}
	body {
		h1("Hello World")
		p("This is a hello world script") 
		p("The time is: " + new Date()) 
		p("Arguments:" + args.join(", "))
		p("Output format:" + outputFormat)
	} 
}
if("pdf".equals(outputFormat.trim()))
	return renderer.get("pdf").render(writer.toString())
if("html".equals(outputFormat.trim()))
	return renderer.get("html").render(writer.toString())
if("docx".equals(outputFormat.trim()))
	return renderer.get("docx").render(writer.toString())
writer.toString()
6.9.4. Datasources

Beside parameters, arguments and output formats you can also add a datasource to a script. The datasource object will be passed to the script via the "connection" variable. The following example uses the internal demo data (refer to Section ) to present a customer list.

import groovy.xml.* 
import groovy.sql.Sql

def writer = new StringWriter()

new MarkupBuilder(writer).html { 
	head {
		title ( "Hello World" )
	}
	body {
		h1("Hello World")
		p("This is a hello world script") 
		p("The current time is: " + new Date()) 
		p("Arguments:" + args.join(",")) 
		p("Output format:" + outputFormat) h1("Customers: ")
		ul {
			new Sql(connection).eachRow("SELECT DISTINCT CUS_CONTACTLASTNAME 
														FROM T_AGG_CUSTOMER ORDER BY 1 ASC" ){
				li(it.CUS_CONTACTLASTNAME)
			}
		}
	}
}
if("pdf".equals(outputFormat.trim()))
	return renderer.get("pdf").render(writer.toString())
if("html".equals(outputFormat.trim()))
	return renderer.get("html").render(writer.toString())
if("docx".equals(outputFormat.trim()))
	return renderer.get("docx").render(writer.toString())
writer.toString()