Hello,
we are developing a desktop application which uses JasperReports for report generation and printing. So far the Jasper reports works well for us. However, we have noticed, that the reports are sometimes are not the same when generated on linux or windows machine. Further, we have noticed that is due to different versions of true type fonts on different machines. Therefore, we have an simple idea to distribute a font set (e.g. Arial Times and Courier) together with our application and force our application to use only th
ese fonts for report generation. But, I don't know how to do it.
I have read lot of articles in this forum and the only solution I had found was --" Create a jar file with your fonts and put it on the classpath", I tried it but without succ
ess. I suppose that I have missed some simple thing.
Therefore, my question is:
In the .jrxml file is the style section where I define that the style uses the font with name "Arial". How can I force the application to load the font file 'arial.ttf'
from my jar file (or database blob field or whatever...) and use it for the report filling instead of the system font 'arial.ttf' loaded by the Java VM from some system locati
on (e.g. /usr/share/fonts/ttf/ ).
Any advice or link to soulition will be helpful.
Honza Hubeny
we are developing a desktop application which uses JasperReports for report generation and printing. So far the Jasper reports works well for us. However, we have noticed, that the reports are sometimes are not the same when generated on linux or windows machine. Further, we have noticed that is due to different versions of true type fonts on different machines. Therefore, we have an simple idea to distribute a font set (e.g. Arial Times and Courier) together with our application and force our application to use only th
ese fonts for report generation. But, I don't know how to do it.
I have read lot of articles in this forum and the only solution I had found was --" Create a jar file with your fonts and put it on the classpath", I tried it but without succ
ess. I suppose that I have missed some simple thing.
Therefore, my question is:
In the .jrxml file is the style section where I define that the style uses the font with name "Arial". How can I force the application to load the font file 'arial.ttf'
from my jar file (or database blob field or whatever...) and use it for the report filling instead of the system font 'arial.ttf' loaded by the Java VM from some system locati
on (e.g. /usr/share/fonts/ttf/ ).
Any advice or link to soulition will be helpful.
Honza Hubeny
9 Answers:
Posted on April 8, 2008 at 10:02pm
A couple of things that I did to make this work for me:
1) Put the fonts in the default package (i.e. at the very top level of the jar).
2) Use the full file name in your text field elements. For example
1) Put the fonts in the default package (i.e. at the very top level of the jar).
2) Use the full file name in your text field elements. For example
Code: |
<font fontName="Arial" pdfFontName="arialbd.ttf" size="10" isBold="true"/></td></tr></tbody></table><br /> (note that in this example I was only worried about the pdf font). |
Posted on April 9, 2008 at 5:29am
The Telos's solution works fine with report export to pdf. But it does not work in the JRViewer
I made a simple test.
1) I delete the Times.ttf font from the system font directory.
2) I made a jar which contains this font in the default package
3) I add the style line
I made a simple test.
1) I delete the Times.ttf font from the system font directory.
2) I made a jar which contains this font in the default package
3) I add the style line
Code: |
<style <br /> name="Roman"<br /> isDefault="false"<br /> fontName="Times New Roman"<br /> fontSize="48"<br /> pdfFontName="Times.ttf"<br /> pdfEncoding="Cp1250"<br /> isPdfEmbedded="true"<br /> ><br /> </td></tr></tbody></table> to the jrxml file.<br /> 4) start the application<br /> <br /> The results are following. The application cannot find the font "Times New Roman" and it silently replace this font with some default font and use this default font in the JRViewer and for printing. On the other side the export to pdf works, the exporter uses the correct Times.ttf font.<br /> <br /> Please, give me an advice how to force the application to use my ttf fonts in the JRViewer and for printing.<br /> <br /> Thank you,<br /> <br /> Honza Hubeny |
Posted on April 10, 2008 at 5:49am
Changing "Times New Roman" to "Times.ttf" in style element does not make any change. The Java VM still cannot find the Times font and it still silently replace the Times font with first font available.
Futher, I have tried experiments with custom class loader and url handler factory set via REPORT_CLASS_LOADER REPORT_URL_HANDLER_FACTORY, but without any valuable results. The classloader or url factory methods are not called for the font resource.
so the question remains the same:
Please, give me an advice how to force the application to use my ttf fonts in the JRViewer and for printing.
Thank you,
Honza Hubeny
Futher, I have tried experiments with custom class loader and url handler factory set via REPORT_CLASS_LOADER REPORT_URL_HANDLER_FACTORY, but without any valuable results. The classloader or url factory methods are not called for the font resource.
so the question remains the same:
Please, give me an advice how to force the application to use my ttf fonts in the JRViewer and for printing.
Thank you,
Honza Hubeny
Posted on April 10, 2008 at 12:49pm
JasperReports treats fontName attributes as Java/AWT font family names, therefore in order for a fontName to be resolved, the required font must be available in the JVM.
There is no built-in mechanism in JasperReports to register AWT fonts, so if you need to do this you'll have to do it yourself. If you use Java 1.6, you can do it programmatically by loading the fonts via [url=http://java.sun.com/javase/6/docs/api/java/awt/Font.html#createFont(int,%20java.io.InputStream)]Font.createFont() and registering them via [url=http://java.sun.com/javase/6/docs/api/java/awt/GraphicsEnvironment.html#...(java.awt.Font)]GraphicsEnvironment.registerFont().
Regards,
Lucian
There is no built-in mechanism in JasperReports to register AWT fonts, so if you need to do this you'll have to do it yourself. If you use Java 1.6, you can do it programmatically by loading the fonts via [url=http://java.sun.com/javase/6/docs/api/java/awt/Font.html#createFont(int,%20java.io.InputStream)]Font.createFont() and registering them via [url=http://java.sun.com/javase/6/docs/api/java/awt/GraphicsEnvironment.html#...(java.awt.Font)]GraphicsEnvironment.registerFont().
Regards,
Lucian
Posted on May 14, 2008 at 7:43am
Hi!
I'm having the same problem :(
I'm using ttf fonts in my reports for my servlet application (under Ubuntu Linux) and I make registration of them like:
I'm having the same problem :(
I'm using ttf fonts in my reports for my servlet application (under Ubuntu Linux) and I make registration of them like:
Code: |
<br /> FontFactory.register("erasb.ttf","Eras Bk BT"«»);<br /> FontFactory.register("erasm.ttf","Eras Md BT"«»);<br /> ...<br /> </td></tr></tbody></table><br /> (This I try using all kind of possible alias, and without alias...)<br /> <br /> After this I try to look which fonts I've succesfully registered:<br /> <br /> <table align="center" border="0" cellpadding="3" cellspacing="1" width="90%"><tbody><tr><td><b>Code:</b></td></tr><tr><td><pre><br /> System.out.println("These fonts were registered at the FontFactory:\r\n"«»);<br /> for (Iterator i = FontFactory.getRegisteredFonts().iterator(); i.hasNext(); ) <br /> {<br /> System.out.println((String) i.next());<br /> System.out.println("\r\n"«»);<br /> }<br /> </td></tr></tbody></table><br /> <br /> And It shows correctly the fonts I've installed.<br /> <br /> But anyway trying everything to change in the .jxml I can't make it work :(<br /> This is how it's looks a style in my jxml:<br /> <table align="center" border="0" cellpadding="3" cellspacing="1" width="90%"><tbody><tr><td><b>Code:</b></td></tr><tr><td><pre><br /> <style <br /> name="tituloSeccion1"<br /> isDefault="false"<br /> forecolor="#008142"<br /> fontName="Eras Bk BT"<br /> isBold="false"<br /> pdfFontName="erasb.ttf"<br /> ><br /> </td></tr></tbody></table><br /> <br /> Where pdfFontName I tried already using "Eras Bold BT", "Eras Bk MT", ... (That are the names in windows).<br /> <br /> Can someone help me? |
Posted on May 14, 2008 at 9:42am
JasperReports uses fonts from two sources:
Java/AWT fonts are used to measure report texts and to preview or print the report. The fontName attribute specifies the name/family of the Java font to use for a text field.
PDF/iText fonts are used when exporting a report to PDF. The pdfFontName attribute is used to specify the location/alias of the iText font. [/ol]
What you did with FontFactory was to register an PDF/iText font. This would allow you to use "Eras Bk BT" (or the real font name) as pdfFontName. But if you want to make sure that JR properly processes the text, you'll have to make your font available as a Java/AWT font as well.
Regards,
Lucian
What you did with FontFactory was to register an PDF/iText font. This would allow you to use "Eras Bk BT" (or the real font name) as pdfFontName. But if you want to make sure that JR properly processes the text, you'll have to make your font available as a Java/AWT font as well.
Regards,
Lucian
Posted on May 14, 2008 at 10:16am
Hi lucianc,
Thank you for your answer, I didnt know about Java/AWT.
Right now I just need export to PDF so as I can understand it's enough to use registerFont right?
I got it somehow working. I said somehow because I'm able finally to render the pdf using the font, but just some text.
The problem I'm having is that for all the text I've define some styles like I showed before:
Thank you for your answer, I didnt know about Java/AWT.
Right now I just need export to PDF so as I can understand it's enough to use registerFont right?
I got it somehow working. I said somehow because I'm able finally to render the pdf using the font, but just some text.
The problem I'm having is that for all the text I've define some styles like I showed before:
Code: |
<br /> <style <br /> name="cabeceraCampo"<br /> isDefault="false"<br /> forecolor="#008142"<br /> fontName="Eras Md BT"<br /> fontSize="8"<br /> pdfFontName="Eras Md BT"<br /> ><br /> </td></tr></tbody></table><br /> <br /> But then I've notice each text tag has a value of pdfFontName (I generate them with iReport).<br /> <br /> I realize this because I got some text correct rendering that has the same style/pdfFontName and text/pdfFontName.<br /> <br /> So I suposse to delete all the pdfFontName because jasper give priority to the tag pdfFontName in the textfield that to the style? or there're some option that I can force to take from style?<br /> <br /> Thank you in advance |
Posted on May 14, 2008 at 11:43am
kile wrote:
Not exactly correct, see [url=http://www.jasperforge.org/jaspersoft/opensource/business_intelligence/j... FAQ.
Element level attributes always override style attributes. I don't know how to address this iReport issue, you might find something relevant on the [url=http://www.jasperforge.org/index.php?option=com_joomlaboard&Itemid=215&f... forum.
Regards,
Lucian
Right now I just need export to PDF so as I can understand it's enough to use registerFont right? |
Not exactly correct, see [url=http://www.jasperforge.org/jaspersoft/opensource/business_intelligence/j... FAQ.
But then I've notice each text tag has a value of pdfFontName (I generate them with iReport). I realize this because I got some text correct rendering that has the same style/pdfFontName and text/pdfFontName. So I suposse to delete all the pdfFontName because jasper give priority to the tag pdfFontName in the textfield that to the style? or there're some option that I can force to take from style? |
Element level attributes always override style attributes. I don't know how to address this iReport issue, you might find something relevant on the [url=http://www.jasperforge.org/index.php?option=com_joomlaboard&Itemid=215&f... forum.
Regards,
Lucian