I will introduce how to use Japanese fonts and external characters in PDF output with JasperReports.
Create a Maven project and add jasperreports to dependency.
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.10.0</version>
</dependency>
Place the font file (* .ttf) in the project's src / main / resources / fonts
. This will include the font files in the jar (or war) so you don't have to install the font files on the actual running server.
Create src / main / resources / fonts / fontsfamily-ipa.xml
and write the following definition. For the font file path, enter the path from the classpath.
<?xml version="1.0" encoding="UTF-8"?>
<fontFamilies>
<fontFamily name="IPAex Gothic">
<normal><![CDATA[fonts/ipaexg.ttf]]></normal>
<pdfEncoding><![CDATA[Identity-H]]></pdfEncoding>
<pdfEmbedded><![CDATA[true]]></pdfEmbedded>
</fontFamily>
<fontFamily name="IPAex Mincho">
<normal><![CDATA[fonts/ipaexm.ttf]]></normal>
<pdfEncoding><![CDATA[Identity-H]]></pdfEncoding>
<pdfEmbedded><![CDATA[true]]></pdfEmbedded>
</fontFamily>
</fontFamilies>
Identity-H
in<pdfEncoding>
indicates horizontal writing. Vertical writing is ʻIdentity-V`.true
to<pdf Embedded>
, fonts can be embedded in PDF.Create the JasperReports extension definition file src / main / resources / jasperreports_extension.properties` and describe the following definitions. The path and name of this file are fixed (JasperReports specification).
net.sf.jasperreports.extension.registry.factory.fonts=net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory
net.sf.jasperreports.extension.simple.font.families.ipafonts=fonts/fontsfamily-ipa.xml
fontFamily definition Specify the font name (fontFamily name) defined in xml in Text Field or Static Text.
Place the external character font file in the project's src / main / resources / fonts
in the same way as the IPA font. Gaiji font files are usually named ʻEUDC.tte, but rename them to
.ttf` so that JasperReports will recognize them.
Add fontFamily indicating external characters to the fontFamily definition xml defined earlier
<?xml version="1.0" encoding="UTF-8"?>
<fontFamilies>
...
<fontFamily name="_EUDC">
<normal><![CDATA[fonts/EUDC.ttf]]></normal>
<pdfEncoding><![CDATA[Identity-H]]></pdfEncoding>
<pdfEmbedded><![CDATA[true]]></pdfEmbedded>
</fontFamily>
</fontFamilies>
As it is, only the font for external characters is defined, but in reality, external characters appear in normal sentences. Therefore, it is necessary to define so that other fonts can be used at the same time.
Here, when ʻIPAex Gothic or ʻIPAex Mincho
is specified for the font, fontSet
is defined in the fontFamily definition xml so that external characters are also displayed, and the original fontFamily
name is used. change.
<?xml version="1.0" encoding="UTF-8"?>
<fontFamilies>
<fontFamily name="_IPAex Gothic"> <!--to name_Add-->
<normal><![CDATA[fonts/ipaexg.ttf]]></normal>
<pdfEncoding><![CDATA[Identity-H]]></pdfEncoding>
<pdfEmbedded><![CDATA[true]]></pdfEmbedded>
</fontFamily>
<fontFamily name="_IPAex Mincho">
<normal><![CDATA[fonts/ipaexm.ttf]]></normal>
<pdfEncoding><![CDATA[Identity-H]]></pdfEncoding>
<pdfEmbedded><![CDATA[true]]></pdfEmbedded>
</fontFamily>
<fontFamily name="_EUDC">
<normal><![CDATA[fonts/EUDC.ttf]]></normal>
<pdfEncoding><![CDATA[Identity-H]]></pdfEncoding>
<pdfEmbedded><![CDATA[true]]></pdfEmbedded>
</fontFamily>
<fontSet name="IPAex Gothic">
<family familyName="_IPAex Gothic" primary="true" />
<family familyName="_EUDC" />
</fontSet>
<fontSet name="IPAex Mincho">
<family familyName="_IPAex Mincho" primary="true" />
<family familyName="_EUDC" />
</fontSet>
</fontFamilies>
Characters that cannot be displayed in the IPA font will now use the Gaiji font.
Recommended Posts