Víctor Pariente Víctor Pariente - 5 months ago 27
HTML Question

Export custom HTML template

Background



Exporting to HTML generates the following:

<html>
<head>
<title></title>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<style type="text/css">
a {text-decoration: none}
</style>
</head>
<body vlink="#000000" text="#000000" link="#000000" alink="#000000">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="50%">&nbsp;</td>
<td align="center">
<!-- The report -->
</td>
<td width="50%">&nbsp;</td>
</tr>
</tbody>
</table>
</body>
</html>


Problem



The report is centered on the page, but should be left-aligned.

Former Solution



Using JRHtmlExporter's HTML_HEADER parameter looked promising, but the classes have been deprecated. This was the solution:

JRHtmlExporter exporter = new JRHtmlExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRHtmlExporterParameter.HTML_HEADER,
"<html>"+
"<head>"+
" <title></title>"+
" <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>"+
" <link rel=\"stylesheet\" type=\"text/css\" href=\"css/jasper.css\" />"+
" <style type="text/css">"+
" a {text-decoration: none}"+
" </style>"+
"</head>"+
"<body text="#000000" link="#000000" alink="#000000" vlink="#000000">"+
"<table width="100%" cellpadding="0" cellspacing="0" border="0">"+
"<tr><td width="50%">&nbsp;</td><td align="center">");
exporter.exportReport();


Now I have to use the
net.sf.jasperreports.export.HtmlExporter
and
net.sf.jasperreports.export.SimpleHtmlReportConfiguration
classes, in this way:

HtmlExporter exporterHTML = new HtmlExporter();
SimpleExporterInput exporterInput = new SimpleExporterInput(report.getJasperPrint());
exporterHTML.setExporterInput(exporterInput);
HtmlExporterOutput exporterOutput = new SimpleHtmlExporterOutput(out);
exporterHTML.setExporterOutput(exporterOutput );

SimpleHtmlReportConfiguration reportExportConfiguration = new SimpleHtmlReportConfiguration();
reportExportConfiguration.setWhitePageBackground(false);
reportExportConfiguration.setRemoveEmptySpaceBetweenRows(true);
exporterHTML.setConfiguration(reportExportConfiguration);

exporterHTML.exportReport();


How do would you fix this?

Environment




  • JasperReports v5.5.2

  • Java v1.6.0_38


Answer

The HTML_HEADER parameter of JRHtmlExporterParameter has been replaced with:

HtmlExporterConfiguration.getHtmlHeader()

exporterConfig.setHtmlHeader(getHTMLHeader());

private String getHTMLHeader() {
    StringBuffer sb = new StringBuffer();
    sb.append("<html>");
    sb.append("<head>");
    sb.append("  <title></title>");
    sb.append("  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>");
    sb.append("  <style type=\"text/css\">");
    sb.append("    a {text-decoration: none}");
    sb.append("  </style>");
    sb.append("</head>");
    sb.append("<body text=\"#000000\" link=\"#000000\" alink=\"#000000\" vlink=\"#000000\">");
    sb.append("<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">");
    sb.append("<tr><td align=\"left\">");

    return sb.toString();
}
Comments