HTML Tag Table into Text Field - JasperReport - jasper-reports

I have a parameter input to Text Field with html markup language or styled. But when I put tag , It doesn't show at all.
Jaspersoft supported thid Tags:
<b/>, <u/>, <i/>, <font/>,<sup/>, <sub/>, <li/>, <br/>
Is there any way to show table from that html tag ? If it can not, is there any alternative solution ?

There is no way to show html table's inside a textField.
The html attribute can only be used for how characters looks, not to
generate layout
The only option you have is to use the jasper report 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["<table><tr><td>Hello</td></tr><tr><td>World</td></tr></table>]]></hc:htmlContentExpression>
</hc:html>
</componentElement>
The major problem with this component is that it creates an image of your html and you will have problem scaling text.

Related

ireport - list style type="square" doesn't work

I have a requirement where to enable list with square bullets in ireport.
I used below coding and set markup as "html" but don't know how to achieve this.
"Names of fruits are:" +
"<ul style = {margin-top=-13px; list-style-type=square;} >" +
"<li> Apple</li>" +
"<li> Orange</li>" +
"<li> Banana</li>" +
"</ul>"
entered above code in Text Field Expression of iReport. When I compiled No Error. When I do preview, the list comes with bullets of disc not square. Besides, have to reduce the gap between ul & li which occurs default. Anyone knows the solution, pls help with coding.
Unsupported elements
The <ul> and <ol> elements are not supported:
The JasperReports proprietary markup language is called styled text and is an XML based format in which the style of any portion of text inside a text element can be changed by embedding that portion inside a <style> tag or other simple HTML tag from the following list:
<b>
<u>
<i>
<font>
<sup>
<sub>
<li>
<br>
The <style> tag has various attributes for altering the color, font, or other style properties of the text. From the standard HTML <font> tag, only the fontFace, color, and size attributes are recognized by the JasperReports engine. Each individual style property is inherited from the overall text element or from the parent <style> tag when nested <style> tags are used.
Solution #1 - Structured data
Instead of including an HTML blurb directly in the report, consider querying the data from the data source directly. For example, consider the following table in the database, with a name of Fruits:
| fruits |
|--------|
| Apple |
| Orange |
| Banana |
Such as:
SELECT fruit_name FROM fruits;
Then change the presentation logic in the report:
<detail>
<band height="15">
<staticText>
<reportElement x="0" y="0" width="10" height="15" />
<text><![CDATA[■]]></text>
</staticText>
<textField>
<reportElement x="11" y="0" width="50" height="15"/>
<textFieldExpression><![CDATA[$F{fruit_name}]]></textFieldExpression>
</textField>
</band>
</detail>
Solution #2 - Parsed data
If the input string must be a simple HTML document (an idea that mixes presentation with content, an unfortunate coupling), then another possibility is:
Convert the HTML to a plain text string.
Convert that string into structured string of data (e.g., put each line in double quotes then join all newlines with a comma).
Convert the structured string to a JR data source (e.g., JRCsvDataSource).
Pass the JR data source into a subreport.
Revise the detail band of the subreport as shown in the first solution.
Addendum
There are other possibilities, but the underlying problem is that the data (i.e., the HTML string) is providing two different functions: (1) how the data appears with (2) the data to be displayed. To solve this problem in the most flexible way, separate how the data must appear from the data itself. That is, don't pass preformatted HTML into a report.

Show and hide elements for different exporters [duplicate]

I have a JasperReports jrxml file which has a hyperlink inside a textField element.
I do not want to show this hyperlink in anything other than the HTML view because the link does not work in excel, PDF, word, etc. and does not make sense to show.
I have read the faq with the properties but it just confused me because it does not talk about hiding a textField at all, just "bands" for headers and footers.
Here is the text field I want to hide when not HTML:
<textField hyperlinkType="ReportExecution">
<reportElement style="Report_Param_Value_Link" mode="Opaque" x="400" y="0" width="161" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[Boolean.TRUE.equals($P{LAST_WEEK}) ? "View WTD" : "View last week"]]></textFieldExpression>
<hyperlinkParameter name="noMenu">
<hyperlinkParameterExpression><![CDATA["true"]]></hyperlinkParameterExpression>
</hyperlinkParameter>
<hyperlinkParameter name="reportUnit">
<hyperlinkParameterExpression><![CDATA["repo:/Reports/Operations/Business_Support/Subreports/Business_Support_Performance_Dashboard"]]></hyperlinkParameterExpression>
</hyperlinkParameter>
<hyperlinkParameter name="LAST_WEEK">
<hyperlinkParameterExpression><![CDATA[Boolean.valueOf(!Boolean.TRUE.equals($P{LAST_WEEK})).toString()]]></hyperlinkParameterExpression>
</hyperlinkParameter>
</textField>
Use an Element Key Filter.
The quote from JR Ultimate Guide:
This built-in filter implementations excludes from export elements that match a given element key.
Element keys are set at report design time and are propagated into generated reports.
Each element in a filled report has the same key as the element from the report template that generated it.
To trigger an element key filter, the report designer needs to define one or more report properties that start with <exporter_property_prefix>.exclude.key. Each such property matches a single element key which is to be excluded by the filter. The element key is given by the property value, or if no value is set for the property, by the property suffix.
The following example shows how to specify element keys which are to be excluded from specific export outputs:
<jasperReport ...>
<!-- exclude elements with keys Image1 and Text4 from HTML export-->
<property name="net.sf.jasperreports.export.html.exclude.key.Image1"/>
<property name="net.sf.jasperreports.export.html.exclude.key.Text4"/>
<!-- exclude elements with keys Image5 from PDF export -->
<property name="net.sf.jasperreports.export.pdf.exclude.key.the.image" value=”Image5”/>
...
</jasperReport>
In your case you should add key for text field with hyperlink (for example, textFieldWithHL) and then add one property for each format (pdf, docx, xls, csv, xml, txt, odt) you want to exclude from printing this hyperlink:
<property name="net.sf.jasperreports.export.pdf.exclude.key.textFieldWithHL"/>
<property name="net.sf.jasperreports.export.docx.exclude.key.textFieldWithHL"/>
<property name="net.sf.jasperreports.export.xls.exclude.key.textFieldWithHL"/>
<property name="net.sf.jasperreports.export.csv.exclude.key.textFieldWithHL"/>
<property name="net.sf.jasperreports.export.xml.exclude.key.textFieldWithHL"/>
The expressions from your post:
net.sf.jasperreports.export.{format}.exclude.origin.{suffix}.{arbitrary_name}
net.sf.jasperreports.export.{format}.exclude.origin.keep.first.{suffix}.{arbitrary_name}
allow to exclude the whole bands (also group bands). This filters work with JROrigin objects.
For example, consider a report with a logo that must be included as SVG for PDF output or PNG for HTML output. The JRXML file contains:
<image scaleImage="RetainShape" onErrorType="Blank">
<reportElement key="IMAGE_LOGO_PNG" x="1" y="0" width="100" height="60" uuid="a896cade-f6fc-4d8f-b762-29b950309257"/>
<imageExpression><![CDATA[Transcoder.asPNG($V{V_LOGO_FILE} + ".svg")]]></imageExpression>
</image>
<image scaleImage="RetainShape" onErrorType="Blank">
<reportElement key="IMAGE_LOGO_SVG" x="1" y="0" width="100" height="60" uuid="a896cade-f6fc-4d8f-b762-29b950309257"/>
<imageExpression><![CDATA[Transcoder.asSVG($V{V_LOGO_FILE} + ".svg")]]></imageExpression>
</image>
To exclude the SVG from HTML and the PNG from PDF, add the following properties immediately after the <jasperReport...> root element in the JRXML file:
<property name="net.sf.jasperreports.export.html.exclude.key.IMAGE_LOGO_SVG"/>
<property name="net.sf.jasperreports.export.pdf.exclude.key.IMAGE_LOGO_PNG"/>
Looking at your source, it may be possible to create an ExporterFilter that suppresses hyperlinks, and then you have to add that filter to the export process for everything except HTML. However, I don't see why you don't want to show the hyperlink in the other formats. For many years PDF, Word, Excel, etc. will all interpret a hyperlink correctly, and in fact respond to a mouse click on the link just like a browser. That's probably why the process is going to be painful: you are reversing what for most people is expected behavior.

