问题
I am facing the problem, that JasperReports still cannot find the Arial font.
I created a simple Maven Project with following structure and included it to my main application. So the main application contains the installed JAR in classpath:
- jasperreports_extension.properties
- fonts
|-> arial
|-> ariali.ttf
|-> arialbi.ttf
|-> arialbd.ttf
|-> arial.ttf
|-> fonts.xml
But I still see following Exception while exporting report to PDF.
net.sf.jasperreports.engine.JRRuntimeException: Could not load the following font :
pdfFontName : Arial
pdfEncoding : Identity-H
isPdfEmbedded : true
jasperreports_extension.properties
net.sf.jasperreports.extension.registry.factory.simple.font.families=net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory
net.sf.jasperreports.extension.simple.font.families.arial=fonts/fonts.xml
fonts.xml:
<?xml version="1.0" encoding="UTF-8"?>
<fontFamilies>
<fontFamily name="Arial">
<normal>fonts/arial/arial.ttf</normal>
<bold>fonts/arial/arialbd.ttf</bold>
<italic>fonts/arial/ariali.ttf</italic>
<boldItalic>fonts/arial/arialbi.ttf</boldItalic>
<pdfEncoding>Identity-H</pdfEncoding>
<pdfEmbedded>true</pdfEmbedded>
</fontFamily>
</fontFamilies>
Template
<font fontName="Arial" size="8" pdfFontName="Arial" pdfEncoding="Identity-H" isPdfEmbedded="true"/>
回答1:
The problem was the template itself: At one font-tag the <fontName>
property was missing:
Does not work:
<font size="12" isBold="true" pdfFontName="Arial"/>
Does work:
<font fontName="Arial" size="12" isBold="true" pdfFontName="Arial" pdfEncoding="Identity-H" isPdfEmbedded="true"/>
回答2:
Another Solution Worked For Me is:
JRProperties.setProperty("net.sf.jasperreports.default.pdf.font.name", "Helvetica"); JRProperties.setProperty("net.sf.jasperreports.default.pdf.encoding", "UTF-8"); JRProperties.setProperty("net.sf.jasperreports.default.pdf.embedded", "true");
If you set parameters from java side, you have to specify font.name and Helvetica saves the day. I tried Arial here but threw the same error.
回答3:
Copy the fonts files (*.ttf) into the folder:
/path/to/app/WEB-INF/classes/***HERE***
You app will get the fonts from here :)
回答4:
You must give the correct name of font for the PDF exporter. Your fonts are presents into the embedded fonts jar. You don't need to put them again into the classes directory ;-)
There is the correct fonts.xml content for pdf exporter (perhaps the names are case sensitive, be careful) :
<?xml version="1.0" encoding="UTF-8"?>
<fontFamilies>
<fontFamily name="Arial">
<normal>
<ttf>fonts/Arial/ARIAL.TTF</ttf>
<pdf>fonts/Arial/ARIAL.TTF</pdf>
</normal>
<bold>
<ttf>fonts/Arial/ARIALBD.TTF</ttf>
<pdf>fonts/Arial/ARIALBD.TTF</pdf>
</bold>
<italic>
<ttf>fonts/Arial/ARIALI.TTF</ttf>
<pdf>fonts/Arial/ARIALI.TTF</pdf>
</italic>
<boldItalic>
<ttf>fonts/Arial/ARIALBI.TTF</ttf>
<pdf>fonts/Arial/ARIALBI.TTF</pdf>
</boldItalic>
<pdfEncoding>Cp1252</pdfEncoding>
<pdfEmbedded>true</pdfEmbedded>
<exportFonts>
</exportFonts>
</fontFamily>
</fontFamilies>
I hope this help.
Regards, T.
回答5:
In my case, I was using:
<font fontName="Arial" pdfEncoding="Identity-H" isPdfEmbedded="true"/>
I had to remove pdffontName
, the correct font tag is:
<font fontName="Arial" pdfEncoding="Cp1256"/>
Note I am using a font jar that contains the following .xml file:
<?xml version="1.0" encoding="UTF-8"?>
<fontFamilies>
<fontFamily name="Arial">
<normal><![CDATA[fonts/arial.ttf]]></normal>
<pdfEncoding><![CDATA[Identity-H]]></pdfEncoding>
<pdfEmbedded><![CDATA[true]]></pdfEmbedded>
</fontFamily>
</fontFamilies>
来源:https://stackoverflow.com/questions/25548404/jasperreports-5-6-could-not-load-the-following-font