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 包。