SpringMVC使用iReport生成pdf

1.要生成pdf首先需要通过iReport生成pdf模板,详情自行百度。


2.生成pdf模板之后,将.jrxml和.jasper文件放在java项目的某个文件夹中。.jrxml文件是可编辑的文件,.jasper文件不可编辑。

3.最重要的部分来了,见下面:

  @RequestMapping("print")
    @ResponseBody
    public void  printFxt(Long tm_uuid, HttpServletRequest request, HttpServletResponse response) throws IOException{
                //必须是map集合
        Map<String, Object> parameters = this.carSeqMapper.selectFxt(tm_uuid);
        ServletOutputStream outPutStream = response.getOutputStream();
                //注意.jasper的是相对路径,不是绝对路径
        InputStream inputStream = request.getServletContext().getResourceAsStream("/template/cljcfxt.jasper");
        parameters.put("pic1", "check.png");
        parameters.put("pic2", "check.png");
        parameters.put("pic3", "check.png");
        parameters.put("pic4", "check.png");
        parameters.put("contextPath", request.getServletContext().getRealPath("/"));

                //按条件选择复选框
        String operType = (String) parameters.get("oper_type");
        if("1".equals(operType)){
            parameters.put("pic1", "checked.png");
        }
        else if("2".equals(operType)){
            parameters.put("pic2", "checked.png");
        }

        JRDataSource jrDataSource = new JREmptyDataSource();
        try {
                        //生成网页pdf
            JasperRunManager.runReportToPdfStream(inputStream, outPutStream, parameters, jrDataSource);
            response.setContentType("application/pdf");  
        } catch (Exception e) {
            e.printStackTrace();
            response.setContentType("text/plain; charset=utf-8");
            response.setCharacterEncoding("utf-8");
            outPutStream.write("生成pdf文件时,服务器发生异常".getBytes("utf-8"));
        }
        finally{
            if(outPutStream != null){
                outPutStream.flush();
                outPutStream.close();
            }
        }
    }

4.jrxml文件里面的变量属性要跟map集合里面的key值相对应,要修改jrxml直接在ireport里面修改,编译之后再放到java项目里面去覆盖之前的文件。

5.pom.xml文件中引入以下内容:

<dependency>
            <groupId>feiliks.hn</groupId>
            <artifactId>jasperreports</artifactId>
            <version>6.4.1</version>
        </dependency>

        <dependency>
            <groupId>com.lowagie</groupId>
            <artifactId>itext</artifactId>
            <version>2.1.7</version>
            <scope>compile</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.bouncycastle</groupId>
                    <artifactId>bcmail-jdk14</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.bouncycastle</groupId>
                    <artifactId>bcprov-jdk14</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>feiliks.hn</groupId>
            <artifactId>feiliks-font</artifactId>
            <version>1.0.1</version>
        </dependency>

        <dependency>
            <groupId>commons-digester</groupId>
            <artifactId>commons-digester</artifactId>
            <version>2.1</version>
        </dependency>

        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>2.4.5</version>
        </dependency>

6.页面跳转要跳到一个新的页面,即<a>标签里面设置target="_blank"。

7.页面pdf自带打印功能,OK了,一个动态生成PDF的功能就实现了。






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值