基于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);
        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(file.getPath());
            //添加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

基于SpringBoot项目集成iReport

上一篇:JAVA 获取/设置bean的某个属性值


下一篇:java开篇之路:数据类型转换及变量常量作用域