How to hide textField for specified exporter. For example for not HTML

I have a JasperReports jrxml file which has a hyperlink inside a textField element.
I do not want to show this hyperlink in anything other than the HTML view because the link does not work in excel, PDF, word, etc. and does not make sense to show.
I have read the faq with the properties but it just confused me because it does not talk about hiding a textField at all, just "bands" for headers and footers.
Here is the text field I want to hide when not HTML:
<textField hyperlinkType="ReportExecution">
<reportElement style="Report_Param_Value_Link" mode="Opaque" x="400" y="0" width="161" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[Boolean.TRUE.equals($P{LAST_WEEK}) ? "View WTD" : "View last week"]]></textFieldExpression>
<hyperlinkParameter name="noMenu">
<hyperlinkParameterExpression><![CDATA["true"]]></hyperlinkParameterExpression>
</hyperlinkParameter>
<hyperlinkParameter name="reportUnit">
<hyperlinkParameterExpression><![CDATA["repo:/Reports/Operations/Business_Support/Subreports/Business_Support_Performance_Dashboard"]]></hyperlinkParameterExpression>
</hyperlinkParameter>
<hyperlinkParameter name="LAST_WEEK">
<hyperlinkParameterExpression><![CDATA[Boolean.valueOf(!Boolean.TRUE.equals($P{LAST_WEEK})).toString()]]></hyperlinkParameterExpression>
</hyperlinkParameter>
</textField>
Use an Element Key Filter.
The quote from JR Ultimate Guide:
This built-in filter implementations excludes from export elements that match a given element key.
Element keys are set at report design time and are propagated into generated reports.
Each element in a filled report has the same key as the element from the report template that generated it.
To trigger an element key filter, the report designer needs to define one or more report properties that start with <exporter_property_prefix>.exclude.key. Each such property matches a single element key which is to be excluded by the filter. The element key is given by the property value, or if no value is set for the property, by the property suffix.
The following example shows how to specify element keys which are to be excluded from specific export outputs:
<jasperReport ...>
<!-- exclude elements with keys Image1 and Text4 from HTML export-->
<property name="net.sf.jasperreports.export.html.exclude.key.Image1"/>
<property name="net.sf.jasperreports.export.html.exclude.key.Text4"/>
<!-- exclude elements with keys Image5 from PDF export -->
<property name="net.sf.jasperreports.export.pdf.exclude.key.the.image" value=”Image5”/>
...
</jasperReport>
In your case you should add key for text field with hyperlink (for example, textFieldWithHL) and then add one property for each format (pdf, docx, xls, csv, xml, txt, odt) you want to exclude from printing this hyperlink:
<property name="net.sf.jasperreports.export.pdf.exclude.key.textFieldWithHL"/>
<property name="net.sf.jasperreports.export.docx.exclude.key.textFieldWithHL"/>
<property name="net.sf.jasperreports.export.xls.exclude.key.textFieldWithHL"/>
<property name="net.sf.jasperreports.export.csv.exclude.key.textFieldWithHL"/>
<property name="net.sf.jasperreports.export.xml.exclude.key.textFieldWithHL"/>
The expressions from your post:
net.sf.jasperreports.export.{format}.exclude.origin.{suffix}.{arbitrary_name}
net.sf.jasperreports.export.{format}.exclude.origin.keep.first.{suffix}.{arbitrary_name}
allow to exclude the whole bands (also group bands). This filters work with JROrigin objects.
For example, consider a report with a logo that must be included as SVG for PDF output or PNG for HTML output. The JRXML file contains:
<image scaleImage="RetainShape" onErrorType="Blank">
<reportElement key="IMAGE_LOGO_PNG" x="1" y="0" width="100" height="60" uuid="a896cade-f6fc-4d8f-b762-29b950309257"/>
<imageExpression><![CDATA[Transcoder.asPNG($V{V_LOGO_FILE} + ".svg")]]></imageExpression>
</image>
<image scaleImage="RetainShape" onErrorType="Blank">
<reportElement key="IMAGE_LOGO_SVG" x="1" y="0" width="100" height="60" uuid="a896cade-f6fc-4d8f-b762-29b950309257"/>
<imageExpression><![CDATA[Transcoder.asSVG($V{V_LOGO_FILE} + ".svg")]]></imageExpression>
</image>
To exclude the SVG from HTML and the PNG from PDF, add the following properties immediately after the <jasperReport...> root element in the JRXML file:
<property name="net.sf.jasperreports.export.html.exclude.key.IMAGE_LOGO_SVG"/>
<property name="net.sf.jasperreports.export.pdf.exclude.key.IMAGE_LOGO_PNG"/>
Looking at your source, it may be possible to create an ExporterFilter that suppresses hyperlinks, and then you have to add that filter to the export process for everything except HTML. However, I don't see why you don't want to show the hyperlink in the other formats. For many years PDF, Word, Excel, etc. will all interpret a hyperlink correctly, and in fact respond to a mouse click on the link just like a browser. That's probably why the process is going to be painful: you are reversing what for most people is expected behavior.

