Valentin Grégoire
Valentin Grégoire

Reputation: 1150

How to add the HtmlComponent to JasperDesign

I managed to add the HtmlComponent to my project through various searches.

My next problem is that I don't have a clue how to add it to a JasperDesign. I simply can't find any way to add my HtmlComponent to a JasperDesign (not even through a group etc).

I use Jasper 5.6.1 and managed to get the HtmlComponent jar (5.0.1) out of the iReport tool (5.6.0).

My end purpose is to be able to have an HTML-table inside my report. I know it's not possible in any way, but perhaps, an HTML-component would be the most interesting thing. This way, an image of the table would be in my report, which is ok.

This is my current code

private JasperDesign getTable(String html, JasperDesign jasperDesign) {
        HtmlComponent table = new HtmlComponent();
        JRDesignExpression expression = new JRDesignExpression();
        expression.setText(html.replace("\n", ""));
        table.setHtmlContentExpression(expression);
        JRDesignGroup group = new JRDesignGroup();
        group.setName(TABLE_GROUP_PREFIX);
        JRDesignBand groupHeader = new JRDesignBand();
        groupHeader.setHeight(200);
        groupHeader.setSplitType(SplitTypeEnum.IMMEDIATE);
        groupHeader.addElement(table);
        ((JRDesignSection)  group.getGroupHeaderSection()).addBand(groupHeader);

       return jasperDesign;
}

The line groupHeader.addElement(table) fails because HtmlComponent is not an Element, but a Component.

Upvotes: 2

Views: 5790

Answers (1)

Petter Friberg
Petter Friberg

Reputation: 21710

To add a component to the JRDesign you need to wrap it in the JRDesignComponentElement

JRDesignComponentElement ce = new JRDesignComponentElement(design);
ce.setComponentKey(new ComponentKey("http://jasperreports.sourceforge.net/htmlcomponent", "hc", "html"));
ce.setComponent(table);

Full example

//Create the design
JasperDesign design = new JasperDesign();
design.setWhenNoDataType(WhenNoDataTypeEnum.ALL_SECTIONS_NO_DETAIL); //We will run with no data
design.setName("Html test");
//Add a title band
JRDesignBand title = new JRDesignBand();
title.setHeight(100);
design.setTitle(title);

//Create our component
HtmlComponent hc = new HtmlComponent();
JRDesignExpression expression = new JRDesignExpression();
expression.setText("\"<b>Hello</b> world\"");
hc.setHtmlContentExpression(expression);

//Wrap it in a design componenent
JRDesignComponentElement ce = new JRDesignComponentElement(design);
ce.setComponentKey(new ComponentKey("http://jasperreports.sourceforge.net/htmlcomponent", "hc", "html"));
ce.setComponent(hc);
ce.setHeight(100);
ce.setWidth(100);
title.addElement(ce);

//Compile the design
JasperReport report = JasperCompileManager.compileReport(design);

//Generate the print (passing no datasource, empty)
JasperPrint print = JasperFillManager.fillReport(report, new HashMap<String,Object>());

//Export to pdf
JRPdfExporter exporter = new JRPdfExporter(); 
exporter.setExporterInput(new SimpleExporterInput(print));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput("pdf/htmlComponent.pdf"));
SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
exporter.setConfiguration(configuration);
exporter.exportReport();

Output

result

Tons of code for a Hello world, it's often quicker to do the report in jrxml

Upvotes: 2

Related Questions