Jump to content
JasperReports Library 7.0 is now available ×

Loading of TTF fonts from classpath broken?


Recommended Posts

By: Andrew Matthews - namxat

Loading of TTF fonts from classpath broken?

2003-08-21 05:24

Hello. Our company uses reports that embed a TrueType barcode font. I have repeatedly run into the all-too-familiar "cannot load font from location" problem when attempting to export filled reports that use the barcode font.

 

In my experiments, I have found that the font can only be found if it is in the current working directory for the VM, or in a subdirectory of the current working directory, and the pdfFontName tag has the correct relative name.

 

Example:

Current Working Directory: c:work

Directory in which font resides: c:workfonts

Font name: barcodeFont.TTF

Font tag in XML: pdfFontName="fonts/barcodeFont.TTF"

 

Under the above circumstances, the font is loaded just fine, so long as 'c:work' is the current working directory. However, whenever the font is not either accessible from the current working directory or a subdirectory of the current working directory, it cannot be found.

 

I know that, invariably, someone will reply with a post telling me that I have to make the font accessible from the classpath. Been there, done that; it still doesn't work. Example scenario:

 

Current working directory is c:, NOT c:work, in this example.

 

-- added the c:workfonts directory to the classpath, and changed the pdfFontName value to "barcodeFont.TTF". Still couldn't load the font.

 

-- explicitly added c:workfontsbarcodeFont.TTF to the classpath. Still couldn't load the font.

 

-- Added the "-Djava.ext.dirs=c:\work\fonts" flag to my JVM invocation. Still couldn't load the font.

 

-- Changed my working directory to c:work. This time, the font loaded.

 

I don't know if this behavior is peculiar to my situation, or if it is an issue in other situations. I am using version 0.5.0 of the JasperReports API, on a PC running Windows 2000 Pro. This is not a Tomcat/JSP environment I am working in, although that could change soon.

 

I would really like to avoid having to explicity set the "-Duser.dir=c:\work" flag ( assuming it would remedy the situation; I haven't tested it yet ) every time I have to export reports that use external fonts. It's a particularily hackish solution, and not elegant at all.

 

 

 

 

By: Andrew Matthews - namxat

Follow up: a bit more info

2003-08-21 05:31

I neglected to mention this, but I actually have the barcode TTF font installed on my box as a system font, and also find it necessary to export filled reports to html. I can export to html without any problems, presumably because the font is loaded from the system, and not from some dubious classloader.

 

If anybody knows what might be going on with this font issue, please respond. Embedding fonts in PDF files shouldn't be rocket science.

 

 

 

 

By: Andrew Matthews - namxat

I figured it out.

2003-08-21 08:26

I failed to mention that I am executing my code from a archive, via the '-jar' flag. I also failed to realize that if the '-jar' option is used, the '-classpath' option is ignored altogether. I think I have a handle on the problem now.

 

 

 

 

By: Roger Choi - roger644

RE: Loading of TTF fonts from classpath broke

2003-09-26 06:27

You can have the fonts packaged in a jar file, and put the jar into your classpath. Say, you have the following files:

 

c:fontsreportxxxx.ttf

c:fontsreportyyy.ttf

 

you cd to c:fonts

type:

jar cvf report-fonts.jar report*.ttf

 

put this jar file into your classpath:

 

In your xml template:

 

Add this:

<reportFont name="Report_Normal" isDefault="false" fontName="XXX" size="10" isBold="false" pdfFontName="report/xxx.ttf" pdfEncoding="Identity-V" isPdfEmbedded="true"/>

 

Sometimes when you load the class with an Application Server or Servlet Server, you will encounter some strange problem in the JVM classloader problem. Then you need to write some code to debug it, something like this:

 

Thread.currentThread().getContextClassLoader().getResources(fontName);

 

Good luck.

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...