I'd like to create a dynamic reporting webpage using JSP. Basically it should contain the following parts:
Instead of implementing everything myself, I'd like to use a java reporting library, so I created my report using JasperReports. The pdf output is really nice, but the html export of the report is not suitable for my purposes.
JasperReport's html export creates an html file with lots of hardwired code, and quite random configuration options. For instance it creates a table with white background by default (<table style="... bgcolor="white" ... ">
) which can be turned off using IS_WHITE_PAGE_BACKGROUND
option, on the other hand cellpadding="0" cellspacing="0" border="0"
are hardwired in the table
tag. It is also strange (and makes css styling difficult) that instead of span classes the html file contains <span style="font-family: sansserif; color: #000000; font-size: 10.0px;">
for all my fields.
Of course I can implement the html-output using JSP, but it means I have to design the output twice (once in jrxml for JasperReports, once in JSP), and I have to reimplement reporting functions (like subtotal calculation, total calculation, grouping ...) which is against the DRY principle.
What is the best practice for implementing this? Is it possible to create a better HTML export using JasperReports?
The output of the report filling process JasperPrint objects can be viewed using a built-in viewer component, or printed, or exported to more popular document formats like PDF, HTML, RTF, XLS, ODT, CSV, or XML.
Navigate to http://[localhost]:8480/jasperserver-pro/. Log into Standalone as sysadmin. Right-click the report you wish to export, then click Export.
It's not easy to change the HTML output of JasperReports to be nice. Here is an old quote on why:
...document formats such as HTML or XLS, do not support absolute positioning of the text and graphic elements. The content of such documents is arranged in a grid or table structure. Of course, some may argue that absolute positioning of elements in HTML is possible thanks to CSS, but you can be sure that the CSS standard functionality is far from being implemented in all browsers or that the same HTML document won't look the same everywhere.
This is why the ^JasperReports built-in exporters that produce HTML, XLS or CSV documents use a special algorithm in order to arrange the elements present on a certain document page in some sort of a grid. When the report designs are very complex or agglomerated, passing from absolute positioning to grid or table layout produces very complex tables with many unused rows and columns, to make it for the empty space between elements or their special alignment.
source
As mentioned it's old but as far as I can tell it's still accurate.
The list of things you can control for the HTML exporter is very limited:
net.sf.jasperreports.export.html.frames.as.nested.tables
net.sf.jasperreports.export.html.remove.emtpy.space.between.rows
net.sf.jasperreports.export.html.size.unit
net.sf.jasperreports.export.html.using.images.to.align
net.sf.jasperreports.export.html.white.page.background
net.sf.jasperreports.export.html.wrap.break.word
net.sf.jasperreports.export.{format}.exclude.origin.{suffix}.{arbitrary_name}
net.sf.jasperreports.export.{format}.exclude.origin.keep.first.{suffix}.{arbitrary_name}
documentation here
I've stayed away from HTML and only use PDF, Excel and CSV unless customers demand HTML. If you must use HTML you can define a stylesheet to work with your site and use jQuery to remove all the inline styles so your stylesheet takes over. Something like:
$(document).ready(function() {
$('span').removeAttr('style');
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With