快速学习JasperReport-用户档案下载

5 用户档案下载

5.1 搭建环境

(1) 配置坐标

代码语言:javascript
复制
  		<dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports</artifactId>
            <version>6.5.0</version>
        </dependency>
        <dependency>
            <groupId>org.olap4j</groupId>
            <artifactId>olap4j</artifactId>
            <version>1.2.0</version>
        </dependency>
        <dependency>
            <groupId>com.lowagie</groupId>
            <artifactId>itext</artifactId>
            <version>2.1.7</version>
        </dependency>

(2)解决乱码问题

代码语言:javascript
复制
net.sf.jasperreports.extension.registry.factory.simple.font.families=net.sf.jasperrepor
ts.engine.fonts.SimpleFontExtensionsRegistryFactory
net.sf.jasperreports.extension.simple.font.families.lobstertwo=stsong/fonts.xml

5.2 实现用户档案下载

代码语言:javascript
复制
/**
     * 打印员工pdf报表x
     */
    @RequestMapping(value="/{id}/pdf",method = RequestMethod.GET)
    public void pdf(@PathVariable String id) throws IOException {
        //1.引入jasper文件
        Resource resource = new ClassPathResource("templates/profile.jasper");
        FileInputStream fis = new FileInputStream(resource.getFile());
        //2.构造数据
        //a.用户详情数据
        UserCompanyPersonal personal = userCompanyPersonalService.findById(id);
        //b.用户岗位信息数据
        UserCompanyJobs jobs = userCompanyJobsService.findById(id);
        //c.用户头像       域名 / id
        String staffPhoto = "http://pkbivgfrm.bkt.clouddn.com/"+id;
        System.out.println(staffPhoto);
        //3.填充pdf模板数据,并输出pdf
        Map params = new HashMap();
        Map<String, Object> map1 = BeanMapUtils.beanToMap(personal);
        Map<String, Object> map2 = BeanMapUtils.beanToMap(jobs);
        params.putAll(map1);
        params.putAll(map2);
        params.put("staffPhoto","staffPhoto");
        ServletOutputStream os = response.getOutputStream();
        try {
            JasperPrint print = JasperFillManager.fillReport(fis, params,new JREmptyDataSource());
            JasperExportManager.exportReportToPdfStream(print,os);
       } catch (JRException e) {
            e.printStackTrace();
       }finally {
            os.flush();
       }
   }