stebeg
stebeg

Reputation: 165

Add CSS to JasperReports' HTML Export

I've created a report with JasperReports 6.4.3 which is normally exported to PDF. Now, I'm trying to export this report to HTML as well. I don't want to create a HTML file via JasperExportManager, so I'm using JasperReport's HtmlExporter to wirte the report directly into an outputstream.

Her is my code:

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);
    }
}

The output looks good, but I need to add some styles to the HTML output. So I wonder if it is possible to add a reference to a css file to the exported html report.

Upvotes: 0

Views: 1203

Answers (1)

Narcis
Narcis

Reputation: 2511

Similarly to how you are setting the between pages HTML, you could do the same for the header/footer with:

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

The default HTML code for header is set here and the one for footer is set here.

You need to match the opening/closing tags when modifying any of them.

Upvotes: 1

Related Questions