JasperReports html 和 pdf 输出

发布于 2024-08-10 00:10:36 字数 1248 浏览 1 评论 0 原文

我想使用 JSP 创建一个动态报告网页。基本上它应该包含以下部分:

  1. 过滤器:用户可以指定过滤条件并按下过滤按钮。
  2. HTML 输出:可以在此处看到过滤器的结果。这是一个大的 html 页面(如果一页太大,则可以是多个页面)。它可能包含到系统其他部分的链接。
  3. 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:

  1. Filter: The user can specify the filter conditions and press a filter button.
  2. 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.
  3. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╄→承喏 2024-08-17 00:10:36

将 JasperReports 的 HTML 输出改得漂亮并不容易。这是一个关于原因的古老引用:

...文档格式,例如 HTML 或 XLS,
不支持绝对定位
文本和图形元素。这
此类文件的内容已安排
在网格或表格结构中。的
当然,有些人可能会认为绝对
HTML 中元素的定位是
感谢CSS,但你可以
确保 CSS 标准
功能性还差得很远
在所有浏览器或其他浏览器中实现
同一个 HTML 文档看起来不会
到处都一样。

这就是为什么 ^JasperReports 内置导出器生成 HTML,
XLS 或 CSV 文档使用特殊的
算法以便安排
某个文档中存在的元素
某种网格中的页面。什么时候
报告设计非常复杂或
聚集,从绝对
定位到网格或表格布局
生成非常复杂的表格,其中有很多
未使用的行和列,以使其
用于元素之间的空白空间
或他们的特殊排列。

来源

如前所述,它很旧,但据我所知它仍然存在准确的。

您可以为 HTML 导出器控制的内容非常有限:

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}

此处的文档

我已经远离 HTML,只使用 PDF、Excel 和 CSV,除非客户需要 HTML。如果您必须使用 HTML,您可以定义一个样式表来与您的站点配合使用,并使用 jQuery 删除所有内联样式,以便您的样式表接管。像这样的东西:

$(document).ready(function() {
   $('span').removeAttr('style');
});

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');
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文