简体   繁体   English

如何在 Jasper 报告正确导出的 PDF 中显示越南语?

[英]How to display Vietnamese in PDF what exported by Jasper report correctly?

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. Jasper 报告为 Eclipse 2019.6。 Vietnamese character display in error越南字符显示错误

Java code Java代码

@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文件 *.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我在类路径中创建文件jasperreports.properties 在此处输入图像描述

In Eclipse IDE (with Jaspersoft module) Eclipse IDE(带Jaspersoft模块) 在此处输入图像描述

How to fix it?如何解决?

Try this: 尝试这个:

  1. Do not set a PDF Font Name for the font family that you created with the Arial ttf. 不要为使用Arial ttf创建的字体系列设置PDF字体名称。 You don't want to use Helvetica because it doesn't include glyphs for Vietnamese characters. 您不希望使用Helvetica,因为它不包含越南字符的字形。 Leave it empty so that the Arial ttf is used in PDF. 将其保留为空,以便在PDF中使用Arial ttf。
  2. Set the PDF Encoding for the font family to Identity-H (Vietnamese uses horizontal writing). 将字体系列的PDF编码设置为Identity-H(越南语使用水平书写)。
  3. The screenshot shows that the font family is called "times", so you'll have to use <font fontName="times"/> for the text element. 屏幕快照显示字体系列称为“时间”,因此您必须对文本元素使用<font fontName="times"/>

This will result in the Arial ttf being embedded in the PDF file (a subset of it actually) and used for the text. 这将导致Arial ttf嵌入PDF文件(实际上是其子集)并用于文本。 Therefore the PDF would show the Vietnamese characters, as long as the Arial ttf includes glyphs for them (and my Arial ttf copy does). 因此,只要Arial ttf包含越南文字,PDF就会显示越南文字(而我的Arial ttf副本也包含)。

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"/> 如果由于某种原因您不想将Arial嵌入PDF文件中,也可以使用net.sf.jasperreports:jasperreports-fonts:6.9.0中包含的DejaVu字体,例如<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. 如果您通常希望将字体嵌入PDF中,或者您可以不设置它并为每个字体系列指定PDF编码,则可能还需要将net.sf.jasperreports.default.pdf.encoding属性设置为Identity-H

It's a font issue. 这是字体问题。 Not a PDF encoding problem. 不是PDF编码问题。 Maybe try to use Arial Unicode MS 也许尝试使用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. 还要确保在类路径中使用Arial字体扩展名jar。 Search internet for arial font extension jar. 在Internet上搜索arial字体扩展罐。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM