SpringBoot集成ireport打印,并解决PDF中文显示问题

1、相关jar包引入

XML 复制代码
<!--==========  ireport报表相关  start=============-->
        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports</artifactId>
            <version>4.5.1</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-collections</groupId>
                    <artifactId>commons-collections</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>commons-beanutils</groupId>
                    <artifactId>commons-beanutils</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.poi</groupId>
                    <artifactId>poi-ooxml</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--itex pdf相关-->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.6</version>
        </dependency>
        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports-fonts</artifactId>
            <version>4.0.0</version>
        </dependency>
        <!-- 解决中文字体显示问题 -->
        <dependency>
            <groupId>cn.lesper</groupId>
            <artifactId>iTextAsian</artifactId>
            <version>3.0</version>
        </dependency>
        <!--pdf itext 的jar依赖  -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-pdfa</artifactId>
            <version>5.5.11</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>2.4.13</version>
        </dependency>
        <!--==========  ireport报表相关  end=============-->

2、工具类

java 复制代码
/**
     * @param response              httpServletResponse
     * @param fileName              显示/保存 的文件名,不需要(.pdf)后缀
     * @param data                  Fields 数据集合
     * @param params                其它 params参数
     * @param downLoad              是否下载  true:下载  false:在线预览
     */
    public static void printPDF(HttpServletResponse response, String fileName, InputStream templateStream, List<?> data, Map<String,Object> params, boolean downLoad) throws IOException, JRException {
        // 1. 获取数据源
        JRDataSource jrDataSource  = new JRBeanCollectionDataSource(data);
        // 2. 获取模板文件
        // File jasperFile = ResourceUtils.getFile("classpath:" + jrxmlTemplate);
        // 3. 编译 jrxml
        JasperReport jasperReport = JasperCompileManager.compileReport(templateStream);
        // 4. 获取报表
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, jrDataSource);
        // 通过outputStream输出
        JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());

        // 5. 设置响应格式
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/pdf");
        if(downLoad){
            response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".pdf");
        }else{
            response.setHeader("content-disposition", "inline;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".pdf");
        }
    }

3、调用Demo

java 复制代码
    @RequestMapping(value = "/printTest", method = RequestMethod.GET)
    public Result printTest(HttpServletResponse response) throws Exception {
      
        List<Map<String,Object>> data = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        map.put("name","张三");
        map.put("phone","88888888");
        data.add(map);
        String objectName = "test.jrxml";
        OSS ossClient = aliyunOssClient.createOssClient();
        OSSObject ossObject = ossClient.getObject(bucketName, objectName);
        InputStream templateStream = ossObject.getObjectContent();
        iReportHelper.printPDF(response,"打印预览", templateStream,data,map,false);
        return Result.setSucMsg("success");
    }

4、模板注意点

a) 模板数据源:list对应Fields,map对应Parameters

b) 模板字体尽量选用默认字体,jasper内置自带,其他字体容易报错jvm找不到字体,需要服务器安装。

c) 打印PDF选中以下属性,中文字体方可正常显示。且注意引入jasperreports-fonts 包。

相关推荐
源码站~1 天前
基于SpringBoot+Vue的健身房管理系统
vue.js·spring boot·后端·毕业设计·前后端分离·管理系统·健身房
程序员爱钓鱼1 天前
Python编程实战 - 面向对象与进阶语法 - 异常类型与捕获
后端·python·ipython
程序员爱钓鱼1 天前
Python编程实战 - 面向对象与进阶语法 - 类方法与静态方法
后端·python
鬼火儿1 天前
MySQL系列之数据类型(String)
java·后端
程序新视界1 天前
MySQL的隔离级别及其工作原理详解
数据库·后端·mysql
IT_陈寒1 天前
Redis 7.0的这个新特性让我处理百万级QPS轻松了50%,你可能还不知道!
前端·人工智能·后端
蓝-萧1 天前
【Java】如何使用jdbc连接并操作MySQL,一文读
java·后端
qianbailiulimeng1 天前
【MySQL】mysqldump使用方法
java·后端
李慕婉学姐1 天前
Springboot微信小程序在线考试系统w47h61gy(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·spring boot·微信小程序
Victor3561 天前
Redis(102)Redis的单线程性能为什么这么高?
后端