2
votes

I've an application where I use jasper reports libraries for generating report from online. Whatever font I use other than Helvetica, to generate a pdf report, is a problem. I'm not able to detect from where the font is referred/loaded. I dont have any explicit classes for either Helvetica or Times-Roman.

Following is the error thrown:

net.sf.jasperreports.engine.JRRuntimeException: Could not load the following font : 
pdfFontName   : Times-Roman
pdfEncoding   : Identity-H
isPdfEmbedded : false
at net.sf.jasperreports.engine.export.JRPdfExporter.getFont(JRPdfExporter.java:2036) ~[jasperreports-5.5.0.jar:5.5.0]
at net.sf.jasperreports.engine.export.JRPdfExporter.getChunk(JRPdfExporter.java:1818) ~[jasperreports-5.5.0.jar:5.5.0]
at net.sf.jasperreports.engine.export.JRPdfExporter.getPhrase(JRPdfExporter.java:1787) ~[jasperreports-5.5.0.jar:5.5.0]
at net.sf.jasperreports.engine.export.SimplePdfTextRenderer.getPhrase(SimplePdfTextRenderer.java:89) ~[jasperreports-5.5.0.jar:5.5.0]
at net.sf.jasperreports.engine.export.SimplePdfTextRenderer.render(SimplePdfTextRenderer.java:99) ~[jasperreports-5.5.0.jar:5.5.0]
at net.sf.jasperreports.engine.export.JRPdfExporter.exportText(JRPdfExporter.java:2158) ~[jasperreports-5.5.0.jar:5.5.0]
at net.sf.jasperreports.engine.export.JRPdfExporter.exportElements(JRPdfExporter.java:861) ~[jasperreports-5.5.0.jar:5.5.0]
at net.sf.jasperreports.engine.export.JRPdfExporter.exportPage(JRPdfExporter.java:821) ~[jasperreports-5.5.0.jar:5.5.0]
at net.sf.jasperreports.engine.export.JRPdfExporter.exportReportToStream(JRPdfExporter.java:698) ~[jasperreports-5.5.0.jar:5.5.0]
at net.sf.jasperreports.engine.export.JRPdfExporter.exportReport(JRPdfExporter.java:420) ~[jasperreports-5.5.0.jar:5.5.0]

But for the same report if I give PDF font name as Helvetica, everything runs fine.

Can anybody please help me understand how this works ? Also, If I want to add some new font, where can I add it ?

Followed the steps to add a new font in jasper fonts jar , mentioned in below link

http://www.codesuggestions.com/java/jasper-reports/adding-custom-fonts-t...

Earlier error vanished. A new error popped up.

Caused by: java.lang.NoClassDefFoundError: Could not initialize class net.sf.jasperreports.engine.util.JRStyledTextParser
at net.sf.jasperreports.engine.fill.JRBaseFiller.<init>(JRBaseFiller.java:121) ~[jasperreports-5.5.0.jar:5.5.0]
at net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:88) ~[jasperreports-5.5.0.jar:5.5.0]
at net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:103) ~[jasperreports-5.5.0.jar:5.5.0]
at net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:61) ~[jasperreports-5.5.0.jar:5.5.0]
at net.sf.jasperreports.engine.fill.JRFiller.createFiller(JRFiller.java:179) ~[jasperreports-5.5.0.jar:5.5.0]
at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:81) ~[jasperreports-5.5.0.jar:5.5.0]
at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:446) ~[jasperreports-5.5.0.jar:5.5.0]
at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:276) ~[jasperreports-5.5.0.jar:5.5.0]
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:745) ~[jasperreports-5.5.0.jar:5.5.0]

jasperreports-5.5.0.jar is present in the classspath of the application, still facing this error.

1
To add fonts you need to use font-extensions see this stackoverflow.com/questions/33940126/…Petter Friberg
Thanks for the response Petter. I've tried out the mentioned method of exporting .ttf of the font as a jar and adding it to the classpath. But, it did not work, I still get the Caused by: java.lang.NoClassDefFoundError: Could not initialize class net.sf.jasperreports.engine.util.JRStyledTextParser error. Is there a specific name I should provide to the jar being created from iReports, or any name would do ?Suhasini
What font are you using?, make sure it works with the itext library stackoverflow.com/questions/35127956/…. The java.lang.NoClassDefFoundError, says you are missing a library, but I need more stacktrace since in current I can't see whichPetter Friberg
I'm using arial font. In iReports generation of a pdf report with arial is not a problem. The problem is when I add it to my application.Suhasini
So you are missing the font-extensions or another library in classpath, which library?, I need more stacktrace to understand edit question and ping mePetter Friberg

1 Answers

0
votes

The problem with me was that I had understood the exception wrong ! Hence following the wrong solutions.

The solution mentioned in the links that I followed was for addition of custom fonts for FontName attribute and not PDFFontName attribute.

Jasper's PDFExporter refers to fonts from iText.jar. Hence changing the getFont() method call in my class helped me.

font = FontFactory.getFont(
                pdfFont.getPdfFontName(),
                pdfFont.getPdfEncoding(),
                pdfFont.isPdfEmbedded(),
                jrFont.getFontSize() * fontSizeScale,
                pdfFontStyle,
                forecolor
                );

I changed this line and provided the absolute path for my font.ttf.

font = FontFactory.getFont(
                "D\\fonts\\Arial.ttf",
                BaseFont.IDENTITY_H,
                BaseFont.EMBEDDED,
                jrFont.getFontSize() * fontSizeScale,
                pdfFontStyle,
                forecolor
                );