java JasperReports html 和 pdf 输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1686117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JasperReports html and pdf output
提问by asalamon74
I'd like to create a dynamic reporting webpage using JSP. Basically it should contain the following parts:
我想使用 JSP 创建一个动态报告网页。基本上它应该包含以下部分:
- 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.
- 过滤器:用户可以指定过滤条件并按下过滤器按钮。
- HTML-output:过滤器的结果可以在这里看到。它是一个大的 html 页面(如果一页太大,则有几个)。它可能包含到系统其他部分的链接。
- PDF 输出:用户应该能够保存报告的 pdf 版本以用于打印或存档目的。
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.
我不想自己实现所有内容,而是想使用 java 报告库,因此我使用JasperReports创建了我的报告。pdf 输出非常好,但报告的 html 导出不适合我的目的。
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_BACKGROUNDoption, on the other hand cellpadding="0" cellspacing="0" border="0"are hardwired in the tabletag. 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.
JasperReport 的 html 导出会创建一个 html 文件,其中包含大量硬连线代码和相当随机的配置选项。例如,它默认创建一个带有白色背景的表格 ( <table style="... bgcolor="white" ... ">),可以使用IS_WHITE_PAGE_BACKGROUND选项将其关闭,另一方面cellpadding="0" cellspacing="0" border="0"在table标签中硬连线。这也很奇怪(并且使 css 样式变得困难),而不是跨类,html 文件包含<span style="font-family: sansserif; color: #000000; font-size: 10.0px;">我的所有字段。
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 DRYprinciple.
当然我可以使用JSP来实现html输出,但这意味着我必须设计两次输出(一次在JasperReports的jrxml中,一次在JSP中),并且我必须重新实现报告功能(如小计计算,总计计算,分组...) 这违反了DRY原则。
What is the best practice for implementing this? Is it possible to create a better HTML export using JasperReports?
实现这一点的最佳做法是什么?是否可以使用 JasperReports 创建更好的 HTML 导出?
回答by beggs
It's not easy to change the HTML output of JasperReports to be nice. Here is an old quote on why:
将 JasperReports 的 HTML 输出更改为漂亮并不容易。这是关于原因的旧引用:
...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.
...文档格式,例如 HTML 或 XLS,不支持文本和图形元素的绝对定位。此类文档的内容以网格或表格结构排列。当然,有些人可能会争辩说,由于 CSS,HTML 中元素的绝对定位是可能的,但是您可以确定 CSS 标准功能远未在所有浏览器中实现,或者相同的 HTML 文档在任何地方看起来都不一样.
这就是生成 HTML、XLS 或 CSV 文档的 ^JasperReports 内置导出器使用特殊算法的原因,以便将某个文档页面上的元素排列在某种网格中。当报表设计非常复杂或聚集时,从绝对定位到网格或表格布局会产生非常复杂的表格,其中包含许多未使用的行和列,以使其元素之间的空白或它们的特殊对齐。
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:
您可以为 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}
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:
我一直远离 HTML,只使用 PDF、Excel 和 CSV,除非客户需要 HTML。如果您必须使用 HTML,您可以定义一个样式表来处理您的站点,并使用 jQuery 删除所有内联样式,以便您的样式表接管。就像是:
$(document).ready(function() {
$('span').removeAttr('style');
});

