JasperSoft的乱码解决(idea)
程序员文章站
2022-07-14 10:37:03
...
-
编写一个测试的PDF
-
从c盘找到windows下的fonts,这里全是字体,选择一个你使用的,复制出来
-
想在idea中使用,你的具备这几样,jasper的坐标,以及三个文件
4.这里做介绍,下面会附上文件的详细信息
1.fonts.xml 加载字体
2. simhei.ttf 字体
3.jasperreports_extension.prop 加载xml
-
将这几个文件放置在resource目录下
-
可以根据以下的方式,添加数据,导出
提供代码:
Fonts.xml
<?xml version="1.0" encoding="UTF-8"?>
<fontFamilies>
<!--<fontFamily name="华文宋体">
<normal>stsong/stsong.TTF</normal>
<bold>stsong/stsong.TTF</bold>
<italic>stsong/stsong.TTF</italic>
<boldItalic>stsong/stsong.TTF</boldItalic>
<pdfEncoding>Identity-H</pdfEncoding>
<pdfEmbedded>true</pdfEmbedded>
<exportFonts>
<export key="net.sf.jasperreports.html">'华文宋体', Arial, Helvetica, sans-serif</export>
<export key="net.sf.jasperreports.xhtml">'华文宋体', Arial, Helvetica, sans-serif</export>
</exportFonts>
</fontFamily>-->
<fontFamily name="黑体">
<normal>stsong/simhei.ttf</normal>
<bold>stsong/simhei.ttf</bold>
<italic>stsong/simhei.ttf</italic>
<boldItalic>stsong/simhei.ttf</boldItalic>
<pdfEncoding>Identity-H</pdfEncoding>
<pdfEmbedded>true</pdfEmbedded>
<exportFonts>
<export key="net.sf.jasperreports.html">'黑体', Arial, Helvetica, sans-serif</export>
<export key="net.sf.jasperreports.xhtml">'黑体', Arial, Helvetica, sans-serif</export>
</exportFonts>
</fontFamily>
</fontFamilies>
jasperreports_extension.properties
net.sf.jasperreports.extension.registry.factory.simple.font.families=net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory
net.sf.jasperreports.extension.simple.font.families.lobstertwo=stsong/fonts.xml
controller
@Controller
@RequestMapping("/cargo/export")
public class JasperController extends BeasController {
@RequestMapping("/exportPdf")
public void exportPdf(String id) throws Exception {
// 读取webapp下的文件
InputStream inputStream = session.getServletContext().getResourceAsStream("/jasper/test01.jasper");
Map map = new HashMap<>();
map.put("username", "张三");
// 封装
JasperPrint jasperPrint = JasperFillManager.fillReport(inputStream, map, new JREmptyDataSource());
// 导出
JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());
}
}
ps:字体的文件自己去c盘找就可以
效果图: