Friday, November 9, 2012

Stylesheets with Pentaho Report Designer


Stylesheets with Pentaho Report Designer

Stylesheets are the second big feature being integrated into PRD this year (the other one being crosstabs). I submitted a feature request for stylesheets some months ago (PRD-3823, PRD3834), simply because working on big projects requires stylesheets: You don’t want to waste hours changing every single report in case your client wants to have a style amended; you don’t want to recreate styles over and over again when you create new reports etc. I did not expect this feature to arrive anytime soon, so, quite surprisingly, as of today they are available in the latest trunk snapshot even with full CSS3 support (download here) (posted by Thomas here)! My very simple sample report can be downloaded here.
It is possible to store styles internally or externally. External stylesheets will have the extension .prptstyle.
To me stylesheets are an import part of report design and I am sure many other users will welcome this new feature. So let’s have a look at how to create and use stylesheets in PRD:

Once you have the trunk snapshot downloaded (note: do not use for production!) open PRD and create a new report, add a query and a few fields:



How to create internal styles

If you want to use internal styles, choose Format > Edit Style Definition.

This option is only useful when you intend to create a specific design for one specific report. I highly recommend you make use of an external stylesheet for bigger projects, which we will discuss next.

How to create an external stylesheet

In this case we want to create an external stylesheet, choose Window > Style Definition Editor

Make sure you save the external stylesheet straight away and while you are working:


Add a new style rule:


Note: The name of the style-rule is very important, it’s not just an arbitrary name!

Examples:

Style Rule NameExplanation
label[name="title"]The style will be automatically applied to all labels with the name title. The title for the label element can be defined in the Attributes pane:
group-header label[name="title"]This style will only be applied to a label with the name title in the group header.
report-header band[name=”subtitle”] labelThis style will only be applied to a label which is located in the band of the subtitle.
details-header labelThis style will be applied to all labels in the details header
itemband text-fieldThis style will be applied to all text fields in the details band
number-field[field="Sales"]This style will be applied to all number-fields which represent the data field Sales:
#headerThis CSS id selector style (more info here) will be applied to a unique element with the id in the Attributes pane set to ‘header’:
Note: Do not prefix the id with a # in the attribute value!
.centerThis CSS class selector style  (more info here) will be applied to multiple elements which have the style-class in the Attributes pane set to center:
Note: You must not prefix center with a dot in the attribute value!
.label.centerThis CSS class selector style  (more info here) will be applied to multiple elements which have the style-class in the Attributes pane set to center and are labels.
Note: The attribute value has to be set to only “center”!
report-footer label.noticeThis style will be applied to the label element with the class attribute set to notice in the report footer.


As you can see, the style rule name is a combination of various components.

Please find below a list of important report bands:
report-header
page-header
group-header
details-header
itemband
no-data-band
details-footer
page-footer
group-footer
report-footer
watermark

Important report elements:
label
text-field
number-field
date-field
message
band
sub-report

How to reference an external stylesheet

In the Structure pane select Master Report.
In the Attributes pane click the select button for the style-sheet-reference:


Click the Select button again:


Next set the Filter to Resources and then select your stylesheet. Click OK:


How to assign styles to elements

Only if you used CSS class and id selector compliant style rule names, you must specify them
respectively in the style-class and id attributes in the Attributes pane.
Specifying a class:


Specifying an id:


Note: Only specify the name without the dot or hash prefix!

Here a report with various styles applied (not only id and class selectors):


Working with the stylesheet XML file

If you know what you are doing, you can directly open the *.prptstyle file in your favourite text editor and amend it there:

Note: You must be familiar with XML and must understand the structure of the stylesheet. This practice is not recommended for anyone not familiar with it.

8 comments:

  1. Hi Diethar, congratulations form the post. I'm very interested in your post, but i've tried to do the same as you, but i had a error. and wanted to ask you a question. have you managed to publish the report and run it on pentaho 4.8?.

    thanks,

    ReplyDelete
    Replies
    1. Thanks a lot Pedro for your feedback. I don't think this currently works on BA Server 4.8 (unless you change the libraries). This feature was only added to the PRD development trunk a short time ago, so you will have to wait some time until it is available on the BA Server.

      Delete
  2. Hello Diethard,
    Your post is great, I was looking for the solution for a long time. But in my PRD there is no option available i.e. Format > Edit Style Definition and Window > Style Definition Editor.
    Can you please tell me how to make these options visible.
    Thanks in advance.
    Rahul Tiwari

    ReplyDelete
    Replies
    1. Did you download the latest trunk snapshot as mentioned in the beginning of the blog post?

      Delete
  3. Hy,

    I tried your tuto and it works almost perfectly.
    My problem is when I choose text/html as format. Your rule style are not working anymore.
    Is it normal

    ReplyDelete
    Replies
    1. It should work for most output formats, definitely HTML. Unless there is a bug somewhere that is.

      Delete
    2. We tried this with Pentaho 5.4.0.1-130
      Your guide works nice so far, but it doesnt work for text/html, as Florent Thomas mentioned already. Is there a workaround? like to set !important or something like that?

      Delete
    3. Try to log a jira case on jira.pentaho.com.

      Delete