问题描述
我有一个Jasper Report模板,其中包含英语文本以及称为Pashto的语言(Pashto是一种比阿拉伯语字符更多的语言,并且具有阿拉伯语的所有字符)。
我的JRXML具有以下内容。
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.13.0.final using JasperReports Library version 6.13.0-46ada4d1be8f3c5985fd0b6146f3ed44caed6f05 -->
<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="birth registration" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" resourceBundle="localizationdemo" uuid="c07e9faf-a34d-48bd-bbab-962a662c7110">
<property name="ireport.zoom" value="1.5"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="205"/>
<style name="table">
<box>
<pen lineWidth="1.0" lineColor="#000000"/>
</box>
</style>
<style name="table_TH" mode="Opaque" backcolor="#F0F8FF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="table_CH" mode="Opaque" backcolor="#BFE1FF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="table_TD" mode="Opaque" backcolor="#FFFFFF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<subDataset name="Table Dataset 1" uuid="f0720649-b198-4500-815e-f6f9e5fbafef"/>
<queryString language="SQL">
<![CDATA[select * from birth where id in (select id from birth limit 1)]]>
</queryString>
<title>
<band height="773" splitType="Stretch">
<rectangle>
<reportElement x="21" y="41" width="516" height="23" uuid="3a6b356d-f32d-41fd-a28d-3895189070d4"/>
</rectangle>
<line>
<reportElement x="0" y="4" width="555" height="1" uuid="906b4539-daeb-4afe-94cf-f52db3059c25"/>
</line>
<line>
<reportElement x="555" y="4" width="1" height="757" uuid="272adf95-1af4-49b8-8cc0-8c50a522bfe1"/>
</line>
<line>
<reportElement x="0" y="4" width="1" height="757" uuid="aab9580d-1e08-4081-b974-e8fefc49b000"/>
</line>
<line>
<reportElement x="0" y="760" width="555" height="1" uuid="0f2d922f-93af-4f28-b8a3-f07366aa2ddd"/>
</line>
<staticText>
<reportElement x="22" y="40" width="100" height="30" uuid="4d9b3efd-1d2a-48a7-947f-afabbb4f5afb"/>
<text><![CDATA[Name]]></text>
</staticText>
<line>
<reportElement x="0" y="760" width="555" height="1" uuid="0f2d922f-93af-4f28-b8a3-f07366aa2ddd"/>
</line>
<staticText>
<reportElement x="124" y="40" width="100" height="30" uuid="4d9b3efd-1d2a-48a7-947f-afabbb4f5afb"/>
<text><![CDATA[Father's Name]]></text>
</staticText>
<line>
<reportElement x="0" y="760" width="555" height="1" uuid="0f2d922f-93af-4f28-b8a3-f07366aa2ddd"/>
</line>
<staticText>
<reportElement x="225" y="40" width="100" height="30" uuid="4d9b3efd-1d2a-48a7-947f-afabbb4f5afb"/>
<text><![CDATA[پښتو لیک]]></text>
</staticText>
</band>
</title>
</jasperReport>
我的Spring Boot Controller是
@GetMapping(value = "/download/{reportFileName}/{locale}")
public void downloadReport(final HttpServletResponse response,final HttpServletRequest request,@PathVariable("reportFileName") String reportFileName,@PathVariable("locale") String locale) throws IOException,JRException,SQLException,Exception
{
try{
final HashMap<String,Object> parameters = new HashMap<>();
parameters.put(JRParameter.REPORT_LOCALE,new Locale(locale));
String returnedPath = this.service.generatePdfJasperReportFromJRXMLFile(parameters,"/certificates/birth_registration/" + reportFileName,"pdf",locale);
this.service.downloadReport(returnedPath,response);
}
catch(Exception e){
System.out.println("Some error has occurred while preparing the pdf AnarReport.---------------" + e.getMessage());
e.printStackTrace();
}
}
我的服务文件具有这些功能
public String generatePdfJasperReportFromJRXMLFile(HashMap<String,Object> parameters,String jrxmlFile,String reportType,String locale) throws IOException,SQLException {
/**
This function generates a pdf report from a .jrxml file stored in the classpath
A Usage Example can be:
final Map<String,Object> parameters = new HashMap<>();
parameters.put("name","province name");
String returnedPath = service.generatePdfJasperReportFromJRXMLFile(new HashMap<>(parameters),"/test-report.jrxml")
@return - returns the path of the destination file.
*/
// Fetching the .jrxml file from the resources folder.
final InputStream stream = this.getClass().getResourceAsStream(jrxmlFile);
String pdfFilePath = this.printJasperReport(stream,parameters,reportType);
return pdfFilePath;
}
private String printJasperReport(InputStream stream,Map<String,String reportType) throws IOException,SQLException{
/**
This function generates a pdf file from the given stream with the given parameters and saves it into a
a temporary file.
@return - returns the path of the temp file.
*/
System.out.println("parameters+++++++++++++++++++++++++++++" + parameters);
// Compile the Jasper report from .jrxml to .japser
final JasperReport report = JasperCompileManager.compileReport(stream);
// The URL would look something like "jdbc:postgresql://localhost:5432/ebreshna_test?user=some_user&password=some_password"
String url = String.format("%s?user=%s&password=%s",this.datasourceUrl,this.datasourceUsername,this.datasourcePassword);
// The Generated PDF file will be stored in a temp directory
String destFile = "";
try{
Connection conn = DriverManager.getConnection(url);
final JasperPrint print = JasperFillManager.fillReport(report,conn);
// Export the report to a PDF file.
if(reportType.equals("pdf")){
destFile = File.createTempFile("temp-jasper-report",".pdf").getPath();
JasperExportManager.exportReportToPdfFile(print,destFile);
}
else if(reportType.equals("excel")){
destFile = File.createTempFile("temp-jasper-report",".xlsx").getPath();
// JRXlsExporter exporter = new JRXlsExporter();
JRExporter exporter = new JRXlsExporter();
exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,Boolean.TRUE);
exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,Boolean.FALSE);
exporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE,Boolean.TRUE);
//we set the one page per sheet parameter here
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,Boolean.TRUE);
exporter.setParameter(JRExporterParameter.JASPER_PRINT,print);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,destFile);
// exporter.setParameter(JRExporterParameter.INPUT_STREAM,print);
// exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,destFile);
exporter.exportReport();
}
else if(reportType.equals("csv")){
destFile = File.createTempFile("temp-jasper-report",".csv").getPath();
JRCsvExporter exporter = new JRCsvExporter();
exporter = new JRCsvExporter();
exporter.setExporterInput(new SimpleExporterInput(print));
exporter.setExporterOutput(new SimpleWriterExporterOutput(new File(destFile)));
SimpleCsvExporterConfiguration configuration = new SimpleCsvExporterConfiguration();
configuration.setWriteBOM(Boolean.TRUE);
configuration.setRecordDelimiter("\r\n");
exporter.setConfiguration(configuration);
exporter.exportReport();
}
}catch(Exception e){
System.out.println("Some error has occurred while preparing the pdf AnarReport." + e.getMessage());
e.printStackTrace();
}
return destFile;
}
public void downloadReport(String filePath,HttpServletResponse response) throws Exception{
File file = new File(filePath);
if (file.exists()) {
fileDownloadUtil.fileDownload(file,response);
}
}
现在的问题是,在下载pdf文件后,不会显示普什图语的文本。
在此我将不胜感激。
编辑
经过多次尝试和失败后,我使用了Arial字体,该字体可以工作,但是输出仍然有问题。
这是spring boot生成报告的当前结果。
正确的结果应该是jasper studio中当前显示的结果。
解决方法
首先根据教程创建并将字体扩展(jar)文件添加到项目类路径中。然后按如下所示设置字体名称和其他属性,
按照以下示例,首先为Saraiki字体(Saraiki字体支持普什图语)创建Saraiki.jar(字体扩展名)文件。然后将其添加到项目类路径。如果要使用此字体,则应使用字体扩展文件的定义字体名称。但是,当设置任何其他字体名称(未定义的字体名称)时,该字体不支持。
<staticText>
<reportElement x="225" y="40" width="100" height="30" uuid="4d9b3efd-1d2a-48a7-947f-afabbb4f5afb"/>
<textElement>
<font fontName="Saraiki" size="10" isBold="false"/>
</textElement>
<text><![CDATA[پښتو لیک]]></text>
</staticText>