Line is not visible in subreport jasper report?

I am using jasper report to create pdf document. I used one Subreport inside the main document. This Subreport consist of 3 textfield and one line. I use javabean datasource to populate the value. Now I could not see line if the datasource returns null value. Can anyone guide me how to solve this problem.
Below you can see the xml code which I used for drawing line in jrxml
file:
<line direction="TopDown">
<reportElement mode="Transparent" x="-6" y="17" width="480" height="0"
forecolor="#C6C8CA" key="line-1" isPrintInFirstWholeBand="true"/>
<graphicElement stretchType="NoStretch">
<pen lineWidth="0.25" lineStyle="Solid"/>
</graphicElement>
</line>
Is the line in the same Band with the text fields? If yes, then that's you problem. In Jasper if the datasource returns no elements. The band will not be rendered, as simple as that.
If you want the line to always appear put it in a band that is always rendered by default. Like Page_Header, Page_Footer ...etc.
This also happens when 2 items overlap. If your text box is over the line, change line's order by
Order->Bring to Front

External Styles in JasperReports

I'm working on a system that includes a large number of reports, generated using JasperReports. One of the newer features is that you can define styles for reports.
From the available docs I believe there is some way to have an external file defining styles to use, and you can reference that in your jasper reports. This allows a single style to be used by multiple reports.
I can't find any concrete information on whether this is an actual feature, and if it is, how to use it. Does anyone know if it is possible to have external styles for jasper reports, and if so, how to do it?
Use JasperReport templates. A JasperReports template is one that ends in .jrtx, and may look similar to this (styles.jrtx):
<?xml version="1.0"?>
<!DOCTYPE jasperTemplate
PUBLIC "-//JasperReports//DTD Template//EN"
"http://jasperreports.sourceforge.net/dtds/jaspertemplate.dtd">
<jasperTemplate>
<style name="Report Title" isDefault="false" hAlign="Center" fontSize="24" isBold="true"/>
<style name="Heading 1" isDefault="false" fontSize="18" isBold="true"/>
<style name="Heading 2" isDefault="false" fontSize="14" isBold="true"/>
</jasperTemplate>
and then in your .jrxml file, include it as a template:
...
<template><![CDATA["styles.jrtx"]]></template>
...
iReport also understands this, so your styles are imported and shown in iReport correctly (though I did notice sometimes it wouldn't pick them up an a reload or recompile was necessary).
You can also avoid specifying the actual file name in the <template> element by using a parameter passed into your report at runtime
<parameter name="TEMPLATE_FILE" isForPrompting="false" class="java.lang.String"/>
<template><![CDATA[$P{TEMPLATE_FILE}]]></template>
where $P{TEMPLATE_FILE} is the full path to the style resource
I like to share my learning of using styles in Jasper reports, which I think quite useful for report designers like me, from a book named JasperReport Development cookbook by Bilal Siddiqui. I like this book and found demonstrating styles in a variety of manner like:
Creating a reusable style
Simply select “Style” while creating a new report and define style for text, line and rectangles. The style file will be stored as .jrtx file.
Import reusable style it in your report
There are three chunk of information when importing styles in your report.
Step1. Name and location of style template
<template><![CDATA["C:\\ BigBoldRedTemplate.jrtx"]]></template>
Step2. Each time you apply style to your report elements using the style template, a <reportElement> tag is created as shown below:
//style applied to a rectangle
<rectangle radius="10">
<reportElement style="BigBoldRed" mode="Transparent" x="0" y="0" width="555" height="44"/>
</rectangle>
//style applied to a the text field
<staticText>
<reportElement style="BigBoldRed" x="0" y="0" width="555" height="66"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Monthly Customer Invoices]]></text>
</staticText>
Mixing the internal and reusable styles in report
Using the power of HTML to style your report
For example, your text field has following expression which includes HTML tags (i.e. <li>) and you want the HTML tags to work in your report design:
"<li>"+"Invoice # "+$F{InvoiceID}+", "+
$F{CustomerName}+" purchased
"+$F{ProductName}+" in
"+$F{InvoicePeriod}+" (Invoice value:
\$ "+$F{InvoiceValue}+")"+"
Solution is simple, just set “Markup” property of the text field to “Styled” and that it.
I have taken permission from the author to copy code chunk from his JasperReports cookbook in this post.