gpt4 book ai didi

java - 导出为 HTML 时如何使报告大小适应屏幕大小

转载 作者:行者123 更新时间:2023-11-30 01:46:08 24 4
gpt4 key购买 nike

有没有办法将 HTML 报告的大小指定为百分比,例如 width = "90%"?或者以其他方式确保报表的宽度在导出为 HTML 时占用户屏幕的一定百分比?

我不介意创建额外的模板,或者只将图表导出为图像。

我正在使用 Jasper Reports 版本 6.9.0。

最佳答案

Jasper 报告导出的目标是像素完美(可打印)输出,这就是为什么默认情况下,无论浏览器窗口的大小如何,大小都将以像素为单位与报告的实际宽度相对应。

Jasper 报告 HTMLExporter为此,创建一个包含 3 列的表格,第一个空列 width="50%",第二个列宽度(以 px 为单位)作为报告的宽度,第三个空列再次为 width="50 %”。这将使结果居中,并且报告的宽度将等于 jrxml 中指示的宽度。

如果您从 java 导出,您可以覆盖此行为并设置您自己的 html 页眉和页脚。

示例

HtmlExporter exporter = new HtmlExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleHtmlExporterOutput("html/my.html"));
SimpleHtmlExporterConfiguration configuration = new SimpleHtmlExporterConfiguration();
configuration.setHtmlHeader(
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n" +
"<html>\r\n" +
"<head>\r\n" +
" <title></title>\r\n" +
" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>\r\n" +
" <style type=\"text/css\">\r\n" +
" a {text-decoration: none}\r\n" +
" .jrPage {width:90% !important}\r\n" +
" </style>\r\n" +
"</head>\r\n" +
"<body text=\"#000000\" link=\"#000000\" alink=\"#000000\" vlink=\"#000000\">\r\n" +
"<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">\r\n" +
"<tr><td width=\"5%\">&nbsp;</td><td align=\"center\">\r\n");

configuration.setHtmlFooter(
"</td><td width=\"5%\">&nbsp;</td></tr>\r\n" +
"</table>\r\n" +
"</body>\r\n" +
"</html>\r\n");
exporter.setConfiguration(configuration);
exporter.exportReport();

基本上,我对默认 html 页眉和页脚所做的更改是

  1. 添加了 .jrPage 样式以覆盖像素宽度,改为更改为 90% 宽度。

  2. 将两侧栏的百分比从 50% 更改为 5%

这将创建一个动态大小的 html,该 html 居中并占据可用窗口大小的 90%。

<小时/>

示例如何在本例中自动缩放由图表生成的图像

考虑这个问题中的jrxml How can I export report to PDF/A-1a, PDF/A-1b?

  1. 添加net.sf.jasperreports.export.html.class到图表元素

    <pieChart>
    <chart isShowLegend="false">
    <reportElement x="225" y="-670" width="320" height="140" uuid="23bd26a6-04a4-406f-8a1a-5e1b260cb75d">
    <property name="net.sf.jasperreports.export.html.class" value="pieChart"/>
    </reportElement>
    ....
  2. 在 HTML header 中添加一些 CSS

     @media only screen and (min-width : 768px) {
    td.pieChart img {height:300px !important;}
    }
    @media only screen and (min-width : 1224px) {
    td.pieChart img {height:400px !important;}
    }

但请注意,图像仅“重新缩放”,因此它将保持其原始分辨率。

关于java - 导出为 HTML 时如何使报告大小适应屏幕大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57889915/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com