1

I am using these dependencies

implementation ('net.sf.jasperreports:jasperreports:6.9.0')
implementation ('net.sf.jasperreports:jasperreports-fonts:6.9.0')
implementation ('net.sf.jasperreports:jasperreports-functions:6.9.0')
implementation ('net.sf.jasperreports:jasperreports-metadata:6.9.0')
implementation ('net.sf.jasperreports:jasperreports-fonts:6.9.0')
implementation ('net.sf.jasperreports:jasperreports-annotation-processors:6.9.0')
implementation ('net.sf.jasperreports:jasperreports-chart-themes:6.9.0')
implementation ('net.sf.jasperreports:jasperreports-chart-customizers:6.9.0')
implementation ('net.sf.jasperreports:jasperreports-custom-visualization:6.9.0')
compile 'com.lowagie:itext:2.1.7.js5'

with Jasper report for Eclipse 2019.6 . Vietnamese character display in error

Java code

@RequestMapping(value = "/ca_audit/minute_pdf", method = RequestMethod.GET)
public void generatePDFCashAuditMinute() throws JRException {
    JasperReport jasperReport = JasperCompileManager.compileReport("D:/vy/template.jrxml");
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("p1", "Example joint stock company"); // Tên tổ chức
    parameters.put("p2", "EXKK002"); // Số biên bản
    parameters.put("p3", 26); // Ngày
    JRDataSource jrDataSource = new JREmptyDataSource();
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, jrDataSource);
    JasperExportManager.exportReportToPdfFile(jasperPrint, "D:/foo/out.pdf");
}

The content of file *.jrxml

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="bien_ban_kiem_ke_quy_a4" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="7098c966-5c92-43e3-a729-b3d486fc858b">
    <parameter name="p1" class="java.lang.String"/>
    <parameter name="p2" class="java.lang.String"/>
    <parameter name="p3" class="java.lang.Integer"/>
    <queryString>
        <![CDATA[select 1 as id]]>
    </queryString>
    <field name="id" class="java.lang.Integer"/>
    <title>
        <band height="84" splitType="Stretch">
            <textField>
                <reportElement x="14" y="6" width="100" height="30" uuid="2106c35d-3d5e-4203-950a-54a87b6952ea"/>
                <textElement>
                    <font fontName="Arial"/>
                </textElement>
                <textFieldExpression><![CDATA[$P{p1}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement x="376" y="23" width="64" height="16" uuid="77d41a67-82d2-46d0-9a3e-5327ae3b450b"/>
                <text><![CDATA[Số biên bản:]]></text>
            </staticText>
            <textField>
                <reportElement x="440" y="23" width="50" height="16" uuid="a505da46-48af-4dcb-84bc-f9d27697919d"/>
                <textFieldExpression><![CDATA[$P{p2}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement x="260" y="47" width="34" height="13" uuid="541c25ed-4b29-4cc4-a19d-05f295ae0340"/>
                <text><![CDATA[ngày]]></text>
            </staticText>
            <textField>
                <reportElement x="290" y="47" width="40" height="13" uuid="72e64426-d702-4694-bc25-81a2f3e43f82"/>
                <textFieldExpression><![CDATA[$P{p3}]]></textFieldExpression>
            </textField>
        </band>
    </title>
</jasperReport>

I create file jasperreports.properties in classpath enter image description here

In Eclipse IDE (with Jaspersoft module) enter image description here

How to fix it?

Vy Do
  • 46,709
  • 59
  • 215
  • 313
  • @Alex K, don't mark it as duplicated. After 2 hours read these answers, I still cannot run success. – Vy Do Jul 27 '19 at 01:01

3 Answers3

2

Try this:

  1. Do not set a PDF Font Name for the font family that you created with the Arial ttf. You don't want to use Helvetica because it doesn't include glyphs for Vietnamese characters. Leave it empty so that the Arial ttf is used in PDF.
  2. Set the PDF Encoding for the font family to Identity-H (Vietnamese uses horizontal writing).
  3. The screenshot shows that the font family is called "times", so you'll have to use <font fontName="times"/> for the text element.

This will result in the Arial ttf being embedded in the PDF file (a subset of it actually) and used for the text. Therefore the PDF would show the Vietnamese characters, as long as the Arial ttf includes glyphs for them (and my Arial ttf copy does).

If for some reason you don't want to embed Arial in the PDF file, you can also use the DejaVu fonts included in net.sf.jasperreports:jasperreports-fonts:6.9.0, for instance <font fontName="DejaVu Sans"/>

You might also want to set the net.sf.jasperreports.default.pdf.encoding property to Identity-H, if you generally want to embed fonts in PDFs, or just leave it unset and specify the PDF encoding for each font family.

dada67
  • 4,723
  • 17
  • 19
2

It's a font issue. Not a PDF encoding problem. Maybe try to use Arial Unicode MS

Inside your tag add the following

<font fontName="Arial Unicode MS" size="11" pdfFontName="Arial Unicode MS" pdfEncoding="Identity-H"/> 

Also make sure that Arial font extension jar in class path. Search internet for arial font extension jar.

pbloz
  • 73
  • 5
0

File jasperreports.properties

net.sf.jasperreports.default.pdf.encoding=UTF-8
net.sf.jasperreports.export.character.encoding=UTF-8

enter image description here

https://community.jaspersoft.com/wiki/custom-font-font-extension

https://freefontsdownload.net/download/36926/arial-unicode-ms.zip

https://learn.microsoft.com/vi-vn/typography/font-list/arial-unicode-ms

What is the purpose of the 'Pdf embedded' option of the Static Text element in iReport 5.6.0?

Vy Do
  • 46,709
  • 59
  • 215
  • 313