Pointer
Pointer

Reputation: 2186

Why does html tags (s, strong) not work in jasper reports?

I'm add in static text html tag but after click on preview bold and strikethrough not show correctly.

Also when load data from database it is not show correctly.

Example

Any solution?

Upvotes: 3

Views: 11443

Answers (2)

yglodt
yglodt

Reputation: 14551

In case you face the situation that bold fonts work in preview, but not in the resulting PDF, add this dependency:

<dependency>
    <groupId>net.sf.jasperreports</groupId>
    <artifactId>jasperreports-fonts</artifactId>
    <version>6.19.1</version>
</dependency>

Upvotes: 1

Petter Friberg
Petter Friberg

Reputation: 21710

Jasper Report does not support all html tags, the support tags are defined in Styled Text Sample

As you can see <s> and <strong> tag are not supported.

Your choice is to replace them with <font style="text-decoration: line-through"> and <b> if you like to use html

or

<style isStrikeThrough="true"> and <style isBold="true"> and then use styled text instead of html.

If you have dynamic data you can use java to replace it

${myField}.replace("<s>","<font style=\"text-decoration: line-through\">").
        replace("</s>","</font>").
        replace("<strong>","<b>").replace("</strong>","</b>")

If you need to replace multiple tags, I would recommend creating a method in java (static) and call this method instead of executing the replace within the report

Example

jrxml

<?xml version="1.0" encoding="UTF-8"?>
<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="html" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="fe5b2242-b491-46ba-8456-aa71ae5e2212">
    <queryString>
        <![CDATA[]]>
    </queryString>
    <title>
        <band height="53" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="210" height="50" uuid="e462bb03-e884-4b5b-b41f-2867a4bd63b2"/>
                <textElement markup="html"/>
                <textFieldExpression><![CDATA["<s>&lt;s&gt;</s> and <strong>&lt;strong&gt;</strong> will not work but <font style=\"text-decoration: line-through\">&lt;font style=\"text-decoration: line-through\"&gt;</font> and <b>&lt;b&gt;</b> will"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="220" y="0" width="220" height="50" uuid="744bb631-d03a-452e-ae5e-19e7ef5a378a"/>
                <textElement markup="html"/>
                <textFieldExpression><![CDATA["With java however you can replace'em and both <s>&lt;s&gt;</s> and <strong>&lt;strong&gt;</strong> will work".replace("<s>","<font style=\"text-decoration: line-through\">").replace("</s>","</font>").replace("<strong>","<b>").replace("</strong>","</b>")]]></textFieldExpression>
            </textField>
        </band>
    </title>
</jasperReport>

Result

result

Upvotes: 7

Related Questions