Jaspersoft studio: UTF-8 encoding issue - java

I am using Jaspersoft Studio final version (v6.1.1). I have designed my report and it contains Turkish characters. When exporting the report from Studio as PDF Turkish characters are not shown although I've set the Studio workspace encoding to UTF-8. Also I have embed the jrxml report file in my JSF app and export the report, the result is the same.
Do I have to configure something more?

I was able to solve the problem. In the Jaspersoft Studio IDE, change encoding to UTF-8 in Window/Prefrences/General/Workspace. Afterwards click any field, parameter, or text in the IDE and from the Properties/Advanced menu select PDF/PDF Encoding. I hope it will help.

I think i have found an easier and more general solution. Instead of changing encoding of every field, you can do it using java coding from your web application`s controller. Below is my solution, i hope it helps a bit. Just put the font to your webapp folder. And change the encoding programatically. BTW greetings from turkey :)
JRDesignStyle jrDesignStyle = new JRDesignStyle();
/*Set the Encoding to UTF-8 for pdf and embed font to arial*/
jrDesignStyle.setDefault(true);
String fontPath = req.getSession().getServletContext().getRealPath("/jasper/arial.ttf");
jrDesignStyle.setPdfFontName(fontPath);
jrDesignStyle.setPdfEncoding("Identity-H");
jrDesignStyle.setPdfEmbedded(true);
jasperPrint.addStyle(jrDesignStyle);
JRPdfExporter exporter = new JRPdfExporter();

Related

Eclipse won't save html file with dynamic charset

I'm using Thymeleaf template engine, and want to get the charset attribute value for the meta tag on the head block from my project properties, i.e :
<meta th:charset="${#environment.getProperty('html.charset')}">
But when I want to save my html file I get this pop-up error :
Save could not be completed. Try File > Save As... if the problem persists.
Reason:
Character encoding "${#environment.getProperty('html.charset')}" is not a legal character encoding.
I even tried to save the file from outside of eclipse, but when back on eclipse and want to open the file, I couldn't access it and got this error :
Unsupported Character Encoding
Character encoding "${#environment.getProperty('html.charset')}" is not supported by this platform.
Set Encoding...
I tried to suspend all validators on eclipse Preferences but this doesn't resolve the problem.
Any ideas for help ? Thanks in advance.
I'm on Mac OS and using Eclipse IDE for Enterprise Java and Web Developers, Version: 2021-09 (4.21.0).
The charset value is being used, regardless of the prefix, to read and write the file correctly. That there is a prefix on the attribute in a non-prefixed meta tag is a clue that it should be ignored by the normal encoding detection. Please open a bug report for this.
For a workaround, force the use of the encoding you want using the file's Properties dialog's Resource page (get there by right-clicking on the file).

Encoding for project set to UTF-8, default charset returns windows-1252

I've ran into an issue with encoding. Not sure if it's IDE related but I'm using NetBeans 7.4. I got this piece of code in my J2EE project:
String test = "kukuřičné";
System.out.println(new String(test.getBytes("UTF-8"))); // should display ok
System.out.println(new String(test.getBytes("ISO-8859-1")));
System.out.println(new String(test.getBytes("UTF-16")));
System.out.println(new String(test.getBytes("US-ASCII")));
System.out.println(new String(test.getBytes("windows-1250")));
System.out.println(test); // should display ok
And when I run it, it never displays properly. UTF-8 should be able to print that out ok but it doesn't. Also when I tried:
System.out.println(Charset.defaultCharset());
it returned windows-1252. The project is set to UTF-8 encoding. I've even tried resaving this specific java file in UTF-8 but it still doesn't display properly.
I've tried to create J2SE project on the other hand and when I run the same code it displays properly. Also the default charset returns UTF-8.
Both projects are set the UTF-8 encoding.
I want my J2EE project to act the same like the J2SE one. I didn't notice this issue until I updated my java to version 1.7.0_51-b13 but again I'm not sure if that is related.
I'm experiencing the same issue like this guy: http://forums.netbeans.org/ptopic37752.html
I've also tried setting the default encoding for the whole IDE: -J-Dfile.encoding=UTF-8 but it didn't help.
I've noticed an important fact. When I create a new web application it displays ok. When I create new Maven web application it displays incorrectly.
Found the same issue here: https://netbeans.org/bugzilla/show_bug.cgi?id=224526
I still haven't fixed it yet. There's still no solution working.
In my pom.xml the encoding is set properly, but it still shows windows-1252 in the end.
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
I've spend few hours trying to find the best solution.
First of all this is an issue of maven which picks up platform encoding and uses it even though you've specified different encoding to be used. Maven doesn't seem to care (it even prints to console that it's using UTF-8 but when you run a file with the code above, it won't display properly).
I've managed to tackle this issue by setting a system variable:
JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF8
There should be another option instead of setting system variables and that is to set it as additional compiler parameter.
like javac -Dfile.encoding=UTF8
You are mixing a few concepts here:
the project encoding is the encoding used to save the Java source files (xxxx.java) - it has nothing to do with how your code executes
test.getBytes("UTF-8") returns a series of bytes representing your String in UTF-8 encoding
to recreate the original string, you need to explicitly give the encoding, unless it is the default of your machine: new String(test.getBytes("UTF-8"), StandardCharsets.UTF_8)

Specifying a base or default font with PDFBox

Is there a system property or other parameter that can be passed to PDFBox when attempting to convert from a PDF document into an Image? I am utilizing the provided PDF2Image class like so:
String[] args = new String[]{"-imageType", "jpg", "myfile.pdf"};
org.apache.pdfbox.PDFToImage.main(args);
It works great on Windows and fails with this warning on Ubuntu:
[org.apache.pdfbox.util.PDFStreamEngine] java.lang.NullPointerException java.lang.NullPointerException at org.apache.pdfbox.pdmodel.font.PDType1Font.getawtFont(PDType1Font.java:261)
I assume from my research that this is a problem with a "missing" font, but I've found very little help so far using google and the projects website.
I've tried both versions 1.7.1 and 1.8.2 of PDFBox.
Similar question here: setting ttf for PDF to Image on PDFbox

Use custom fonts when creating pdf using iReport

iReport Version : 3.7.1
I approached this problem in two ways.
My first method
I am creating a pdf page in iReport and I have one of the fields (from database) set to a font Gujarati-Salarl (A font for a regional language of India).
This font is available in the font list of iReport.
When I hit preview I get the desired output , with the field in the required font style.
Everything till here is fine.
But the pdf file generated still contains the same original font in English characters.
The result is same whether i call the report from the application or from a JAVA file.
After a bit of study I found this , a second method
JRProperties.setProperty("net.sf.jasperreports.default.pdf.font.name", "C:\\Gujrati-Saral-1.ttf");
This sets the font of the pdf to the ttf font provided but now the entire pdf comes in the Guarati-saral font which is not what I am looking for..
Is there a way to specifically apply this font only to that one particular field?
or
when going by the second method is there a way to not applying the font Guarati-saral to all other fields except the required one ?
You should use the Font Extensions mechanism.
After creating the font and exporting it as a jar file you should add generated jar file to the application classpath.
a) Creating new font in iReport (via Options -> Fonts -> "Install Font" Button)
b) Exporting the new font (or the existing one) as jar file in iReport (via Options -> Fonts -> "Export as extension" Button)
a) Creating new font in JasperReports Studio (via Window -> Preferences -> JasperStudio -> Fonts -> "Add" Button)
b) Configure your fonts
c) Create JAR with your fonts in it
You can find more information about using Font Extensions here.
if you have problems displaying UTF-8 characters in the pdf,
from version 4.8.0 of the IReport, no need to do nothing!
Just change the font of the cells in the table from sanserif to Dejavu sans.
thats it.
For those, who need Russian language in Jasper Reports (at least in opennms of old versions), following combination does the trick without using the Font Extension:
Font Name: SanSerif
PDF Font: Courier
PDF Encoding: CP1251
PDF Embedded: Yes (not sure, may be it works even without)
Alexey Bakhmutov
Select the field and go to filed properties and change the
select DejaVu Sans as Font Name
select Pdf Encoding to CP1256 (Arabic)
select pdf font name to Courier
with these changes in I got arabic data showing in PDF.

Saving JSP as UTF-8 in NetBeans

i've got some jsp files from another developers and now need to work with them. When i add to the document any UTF-8 char and want to save the document, NetBeans automatically offers me saving in ISO-8859-1.
Actually i'm getting this message from NetBeans:
The index.jsp contains characters
which will probably be damaged during
conversion to the ISO-8859-1 character
set. Do you want to save the file
using this character set? (Yes/No)
NB didn't offer me any other option like saving the file as UTF-8 (as it should be already written in).
I don't know how to save those jsp files in the character set they are already written in.
And don't tell me, that changing the content of the file itself (which is uneffective due to including headers etc. from other files) is the only way...
http://forums.netbeans.org/topic8750.html
Firstly; don't forget to consider this line at top:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
Secondly;
In the NetBeans folder there is a config file. There should be a line like that:
netbeans_default_options="-J-Xms32m -J-Xmx128m -J-XX:PermSize=32m -J-XX:MaxPermSize=160m -J-Xverify:none -J-Dapple.laf.useScreenMenuBar=true"
Add this to the end of the line:
-J-Dfile.encoding=UTF-8
Thirdly:
NetBeans implements a project encoding setting.
To change the language encoding for a project:
Right-click a project node in the Projects windows and choose Properties.
Under Sources, select an encoding value from the Encoding drop-down field.
The encoding affects at least:
* how non-ASCII characters are displayed in the editor window when you open files
* Java file compilation of sources containing non-ASCII identifiers, string literals, or comments
* textual search for international characters over the project
Starting from NetBeans IDE 6.8, you can also specify the encoding that will be used at runtime. For example, this can be useful when the encoding for the operating system on which the application will run is different from your project's encoding.
To specify the encoding to be used at runtime:
In the Files window for your project, open nbproject > private > private.properties
Add the following line to the private.properties file and save changes:
runtime.encoding = < encoding >
This encoding will override the encoding setting for your project and will be used when running your application.
In general,
*.properties files always use ISO-8859-1 encoding plus \uXXXX escapes. (International characters will be displayed natively in the editor but stored as an escape on disk.)
*.xml files and some *.html files can specify their own encodings, regardless of the project encoding. For such files, the IDE's editor ignores the project encoding.
These may help you.
Sources for my answer that I used:
Link1: http://forums.netbeans.org/topic33.html
Link2: http://wiki.netbeans.org/FaqI18nProjectEncoding

Categories

Resources