Export HTML Content to PDF Using Jasperreports

How to print html content to a PDF with jasper reports?

You can use the HtmlComponent <hc:html/>

Example

<componentElement>
<reportElement x="0" y="100" width="230" height="110" backcolor="#ADD8E6" uuid="332dd551-e8cd-4cb0-a11f-7325f481017b"/>
<hc:html xmlns:hc="http://jasperreports.sourceforge.net/htmlcomponent" xsi:schemaLocation="http://jasperreports.sourceforge.net/htmlcomponent http://jasperreports.sourceforge.net/xsd/htmlcomponent.xsd" scaleType="FillFrame" horizontalAlign="Left" verticalAlign="Top">
<hc:htmlContentExpression><![CDATA["Hello<br/>World"]]></hc:htmlContentExpression>
</hc:html>
</componentElement>

It will generate an image of your html.

JasperReports html and pdf output

It's not easy to change the HTML output of JasperReports to be nice. Here is an old quote on why:

...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.

source

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:

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}

documentation here

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:

$(document).ready(function() {
$('span').removeAttr('style');
});

Export JasperReports in HTML format

For HTML and other formats:

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.HtmlExporter;
import net.sf.jasperreports.engine.export.JRCsvExporter;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRXmlExporter;
import net.sf.jasperreports.export.Exporter;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleHtmlExporterOutput;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;

public byte[] export(final JasperPrint print) throws JRException {
final Exporter exporter;
final ByteArrayOutputStream out = new ByteArrayOutputStream();
boolean html = false;

switch (getReportFormat()) {
case HTML:
exporter = new HtmlExporter();
exporter.setExporterOutput(new SimpleHtmlExporterOutput(out));
html = true;
break;

case CSV:
exporter = new JRCsvExporter();
break;

case XML:
exporter = new JRXmlExporter();
break;

case XLSX:
exporter = new JRXlsxExporter();
break;

case PDF:
exporter = new JRPdfExporter();
break;

default:
throw new JRException("Unknown report format: " + getReportFormat().toString());
}

if (!html) {
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(out));
}

exporter.setExporterInput(new SimpleExporterInput(print));
exporter.exportReport();

return out.toByteArray();
}

Call it using:

JasperPrint print = JasperFillManager.fillReport(report, parameters, dataSource);
byte report[] = export(print);

how to open a jasper report exporting to pdf in a new window

Considering your view.jsp

I can see that you already have put all variables into the session you are only using the form to select to which page you need to go.

You can do something like this...

<script type="text/javascript">
function generateReport() {
var e = document.getElementById("idOfYourSelectYouNeedToAddedIt");
var strPage = e.options[e.selectedIndex].value;
window.open(strPage);
return false; //This make you stay on this page;
//return true; //Set the action tag in the form to the page you like to go to!
}
</script>

<form name="myForm" onsubmit="return generateReport()">
<select id = "idOfYourSelectYouNeedToAddedIt">
<option value=''> Make a selection </option>
..... the other option values
</form>

Note if you like to go to another page you can simple add action tag to your form tag and return true in the generateReport() function

example

 <form name="myForm" action="thisIsWhereILikeToGo.jsp"  onsubmit="return generateReport()">

Note: You should remove the onchange="setAction(this.value) code, it has no use anymore and consider checking that strPage is not empty, no selection...

Export to single HTML with embedded images using Jasper Report

A solution:

Map<String, String> images = new HashMap<>();

SimpleHtmlExporterOutput simpleHtmlExporterOutput = new SimpleHtmlExporterOutput(outputStream);
simpleHtmlExporterOutput.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);
}
});

Full code:

package com.test.report;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRXmlDataSource;
import net.sf.jasperreports.engine.export.HtmlExporter;
import net.sf.jasperreports.engine.export.HtmlResourceHandler;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleHtmlExporterOutput;
import net.sf.jasperreports.export.SimpleHtmlReportConfiguration;

import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.olap4j.impl.Base64;

import com.artech.reportservice.reports.ReportType;

public class ReportTest {
Map<String, String> images = new HashMap<>();

@Test
public void test() throws Exception {
// String outFileName = "test.html";

String xmlFileLocation = "/Users/skozlic/dev/VacationToolProject/wokspace/ReportService/src/test/resources/machineReportTestFile.xml";

JasperReport reportTemplate = ReportType.MPM.getReportTemplate();
JRXmlDataSource jrxmlds = ReportType.MPM.getReportDateSource(xmlFileLocation);
JasperPrint jasperPrint = JasperFillManager.fillReport(reportTemplate, null, jrxmlds);

HtmlExporter exporterHTML = new HtmlExporter();
SimpleExporterInput exporterInput = new SimpleExporterInput(jasperPrint);
exporterHTML.setExporterInput(exporterInput);
SimpleHtmlReportConfiguration reportExportConfiguration = new SimpleHtmlReportConfiguration();

exporterHTML.setConfiguration(reportExportConfiguration);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

SimpleHtmlExporterOutput simpleHtmlExporterOutput = new SimpleHtmlExporterOutput(outputStream);
simpleHtmlExporterOutput.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);
}
});
exporterHTML.setExporterOutput(simpleHtmlExporterOutput);

exporterHTML.exportReport();
FileUtils.writeByteArrayToFile(new File("test.html"), outputStream.toByteArray());

}
}


Related Topics



Leave a reply



Submit