欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JasperSoft的乱码解决(idea)

程序员文章站 2022-07-14 10:37:03
...
  1. 编写一个测试的PDF
    JasperSoft的乱码解决(idea)

  2. 从c盘找到windows下的fonts,这里全是字体,选择一个你使用的,复制出来
    JasperSoft的乱码解决(idea)

  3. 想在idea中使用,你的具备这几样,jasper的坐标,以及三个文件

JasperSoft的乱码解决(idea)

4.这里做介绍,下面会附上文件的详细信息
1.fonts.xml 加载字体
2. simhei.ttf 字体
3.jasperreports_extension.prop 加载xml
JasperSoft的乱码解决(idea)

  1. 将这几个文件放置在resource目录下
    JasperSoft的乱码解决(idea)

  2. 可以根据以下的方式,添加数据,导出

JasperSoft的乱码解决(idea)
提供代码:
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盘找就可以

效果图:
JasperSoft的乱码解决(idea)

相关标签: jasper java idea