How to escape special characters in JasperReports? - jasper-reports

How to escape special characters in JasperReports?
I want to escape special characters like :- é, â, è, ^, # in jasper reports, so is there any way we can escape this characters ?

Try this:
instead of 'é' use '\u00E9'
instead of 'â' use '\u00E2'
instead of 'è' use '\u00E8'
etc.

If you want to escape special XML characters like <, > you can use CDATA:
<staticText>
<reportElement x="0" y="0" width="100" height="14"/>
<textElement/>
<text><![CDATA[This is a test:< ]]></text>
</staticText>
Special characters like é, â, è should work great if you specify the character encoding at the beginning of the jrxml file and encode the file correctly.
<?xml version="1.0" encoding="UTF-8"?>
If the exported report shows invalid characters you should check the jasper export. For instance for pdf export can find info here.

Saving jrxml file in UTF-8 encoding does not solve problem, because .jrxml file is compiled into .jasper file and it looses information about used encoding.
Final report is then missing special characters.
Problem is solved when using /u00XX escape characters inside CDATA XML tag.
In this case the special characters are present in output PDF file.
Outside CDATA tag are escape characters not interpreted correctly.

Related

How to import HTML content (No Styles) into Text Field?

I have some information in an HTML file. There are more HTML files, which all contains strings which depends on an input parameter(bold).
as an Example:
file_**01**.html --> contains: "Bob Bakers"
file_**02**.html --> contains: "Alice Armstrong"
The HTML files are stored in the repository and I would like to import/load the content of one of these files into a textfield. With some images I've done something similia like:
"repo:/home/res/images/" + $F{html_param} + ".html"
And depending on the parameter, it loads the right image. I need to do the same with the strings from the HTML files.
If it is not possible to do this with an Text Field, are there other options to import/load the content and show them in the report?
You can do something like this
<textField>
<reportElement .../>
<textElement markup="html"/>
<textFieldExpression>new String(net.sf.jasperreports.repo.RepositoryUtil.getInstance($P{JASPER_REPORTS_CONTEXT}).getBytesFromLocation("repo:/home/res/images/" + $F{html_param} + ".html"), java.nio.charset.StandardCharsets.UTF_8)</textFieldExpression>
</textField>
This would work as long as the html file structure is simple, otherwise markup="html" might fail to interpret the document.

how to give space after every character of a string in "jasper report"

How to give space after every character of a string in jasper reports. I need this functionality to print date according to cheque format. I am new to jasper. Please help me in this.
Adding blank to every character
unfortunately Jasperreports doesn't support letter spacing (source)
However you can always use java in text fields to tranform the string and add a blank character after each character.
For example in case you have a field myString (source for java part):
<textFieldExpression>
<![CDATA[$F{myString}.replace("", " ").trim()]]>
</textFieldExpression>
More specifically on dates
Java also provides nice feature to format a date. You might consider this as you specified you were working with dates.
An example of date formatting :
<variable name="dateFormatter" class="java.text.SimpleDateFormat">
<variableExpression><![CDATA[new java.text.SimpleDateFormat("yyyy-MM-dd")]]></variableExpression>
</variable>
<textFieldExpression>
<![CDATA[$V{dateFormatter}.format($F{myDate})]]>
</textFieldExpression>
See full documentation here
replaceAll() works fine in jasper reports only if RegularExpression is added in the imports.
value="com.sun.org.apache.xerces.internal.impl.xpath.regex.RegularExpression"/>
$P{mystring}.replaceAll("", " ").trim()

Jasper Reports Polish characters [duplicate]

