将CSS添加到JasperReports的HTML导出中

时间:2017-11-07 09:50:10

标签: java html css jasper-reports

我已经使用JasperReports 6.4.3创建了一个报告,该报告通常导出为PDF。现在,我也试图将此报告导出为HTML。 我不想通过JasperExportManager创建HTML文件,因此我使用JasperReport的HtmlExporter将报告直接写入输出流。

她是我的代码:

public void exportToHtml(OutputStream outputStream, JRDataSource jrDataSource, Map<String, Object> parameter, File reportFile) 
      throws IOException {
    try {
      JasperPrint jasperprint = JasperFillManager.fillReport(reportFile.getAbsolutePath(), parameter, jrDataSource);
      HtmlExporter exporter = new HtmlExporter();

      SimpleHtmlExporterOutput exporterOutput = new SimpleHtmlExporterOutput(outputStream);
      Map<String, String> images = Maps.newHashMap();
      exporterOutput.setImageHandler(new HtmlResourceHandler() {
        @Override
        public void handleResource(String id, byte[] data) {
          System.err.println("id" + id);
          images.put(id, "data:image/jpg;base64," + Base64.encodeBytes(data));
        }

        @Override
        public String getResourcePath(String id) {
          return images.get(id);
        }
      });
      exporter.setExporterOutput(exporterOutput);
      exporter.setExporterInput(new SimpleExporterInput(jasperprint));

      SimpleHtmlExporterConfiguration exporterConfiguration = new SimpleHtmlExporterConfiguration();
      exporterConfiguration.setBetweenPagesHtml("<div style='page-break-after:always'></div>");
      exporter.setConfiguration(exporterConfiguration);
      exporter.exportReport();
    } catch (JRException jrException) {
      throw new IOException(jrException);
    }
}

输出看起来不错,但我需要在HTML输出中添加一些样式。所以我想知道是否可以将css文件的引用添加到导出的html报告中。

1 个答案:

答案 0 :(得分:0)

与设置页面HTML之间的方式类似,您可以对页眉/页脚执行相同的操作:

exporterConfiguration.setHtmlHeader("...");
exporterConfiguration.setHtmlFooter("...");

标题的默认HTML代码设置为here,页脚的默认设置为here

修改其中任何一个时,您需要匹配开始/结束标记。