jarsperreport6.3解决pdf在linux无法打印汉字的问题。

jarsperreport6.3的官方文档中写道。

http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/util/JRFontNotFoundException.html

This is why we encourage our users to package up TTF files as font extensions, using built JasperReports font extension support, and thus make sure the report template is deployed together with the fonts it needs to render properly. Fonts used inside a report template should be considered as indispensable resources, just like image files and resource bundles. Just as a report would not display properly if some required logo image is missing, the same way the report would not display properly if some required font is missing as well. And just as the logo image is shipped with the report template, the same way font files should be shipped as well.

这段话的意识是说:推荐大家使用 把字体包放到工程项目里面,字体文件随着项目走,就不会受到JVM平台的影响。


可是使用ireport或者jarsperreport把extension导出来。然后部署到项目里面

大致的结构如下图所示:


fonts.xml内容

    <?xml version="1.0" encoding="UTF-8"?>
<fontFamilies>
<fontFamily name="CCSSimsun">
<normal>
<ttf><![CDATA[fonts/CCSSimsun/simsun.ttf]]></ttf>
<pdf><![CDATA[fonts/CCSSimsun/simsun.ttf]]></pdf>
</normal>
<bold>
<ttf><![CDATA[fonts/CCSSimsun/simsunbd.ttf]]></ttf>
<pdf><![CDATA[fonts/CCSSimsun/simsunbd.ttf]]></pdf>
</bold>
<pdfFontName><![CDATA[CCSSimsun]]></pdfFontName>
<pdfEncoding><![CDATA[Identity-H]]></pdfEncoding>
<pdfEmbedded><![CDATA[true]]></pdfEmbedded>
<exportFonts/>
<!--
<locales>
<locale><![CDATA[zh_CN]]></locale>
</locales>
--> 
</fontFamily>
</fontFamilies>


fonts.xml 中的locale节点一定要注释掉,如果不注释掉,jarsperreport将不会用你放到项目的字体文件,而是直接从JVM平台查找字体文件。

这个地方是个坑,因为导出来的xml本来就带有locale节点。

jasperreports_extension.properties的内容为:

net.sf.jasperreports.extension.registry.factory.fonts=net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory
net.sf.jasperreports.extension.simple.font.families.CCSSimsun=fonts/fonts.xml

jasperreports.properties的内容为:

net.sf.jasperreports.export.pdf.fontdir.jasper=fonts/CCSSimsun


打印PDF的方法为:

public void printPdf() throws Exception {

ServletContext context = request.getSession().getServletContext();
response.setContentType("application/pdf");
response.setCharacterEncoding("UTF-8");


try
{
String fontFilePath =context.getRealPath("/WEB-INF/classes")+"/";
String subReportPath=context.getRealPath("/WEB-INF/classes/print")+"/";


//EnvironmentFactory.getEnvironment().

File reportFile = new File(context.getRealPath("/WEB-INF/classes/print/"+jasperFileName+".jasper"));
logger.debug("reportFile path......"+reportFile.getAbsolutePath());
if (!reportFile.exists())
throw new Exception("未找到文件名为:"+jasperFileName+"的打印模板");

//JasperDesign design = JRXmlLoader.load(reportFile);
//JasperReport jasperReport = JasperCompileManager.compileReport(design);

JasperReportsContext jrcontext=new SimpleJasperReportsContext();
JasperReport jasperReport = (JasperReport)JRLoader.loadObjectFromFile(reportFile.getPath());
jasperReport.setJasperReportsContext(jrcontext);
final Font awtFont=FontUtil.getInstance(jrcontext).getAwtFontFromBundles("CCSSimsun", Font.PLAIN, 20.0f, null, false);

//解决barcode下面的数字打印不出来的问题,保证barbecue字体和jarsperrepor字体一致
Environment env=new Environment() {

public final Font DEFAULT_FONT = awtFont;

//linux下没有安装图形化界面,但是本机是正常的,所以此处执行时会抛出异常,获取不到每英寸的像素点的个数。
public int getResolution() {
//Toolkit.getDefaultToolkit().getScreenResolution();
return 360;
}

   public Font getDefaultFont() {
       return DEFAULT_FONT;
   }
};
EnvironmentFactory.setDefaultEnvironment(env);

Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("SUBREPORT_DIR", subReportPath);

JRBeanCollectionDataSource beanSource=new JRBeanCollectionDataSource(printBean);

JasperPrint jasperPrint = 
JasperFillManager.fillReport(
jasperReport, 
parameters,
beanSource
);

//JRStyle style = new JRBaseStyle();  
//style.setPdfEmbedded(true);
//style.setPdfFontName("simsun");
//style.setPdfEncoding("Identity-H");
//jasperPrint.setDefaultStyle(style);

JRPdfExporter exporter = new JRPdfExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(response.getOutputStream()));
exporter.exportReport();

logger.debug("report pdf generate over....");
}catch(Exception e){
logger.error("print report pdf error:",e);
throw e;
}
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值