解决JasperReport生成PDF文件,中文不能显示的问题

1、在studio中设置font,然后就可以在jrxml的元素中使用,如下
<textElement textAlignment= "Center" verticalAlignment= "Middle" >
      <font fontName= "hwst" />
</textElement>
<textFieldExpression> <![CDATA[ $P{testParam} ]]> </textFieldExpression>
设置font的方法请移步JasperReport的官网上,传送门:http://community.jaspersoft.com/wiki/custom-font-font-extension
完整的jrxml文件请见附件:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.3.1.final using JasperReports Library version 6.3.1  -->
<!-- 2017-09-20T10:37:45 -->
<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="MvcIReportExample" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="9e89b7df-9b5d-4d02-80af-2b1fded595c9">
	<property name="ireport.zoom" value="1.1"/>
	<property name="ireport.x" value="0"/>
	<property name="ireport.y" value="0"/>
	<property name="com.jaspersoft.studio.data.sql.tables" value=""/>
	<parameter name="testParam" class="java.lang.String">
		<parameterDescription><![CDATA[测试用的参数]]></parameterDescription>
		<defaultValueExpression><![CDATA[123]]></defaultValueExpression>
	</parameter>
	<queryString>
		<![CDATA[select * from cms_new.ba_user]]>
	</queryString>
	<field name="name" class="java.lang.String"/>
	<field name="email" class="java.lang.String"/>
	<field name="password" class="java.lang.String"/>
	<field name="username" class="java.lang.String"/>
	<field name="id" class="java.lang.Long"/>
	<sortField name="name"/>
	<background>
		<band splitType="Stretch"/>
	</background>
	<title>
		<band height="79" splitType="Stretch">
			<staticText>
				<reportElement x="0" y="0" width="555" height="40" uuid="77313ff5-b9b9-446e-b415-b35885e73361"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="14" isBold="true"/>
				</textElement>
				<text><![CDATA[JasperReport Test For Table]]></text>
			</staticText>
			<staticText>
				<reportElement key="" positionType="Float" mode="Opaque" x="195" y="49" width="100" height="30" backcolor="#911614" uuid="bbb5d3c9-a695-4fd6-832c-294da91edb9c"/>
				<textElement textAlignment="Center" verticalAlignment="Middle"/>
				<text><![CDATA[有人要找医生了]]></text>
			</staticText>
			<textField>
				<reportElement x="50" y="10" width="111" height="20" uuid="ff1f5807-7ca0-40d0-8dc1-24aa9bfcfcb0"/>
				<box>
					<topPen lineWidth="0.5"/>
					<leftPen lineWidth="0.5"/>
					<bottomPen lineWidth="0.5"/>
					<rightPen lineWidth="0.5"/>
				</box>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font fontName="hwst"/>
				</textElement>
				<textFieldExpression><![CDATA[$P{testParam}]]></textFieldExpression>
			</textField>
		</band>
	</title>
	<columnHeader>
		<band height="40" splitType="Stretch">
			<staticText>
				<reportElement mode="Opaque" x="160" y="0" width="111" height="40" backcolor="#CCFFCC" uuid="ddc1db2b-4a98-4ab3-8160-0c40f192b052"/>
				<box>
					<topPen lineWidth="0.5"/>
					<leftPen lineWidth="0.5"/>
					<bottomPen lineWidth="0.5"/>
					<rightPen lineWidth="0.5"/>
				</box>
				<textElement textAlignment="Center" verticalAlignment="Middle"/>
				<text><![CDATA[id]]></text>
			</staticText>
			<staticText>
				<reportElement mode="Opaque" x="269" y="0" width="111" height="40" backcolor="#CCFFCC" uuid="5927a66b-71f9-4cd1-ae1d-4c340d6b975d"/>
				<box>
					<topPen lineWidth="0.5"/>
					<leftPen lineWidth="0.5"/>
					<bottomPen lineWidth="0.5"/>
					<rightPen lineWidth="0.5"/>
				</box>
				<textElement textAlignment="Center" verticalAlignment="Middle"/>
				<text><![CDATA[username]]></text>
			</staticText>
			<staticText>
				<reportElement mode="Opaque" x="50" y="0" width="111" height="40" backcolor="#CCFFCC" uuid="0b3a54b3-b8ea-44c8-879b-ee92ab60ee41"/>
				<box>
					<topPen lineWidth="0.5"/>
					<leftPen lineWidth="0.5"/>
					<bottomPen lineWidth="0.5"/>
					<rightPen lineWidth="0.5"/>
				</box>
				<textElement textAlignment="Center" verticalAlignment="Middle"/>
				<text><![CDATA[name]]></text>
			</staticText>
			<staticText>
				<reportElement x="380" y="0" width="100" height="40" uuid="15ec3d3d-bfb1-4822-958f-1d71e8d5e9fa"/>
				<box padding="1">
					<topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
					<leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
					<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
					<rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
				</box>
				<textElement textAlignment="Center" verticalAlignment="Middle"/>
				<text><![CDATA[password]]></text>
			</staticText>
			<staticText>
				<reportElement x="480" y="0" width="75" height="40" uuid="d674e108-884b-4680-a641-529f359bf169"/>
				<box>
					<topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
					<leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
					<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
					<rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
				</box>
				<textElement textAlignment="Center" verticalAlignment="Middle"/>
				<text><![CDATA[email]]></text>
			</staticText>
		</band>
	</columnHeader>
	<detail>
		<band height="39" splitType="Stretch">
			<textField>
				<reportElement x="50" y="0" width="111" height="38" uuid="ff1f5807-7ca0-40d0-8dc1-24aa9bfcfcb0"/>
				<box>
					<topPen lineWidth="0.5"/>
					<leftPen lineWidth="0.5"/>
					<bottomPen lineWidth="0.5"/>
					<rightPen lineWidth="0.5"/>
				</box>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font fontName="hwst"/>
				</textElement>
				<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement x="161" y="0" width="108" height="38" uuid="461fc451-d6f0-4b05-8c5f-b1533a3a0fcd"/>
				<box>
					<topPen lineWidth="0.5"/>
					<leftPen lineWidth="0.5"/>
					<bottomPen lineWidth="0.5"/>
					<rightPen lineWidth="0.5"/>
				</box>
				<textElement textAlignment="Center" verticalAlignment="Middle"/>
				<textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement x="269" y="0" width="111" height="38" uuid="79833268-1830-47b8-b276-baaccd5c4715"/>
				<box>
					<topPen lineWidth="0.5"/>
					<leftPen lineWidth="0.5"/>
					<bottomPen lineWidth="0.5"/>
					<rightPen lineWidth="0.5"/>
				</box>
				<textElement textAlignment="Center" verticalAlignment="Middle"/>
				<textFieldExpression><![CDATA[$F{username}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement x="380" y="0" width="100" height="38" uuid="b2d4728e-de69-4b3a-af93-2a140c538c11"/>
				<box padding="1">
					<topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
					<leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
					<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
					<rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
				</box>
				<textElement textAlignment="Center" verticalAlignment="Middle"/>
				<textFieldExpression><![CDATA[$F{password}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement x="480" y="0" width="75" height="38" uuid="eb0a96c4-33ca-4b38-afa2-3468bc0aa3b2"/>
				<box topPadding="1" leftPadding="1" bottomPadding="1" rightPadding="1">
					<topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
					<leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
					<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
					<rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
				</box>
				<textElement textAlignment="Center" verticalAlignment="Middle"/>
				<textFieldExpression><![CDATA[$F{email}]]></textFieldExpression>
			</textField>
		</band>
	</detail>
</jasperReport>

2、将studio软件的fonts 下的某些常用的字体打成一个jar 包( JasperReportSoft Studio提供字体设置及导出功能),
然后把这个jar 包导入项目即可。  
注意,经测试导出的jar包在grails 2.2.4 项目中,不可以直接使用,需要将jar包中的fontsfamily**.xml做些修改,其中pdfEncoding 属性要设为 ” Identity-H ” 或者 ” Identity-V ” 。
以下的xml是我导出的只有一个字体——华文宋体STSong-Light的软件包,理论上可以配置多个,只测试了包含一个字体的情况
jar包中的配置xml:
<?xml version="1.0" encoding="UTF-8"?>
<fontFamilies>
	<fontFamily name="hwst">
		<normal>fonts/hwst/stsong.ttf</normal>
		<pdfEncoding>Identity-H</pdfEncoding>
		<pdfEmbedded>true</pdfEmbedded>
		<exportFonts>
		</exportFonts>
	</fontFamily>
</fontFamilies>

3、设置完成我们就得到一个字体jar与一个使用了该字体的jrxml文件,可以使用jasperreport的api愉快地生成pdf了

4、另外推荐一篇文档



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值