I am trying to export jasper as pdf but It does not show the cyrillic values. When I export it as excel it does show and the output is fine, but when I try to export is as PDF it does not export the cyrillic values. The cyrillic values are not written in cyrillic font, they are written as cyrillic keyboard.
The code I use to export is:
JRExporter e = new JRPdfExporter();
e.setParameter(JRPdfExporterParameter.JASPER_PRINT, jasperPrint);
e.setParameter(JRPdfExporterParameter.OUTPUT_STREAM, outStream);
e.setParameter(JRPdfExporterParameter.OUTPUT_FILE_NAME, NAME);
I even tried to specift the parameter below:
e.setParameter(JRPdfExporterParameter.CHARACTER_ENCODING, "UTF-8");
but did not succeed. Any suggestions?
Jasper report uses iText and always when a char is not rendered in pdf this should be the checklist:
Is my actual .tff supported (OpenType) and can the font actually render the character. Not all fonts render
all characters in UTF-8, see How can I test if my font is rendered correctly in pdf?
Do I pass correct encoding to iText. In doubts (or in general) use the encoding Identity-H this is recommend for newer PDF standards and gives you the ability to mix different encoding.
Is my font embedded so that if I share the pdf also computers not having this font can display the content?
How can I ensure this is JasperReport?
The deprecated method was to set attributes on the textElement
<textElement>
<font pdfFontName="Helvetica" pdfEncoding="Identity-H" isPdfEmbedded="true"/>
<paragraph lineSpacing="Single"/>
</textElement>
The current non deprecated method v 3-6, is to add Font Extensions and this is easily achieved by using tools like iReport or JasperSoft Studio that can generate a .jar of your font extension so that you can include it in your classpath directly.
How to generate font extension .jar using iReport or JasperSoft Studio.
EDIT: The problem of OP was 1 on checklist (.ttf font could not render), but surely he should consider both 2 and 3 using non deprecated method.

Ireport to CSV is appending an  to all currency cells

It seems when I run my report and export it to Excel .CSV all my currency values seem to append an  at the beginning.
My XML is as follows:
</textField>
<textField isStretchWithOverflow="true" pattern="£ #,##0.00" isBlankWhenNull="false">
<reportElement x="760" y="0" width="100" height="20" uuid="cba1b317-f85f-4b3b-9549-66389c451cfa"/>
<textElement>
<font size="10" pdfEncoding="Cp1252"/>
</textElement>
<textFieldExpression><![CDATA[$F{ItemRateBilled}]]></textFieldExpression>
I've changed the pattern numerous times to no avail. It seems the following is the issue however, I can't figure out how to resolve:
The pound symbol is Unicode codepoint 163. If it's written to a UTF-8 encoded file, it's written as the sequence 194, 163. When the file is imported in Excel, a different encoding is assumed, probably the default ANSI code page. Therefore, the sequence 194, 163 is not interpreted as one Unicode codepoint. Instead, it's interpreted as the sequence  and £.
So, either make Excel interpret the file as a UTF-8 encoded file, or write the file yourself by specifying the System.Text.Encoding.Default codepage.
Current error as a CSV:
This is not an issue originating from Jasper but from Excel.
If you take a look into the file with a program able to interpret UTF-8 (like Notepad++), this should not happen at all.
CSV files exported from Jasper should not be opened directly. Instead, Excel should be opened and in the data tab the import from file function should be used.
When importing the CSV file, change the input encoding to UTF-8. Then Excel will interpret the file correctly.

Japanese characters not printing in pdf [duplicate]

I've hardcoded a Text field with the value
":$£Ω€απ⅔:"
to test if these characters will show in PDF
But it only prints as
:$£€:
I've tried changing the font to "Symbol" but that does not do the trick.
Note: I'm using iReport 5.5
I tried your characters using font-extensions in iReport, These are the steps that I took
Download (or use one on your pc) your desired ttf font (it need to support the characters), I used dejavu-serif.
Install the font in iReport or in Jasper soft studio (click links to understand how)
I used iReport 5.6 and did set the PDF-Encoding: Idenity-H (unicode with horizontal writing) and embedded the font in pdf. This was the code of my textField
<textField>
<reportElement x="8" y="15" width="100" height="20" uuid="41dc1200-091f-4799-a1d2-4622f88a0e84"/>
<textElement>
<font fontName="DejaVu Serif" size="13"/>
</textElement>
<textFieldExpression><![CDATA[":$£Ω€απ⅔:"]]></textFieldExpression>
</textField>
Export the font extension (this creates a .jar)
Add the jar to your classpath.
Export to pdf and enjoy the result.
This is an example of how to use any arbitrary font, if you like to
use DejaVu Sans or DejaVu Serif you can just include the jasper
report fonts library in classpath, hence these font are already included and mapped in library
I had posted a similar question and the above technique solved my problem, until one fine day I restarted the JasperServer context. I was now getting "PDF encoding not found" error when I tried to export the report to pdf.
To solve this problem I had to copy the iText-Asian.jar and Font-extension.jar in the lib folder of the jasperserver context.
"jasperserver/WEB-INF/lib". Once done restart the context and problem solved.
Hope it helps someone.