How to get same styles in DynamicReports and Jaspe

2019-09-05 08:16发布

问题:

This question already has an answer here:

  • External Styles in JasperReports 3 answers

I would like to found best solution for having same styles for dynamicReports and jasperReports. I started to use dynamicReports, however I found it hard to create unusual reports there. It was much easier to do that in iReport graphical builder. So now I have 50% reports which runs on Dynamic and 50% on Jasper.

Now I can't resolve style problem. I've created styles, fonts etc in Template class and used it for DynamicReports. After some magic like:

.setName("cellCenterStyle");
template().setColumnStyle(cellStyle)

I was able to use them for jasper, but this is not a full solution as some options still does not work, for example:

.setDetailEvenRowStyle(Template.evenIntensivStyle)

is working with dynamic, but jasper ignores is.

So, the question:

Could I use same styles for dynamic and jasper with clear structure and without needless "magic". I would prefer solution like *.css styles, but any help would be appreciated.

回答1:

Use a parameter to reference the relative (or absolute) path to the styles file. The main report, for example, might contain:

<template><![CDATA[$P{P_REPORT_STYLES_PATH} + "FontStyle.jrtx"]]></template>
<parameter name="P_REPORT_BASE" class="java.lang.String">
    <defaultValueExpression><![CDATA[""]]></defaultValueExpression>
</parameter>
<parameter name="P_REPORT_RESOURCES_PATH" class="java.lang.String">
    <defaultValueExpression><![CDATA[$P{P_REPORT_BASE} + "resources/"]]></defaultValueExpression>
</parameter>
<parameter name="P_REPORT_STYLES_PATH" class="java.lang.String">
    <defaultValueExpression><![CDATA[$P{P_REPORT_RESOURCES_PATH} + "styles/"]]></defaultValueExpression>
</parameter>

This allows the report in iReport (or Jaspersoft Studio) to reference a relative path to the report styles, such as:

resources/styles/FontStyle.jrtx

This path is relative to the report's .jasper file location.

Then, in DynamicJasper, you can pass in a report parameter. The API call in DynamicJasper might not match the API call for the following JasperReports Library call, but it should be sufficiently similar to get the idea across:

    reportParameters.put("P_REPORT_BASE", "/home/user/dev/reports/workspace");
    JasperFillManager.fillReport(..., reportParameters, dataSource);

Assuming that /home/user/dev/reports/workspace is the location for the .jasper files as well as the workspace location for iReport/Jaspersoft Studio, then both DynamicJasper and the report development environment will find and use the same styles file.

The full path to the styles file, in this example, would be:

/home/user/dev/reports/workspace/resources/styles/FontStyle.jrtx

Your directory paths, of course, will differ.


At this point, you can reference the same styles in both environments. See also:

  • http://community.jaspersoft.com/wiki/styles-template
  • http://dynamicjasper.com/category/docs/howto/styles-templates/
  • https://stackoverflow.com/a/3853401/59087