Saturday 6 August 2016

Tip : How to call an image inside HTML component in Jasper Studio Reports 6.2 or later

Hi,

In this post you can learn, how to call an image inside HTML component using HTML script and  how to apply style to it.

With in the Studio and Operating System Environment

Lets assume, you have an image on your desktop say : tibco.png and the path for the image is  C:\Users\sadakar\Desktop\tibco.png

If you want to use the same image to get in JRXML inside HTML component, convert the path of the image as file:///C:/Users/sadakar/Desktop/tibco.png


Drag and drop html component to be of your choice of band, for instance drag HTML component to summary band and double click on it to open its "Expression Editor" and write below HTML code along with styles.

"<p style='background-color:yellow;font-family:verdana;font-size:50px;'>Hi, I am through HTML CODE using HTML Component</p>"+"<br><br>"+
"<img src='file:///C:/Users/sadakar/Desktop/tibco.png' alt='Japser Architecture' height='1000' width='1800'>"


Sample screenshot of the above code : 


Sample output for the above design


Sample JRXML code: (Copy paste the below code and change image path of your choice - version supported for this example is 6.2 pro or later)

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.2.0.final using JasperReports Library version 6.2.0  -->
<!-- 2016-08-06T16:53:41 -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Blank_A4_1" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="de083992-95ea-48ad-983f-f63b1b7885ab">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<queryString>
<![CDATA[]]>
</queryString>
<title>
<band height="79" splitType="Stretch"/>
</title>
<summary>
<band height="381" splitType="Stretch">
<componentElement>
<reportElement x="0" y="0" width="555" height="381" uuid="c557a700-606d-4893-af98-ca8df921bef9"/>
<hc:html xmlns:hc="http://jasperreports.sourceforge.net/htmlcomponent" xsi:schemaLocation="http://jasperreports.sourceforge.net/htmlcomponent http://jasperreports.sourceforge.net/xsd/htmlcomponent.xsd" scaleType="RetainShape" horizontalAlign="Left" verticalAlign="Middle">
<hc:htmlContentExpression><![CDATA["<p style='background-color:yellow;font-family:verdana;font-size:50px;'>Hi, I am through HTML CODE using HTML Component</p>"+"<br><br>"+
"<img src='file:///C:/Users/sadakar/Desktop/tibco.png' alt='Japser Architecture' height='1000' width='1800'>"]]></hc:htmlContentExpression>
</hc:html>
</componentElement>
</band>
</summary>

</jasperReport>



NOTE: This example is specific for absolute path image with in the studio and operating system environment. It can not be deployed to Japser Server unless you have HTML component jar file installed in lib folder of tomcat.

Usually we go for this kind of implementation to represent web clocks showing different time zones on the report.

I hope it helps some one in community.

Cheers.!
Sadakar Pochampalli


1 comment: