2

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.

2

1 Answer 1

2

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

2
  • Absolutely what I was looking for. Thank you so much! Now I can start fiddling around :). Mar 6, 2017 at 7:08
  • It's worth mentioning that you should minify the HTML and replace all " with \" and surround the end result in double quotes (""). Example: "<table><tr><td style=\"border:1pt solid black;\"></td></tr></table>" Mar 6, 2017 at 7:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.