Use of Japanese fonts and external characters in JasperReport

I will introduce how to use Japanese fonts and external characters in PDF output with JasperReports.

Prerequisites

Thing you want to do

Preparation

Create a Maven project and add jasperreports to dependency.

<dependency>
    <groupId>net.sf.jasperreports</groupId>
    <artifactId>jasperreports</artifactId>
    <version>6.10.0</version>
</dependency>

Use of Japanese fonts

Place the font to use in the classpath

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 fontFamily definition xml

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>

Set to read fontFamily definition xml

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

Specify the font in the layout definition file

fontFamily definition Specify the font name (fontFamily name) defined in xml in Text Field or Static Text.

Use of external characters

Place the external character font in the classpath

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.

Added external characters to fontFamily definition xml

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>

Make it possible to use with IPA font

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

Use of Japanese fonts and external characters in JasperReport
Use of Abstract Class and Interface properly in Java
Handling of date and time in Ruby. Use Date and Time properly.
[Java8] Proper use of Comparable and Comparator in terms of employee sorting
Proper use of redirect_to and render
Proper use of Mockito and PowerMock
Difference between byCharWrapping and byWordWrapping of UI Label in Japanese display
(Determine in 1 minute) About the proper use of empty ?, blank? And present?
Summary of hashes and symbols in Ruby
Japanese localization of CentOS 8 and Japanese keyboard layout
Proper use of interface and abstract class
[Ruby] Classification and usage of loops in Ruby
Japanese setting of mysql in Docker container
Discrimination of Enums in Java 7 and above
Japanese characters described in MessageResources.properties are garbled
Display around fonts in Japanese with Swift: macOS
[Java] Use of final in local variable declaration
Criteria for proper use of render and redirect_to
Basics of threads and Callable in Java [Beginner]
How to use enum (introduction of Japanese notation)
Introduction of Rspec and Japanese localization of error messages
How to use JQuery in js.erb of Rails6
Toward understanding of map and flatmap in Stream (1)
Why use setters/getters instead of public/private in Java
Use Shift Right and Shift Left properly in Selenium
[Rails] Ranking and pagination in order of likes