iReport是一个能够创建复杂报表的开源项目,并且它能调用JasperReports库应用于任何一种java应用程序。本身是分布式的且由符合GNU通用公共许可的百分之百纯java编写。
由于它丰富的图形界面,能够很快的创建出任何一种想要的报表。iReport能够让我们提高技术方面的信心,比如它能降低学习JasperReports的XML语法的难度,而且能够为熟练的技术人员节省大量的时间并能开发出非常精细的报表。
根据自己的需要来创建模板(左上角文件->new 进行创建)
Repoet name:模板名
Location:文件存放地址
File:模板文件全路径
new ByteArrayInputStream(new Base64().decodeBase64(new StringBuffer($F{photoBase64}).toString().getBytes("UTF-8")))
java
//ep.getData() 二进制类型数据
String s = Base64.getEncoder().encodeToString(ep.getData());
//转base64
//String base64 = ep.getBase64Header() + "," + s;
if (ep.getData() == null){
throwMsg("无照片,请先上传");
}
e.put("photoBase64", ep.getData());
二、java Demo示例
1. redisUtil工具类
import com.alibaba.fastjson.JSON;
import com.bjupi.utils.Utils;
import com.hyjs.examination.manage.system.action.PdfController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Component
public class RedisUtil {
private final StringRedisTemplate redisTemplate;
@Autowired
public RedisUtil(StringRedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
/**
* 设置过期时间
*/
public Boolean expire(String key, long timeout, TimeUnit unit) {
return redisTemplate.expire(key, timeout, unit);
}
public void hPut(String key, String hashKey, String value) {
redisTemplate.opsForHash().put(key, hashKey, value);
}
/**
* 获取存储在哈希表中指定字段的值
*/
public Object hGet(String key, String field) {
return redisTemplate.opsForHash().get(key, field);
}
/**
* 获取PDF打印临时token
* @param jasperName 报表名称(无需带后缀)
* @param pageData 页面数据(非迭代数据)
* @param iterationData 迭代数据
* @return
* @throws Exception
*/
public String getPdfToken(String jasperName, Map<String,Object> pageData, List<Map<String,Object>> iterationData){
String token = Utils.createUid();
hPut(PdfController.PDF_PRINT+token, PdfController.JASPER_NAME,jasperName);
if(pageData != null && pageData.size() > 0) {
hPut(PdfController.PDF_PRINT+token, PdfController.PAGE_DATA, JSON.toJSONString(pageData));
}
if(iterationData != null && iterationData.size() > 0) {
hPut(PdfController.PDF_PRINT+token, PdfController.ITERATION_DATA, JSON.toJSONString(iterationData));
}
expire(PdfController.PDF_PRINT+token, 300, TimeUnit.SECONDS);
return token;
}
}
2. controller使用
先调用 /getPrintToken 得到token
再将token作为参数传入/public/pdf-v1
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.hyjs.examination.manage.dict.ResponseData;
import io.swagger.annotations.ApiOperation;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.util.JRLoader;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import com.hyjs.examination.manage.util.RedisUtil;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Component
public class PdfController {
@Autowired
private RedisUtil redisUtil;
//路径名称
public static final String JASPERS_LOCAL = "jaspers";
public static final String PDF_PRINT = "PDF:";
public static final String JASPER_NAME = "JASPER_NAME";
public static final String PAGE_DATA = "PAGE_DATA";
public static final String ITERATION_DATA = "ITERATION_DATA";
@ApiOperation("PDF打印v1版本")
@GetMapping(value = "/public/pdf-v1")
public String getReportByParam(@RequestParam String token, HttpServletResponse response) {
if (StringUtils.isBlank(token)) {
return "缺少必要参数[token]";
}
Object o = redisUtil.hGet(PDF_PRINT + token, JASPER_NAME);
if (o == null || StringUtils.isBlank(o.toString())) {
return "等待打印时间超时,请重新 *** 作";
}
String jasperName = o.toString();
String pageData = null;
Object o1 = redisUtil.hGet(PDF_PRINT + token, PAGE_DATA);
if (o1 != null) {
pageData = JSON.toJSONString(o1);
}
String iterationData = null;
Object o2 = redisUtil.hGet(PDF_PRINT + token, ITERATION_DATA);
if (o2 != null) {
iterationData = o2.toString();
}
return getReportByParam(jasperName, pageData, iterationData, response);
}
private String getReportByParam(String jasperName, String pageData, String iterationData, HttpServletResponse response) {
String msg = "";
if (StringUtils.isBlank(jasperName)) {
msg = "缺少必要参数[jasperName]";
} else {
ClassPathResource resource = new ClassPathResource(JASPERS_LOCAL + File.separator + jasperName + ".jasper");
InputStream jasperStream = null;
List<Map<String, Object>> data = new ArrayList<>();
try {
if (StringUtils.isNotBlank(iterationData)) {
List<Map<String, Object>> list1 = JSON.parseObject(iterationData, new TypeReference<List<Map<String, Object>>>() {
});
if (list1 != null && list1.size() > 0) {
data.addAll(list1);
}
}
JSONObject page = null;
if (StringUtils.isNotBlank(pageData)) {
page = JSONObject.parseObject(pageData);
}
if (page != null) {
if (data.size() > 0) {
for (Map<String, Object> mapInner : data) {
mapInner.putAll(page);
}
} else {
data.add(page);
}
}
JRDataSource source = new JRBeanCollectionDataSource(data);
jasperStream = resource.getInputStream();
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);
JasperPrint jasperPrint = null;
if (data.size() > 0) {
jasperPrint = JasperFillManager.fillReport(jasperReport, null, source);
} else {
jasperPrint = JasperFillManager.fillReport(jasperReport, null, new net.sf.jasperreports.engine.JREmptyDataSource());
}
// pdf
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline;");
OutputStream outputStream = response.getOutputStream();
JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
} catch (Exception e) {
e.printStackTrace();
msg = e.getMessage();
}
}
return msg;
}
@ApiOperation("给考生打印证书(批量or单个)")
@PostMapping(value = "/getPrintToken")
public ResponseData getPrintToken(@RequestBody String data){
JSONObject jsonObject = null;
try {
jsonObject = JSON.parseObject(data);
} catch (Exception e) {
return ResponseData.warnWithMsg("错误的JSON格式。");
}
if (jsonObject == null) {
return ResponseData.warnWithMsg("错误的JSON格式。");
}
String planUuids = jsonObject.getString("planUuids");
String examUuids = jsonObject.getString("examUuids");
if (StringUtils.isBlank(examUuids)) {
return ResponseData.warnWithMsg("缺少必要参数[examUuids]。");
}
//ResponseData自己封装的返回类 printService.getPrintToken:数据
ResponseData examPrintDatas = printService.getPrintToken(planUuids, examUuids);
List<Map<String, Object>> printInfoDatas = (List<Map<String, Object>>)examPrintDatas.getData();
String jasperName = jsonObject.getString("jasperName");
if (StringUtils.isBlank(jasperName)) {
return ResponseData.warnWithMsg("缺少必要参数[jasperName]。");
}
String token = redisUtil.getPdfToken(jasperName, null, printInfoDatas);
return ResponseData.success(token);
}
}
3. pom
net.sf.jasperreports
jasperreports
6.6.0
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)