Non embedded font name example?

Request for help on DynamicReports

Moderator: ricardo

Non embedded font name example?

Postby ddavidson » Mon Apr 17, 2017 1:06 pm

How can I reference non embedded fonts in my PDF. I want to use an existing OS font.

So if I wanted to use "Courier" font, I was hoping I just do:
stl.style().setFontName("Courier")

Do this need to be in fonts.xml? If so, would that look like since I am not supplying the TTF file?

Thanks in advance!
ddavidson
 
Posts: 7
Joined: Mon Apr 17, 2017 12:59 pm

Re: Non embedded font name example?

Postby andreas.hackl » Tue Apr 18, 2017 6:35 am

Hallo!

You can do it programmatically (although this is deprecated):

Code: Select all
FontBuilder font = stl.font();
font.setFontName("Courier");
font.setPdfFontName("Courier");
font.setPdfEmbedded(true);

stl.style().setFont(font)


...or you define a font extension in your fonts.xml file:

Code: Select all
<fontFamily name="Courier">
   <normal>Courier</normal>
   <pdfEncoding>Cp1252</pdfEncoding>
   <pdfEmbedded>true</pdfEmbedded>
</fontFamily>


Best regards,
Andy
andreas.hackl
 
Posts: 45
Joined: Tue Jan 28, 2014 4:47 pm

Re: Non embedded font name example?

Postby ddavidson » Fri Apr 21, 2017 2:50 pm

So that was close. I found this worked:
Code: Select all
      FontBuilder font = stl.font();
      font.setFontName("Courier New");
      font.setPdfFontName("c:/windows/fonts/cour.TTF");
      rootStyle.setFont(font);


Doesn't seem like "font.bold()" matters since there is only the one TTF file specified.
I guess I need a distinct FontBuilder should I also want bold/italic styles?

Thanks!
ddavidson
 
Posts: 7
Joined: Mon Apr 17, 2017 12:59 pm

Re: Non embedded font name example?

Postby andreas.hackl » Mon Apr 24, 2017 6:36 am

Hi!

The example i posted worked for me. What was the problem for you?

If you wanna provide different ttf-files for normal/bold/italic style you should probably define a font extension like described here:

http://www.dynamicreports.org/documentation/fonts

Example for Courier New:

Code: Select all
<fontFamily name="Courier New">
   <normal>c:/windows/fonts/cour.ttf</normal>
   <bold>c:/windows/fonts/courbd.ttf</bold>
   <italic>c:/windows/fonts/couri.ttf</italic>
   <boldItalic>c:/windows/fonts/courbi.ttf</boldItalic>
   <pdfEncoding>Cp1252</pdfEncoding>
   <pdfEmbedded>true</pdfEmbedded>
</fontFamily>


Best regards,
Andy
andreas.hackl
 
Posts: 45
Joined: Tue Jan 28, 2014 4:47 pm

Re: Non embedded font name example?

Postby ddavidson » Mon Apr 24, 2017 12:41 pm

I am on Windows 7

So using you code as is, I get:

net.sf.jasperreports.engine.util.JRFontNotFoundException: Font "Courier" is not available to the JVM. See the Javadoc for more details/
at net.sf.jasperreports.engine.fonts.FontUtil.checkAwtFont(FontUtil.java:635)
at net.sf.jasperreports.engine.fill.SimpleTextLineWrapper.loadFont(SimpleTextLineWrapper.java:369)
....

If I say "Courier New", for both fields, I get:

net.sf.jasperreports.engine.JRRuntimeException: Could not load the following font:
pdfFontName: Courier New
pdfEncoding: Cp1252
isPdfEmbedded : true
at net.sf.jasperreports.engine.export.JRPdfExporter.getFont(JRPdfExporter.java:2323)
at net.sf.jasperreports.engine.export.JRPdfExporter.getChunk(JRPdfExporter.java:2109)

Playing more I got this to work:

FontBuilder font = stl.font();
font.setFontName("Courier New");
font.setPdfFontName("C:/Windows/Fonts/cour.ttf");
font.setPdfEmbedded(true);

So "setFontName" wants a full path to the file?!? At least on Windows.
ddavidson
 
Posts: 7
Joined: Mon Apr 17, 2017 12:59 pm

Re: Non embedded font name example?

Postby andreas.hackl » Mon Apr 24, 2017 2:16 pm

I also had a lot of troubles with fonts. Especially in combination with pdf export.

What i know:
The first error is happening during generating the report.
This requires all used fonts to be available to the java virtual machine. Usually all fonts installed in windows should be available. I think you can enhance this list also with font extensions.
You can check the available fonts in your JVM with this code:
Code: Select all
String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
for (int i = 0; i < fonts.length; i++) {
  System.out.println(fonts[i]);
}

In your case it seems that Courier is unknown to your JVM.

The second error is happening when exporting the generated report to pdf.
In this case the pdf export library does not know the font.
I think the recommended way here is to use a font extension like i posted before.
andreas.hackl
 
Posts: 45
Joined: Tue Jan 28, 2014 4:47 pm


Return to Help

Who is online

Users browsing this forum: No registered users and 2 guests