• 基于SpringBoot项目集成iReport


    学而时习之,不亦说乎

    基于SpringBoot项目集成iReport,实现预览、下载PDF功能。

    具体步骤如下:

    1. 加入相关依赖
    <!-- PDF打印相关依赖 -->
    	<dependency>
    		<groupId>net.sf.jasperreports</groupId>
    		<artifactId>jasperreports</artifactId>
    		<version>6.17.0</version>
    	</dependency>
    	<dependency>
    		<groupId>org.codehaus.groovy</groupId>
    		<artifactId>groovy-all</artifactId>
    		<version>2.4.11</version>
    	</dependency>
    	<!-- 解决中文字体显示问题 -->
    	<dependency>
    		<groupId>cn.lesper</groupId>
    		<artifactId>iTextAsian</artifactId>
    		<version>3.0</version>
    	</dependency>
    
    1. 在项目的resource目录下创建ireport文件夹,将在ireport设计器中设计好的test.jrxml文件,复制黏贴入该文件夹下。

    2. 提供service方法

        @Autowired
        private DataSource dataSource;
        
        @Override
        public void optPdf(HttpServletResponse response,String businessUuid,Integer optType) throws Exception {
    
            String fileName = "test.jrxml";
            String pdfName = "test";
            //File file = ResourceUtils.getFile("classpath:ireport/" + fileName);
            //jar部署方式需要以以下方式获取文件
            InputStream is = Test.class.getResourceAsStream("/ireport/" + fileName);
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/pdf");
            response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(pdfName, "UTF-8") + ".pdf");
    
            try {
                //编译jasperReport
                JasperReport jasperReport = JasperCompileManager.compileReport(is);
                //添加iReport入参
                HashMap<String, Object> params = new HashMap<>();
                params.put("uuid",businessUuid);
                //获取outputStream
                OutputStream outputStream = response.getOutputStream();
                if(optType == 1){
                    //预览
                    byte[] bytes = JasperRunManager.runReportToPdf(jasperReport,params,dataSource.getConnection());
                    response.setContentType("application/pdf;charset=UTF-8");
                    response.setHeader("content-disposition","inline");
                    response.setContentLength(bytes.length);
                    outputStream.write(bytes, 0, bytes.length);
                    outputStream.flush();
                    outputStream.close();
                }else{
                    //下载
                    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params,dataSource.getConnection());
                    JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
                }
    
            } catch (IOException | JRException e) {
                e.printStackTrace();
            }
        }
    
    1. 创建一个Controller类,增加相应API接口。
        @ApiOperation("业务申请单PDF预览")
        @GetMapping("/showPdf")
        public void showPdf(HttpServletResponse response,@RequestParam String businessUuid)throws Exception{
            resourceManageService.optPdf(response,businessUuid,1);
        }
    
        @ApiOperation("业务申请单PDF下载")
        @GetMapping("/downPdf")
        public void downPdf(HttpServletResponse response,@RequestParam String businessUuid)throws Exception{
            resourceManageService.optPdf(response,businessUuid,2);
        }
    
    1. 启动SpringBoot项目

    预览方式访问:

    http://127.0.0.1:8080/showPdf?businessUuid=1111

    下载方式访问:

    http://127.0.0.1:8080/downPdf?businessUuid=1111

  • 相关阅读:
    作品第二课----改变DIV任意属性的值
    今天遇到的mouseout和mouseleave之坑
    作品第二课----弹出层
    作品第二课----求数组中所有数字的和
    作品第二课----点击DIV显示其内容
    作品第二课----点击切换显示隐藏
    Linux 静态库与动态库
    Linux学习6-套接字
    Linux学习5-线程
    Linux学习4-信号
  • 原文地址:https://www.cnblogs.com/conswin/p/15220559.html
Copyright © 2020-2023  润新知