我想使用 JSP 创建一个动态报告网页。基本上它应该包含以下部分:
- 过滤器:用户可以指定过滤条件并按下过滤按钮。
- HTML 输出:可以在此处看到过滤器的结果。这是一个大的 html 页面(如果一页太大,则可以是多个页面)。它可能包含到系统其他部分的链接。
- PDF 输出:用户应该能够保存报告的 PDF 版本以供打印或存档之用。
我不想自己实现所有内容,而是想使用 java 报告库,因此我使用 JasperReports。 pdf 输出确实不错,但报告的 html 导出不适合我的目的。
JasperReport 的 html 导出创建一个 html 文件,其中包含大量硬连线代码和相当随机的配置选项。例如,它默认创建一个白色背景的表格 (),可以使用 IS_WHITE_PAGE_BACKGROUND
选项,另一方面,cellpadding="0" cellspacing="0" border="0"
是硬连线在 table
标记中的。同样奇怪的是(并且使 css 样式变得困难),html 文件包含 而不是 span 类。
我的所有字段。
当然我可以使用JSP实现html输出,但这意味着我必须设计两次输出(一次在JasperReports的jrxml中,一次在JSP中),并且我必须重新实现报告功能(如小计计算,总计计算,分组) ...)这违反了DRY原则。
实现这一点的最佳实践是什么?是否可以使用 JasperReports 创建更好的 HTML 导出?
I'd like to create a dynamic reporting webpage using JSP. Basically it should contain the following parts:
- Filter: The user can specify the filter conditions and press a filter button.
- HTML-output: The result of the filter can be seen here. It's one large html page (or several if one page would be too large). It may contain links to other parts of the system.
- PDF-output: The user should be able to save a pdf version of the report for printing or archiving purposes.
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?
发布评论
评论(1)
将 JasperReports 的 HTML 输出改得漂亮并不容易。这是一个关于原因的古老引用:
来源
如前所述,它很旧,但据我所知它仍然存在准确的。
您可以为 HTML 导出器控制的内容非常有限:
此处的文档
我已经远离 HTML,只使用 PDF、Excel 和 CSV,除非客户需要 HTML。如果您必须使用 HTML,您可以定义一个样式表来与您的站点配合使用,并使用 jQuery 删除所有内联样式,以便您的样式表接管。像这样的东西:
It's not easy to change the HTML output of JasperReports to be nice. Here is an old quote on why:
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:
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: