1.下载JasperReport工具
下载地址:社区版 - Jaspersoft 社区https://community.jaspersoft.com/download-jaspersoft/community-edition/
邮箱:lorettepatri.ckoa5434@gmail.com
密码:Zx123456.
2.工具使用方法注意
1.一次参数需要在左下角Parameters中新建,直接拖转右上角的TextField不会自动新建参数,到头来还是要在Parameters中新建
2.循环参数需要在左下角Fields中新建List中对象对应参数字段
3.常用的属性调整
4.字体设置,不支持中文,需要手动指定
5.排版时如何排版List数据
以本格式为例,中间的需要循环暂时,上下只显示一次.
参考数据:
我这里不是表格形式,所以没有列头,没用到Column Header,具体需根据自己情况安装
3.项目代码使用
1.引入必要文件
fonts.xml内容如下,只需要更改路径即可
<?xml version="1.0" encoding="UTF-8"?>
<fontFamilies>
<!--
可以配置多个字体
name 属性:指定字体名称,这里的字体名称在 JasperReport 模板文件中使用的要一致,才能够匹配上
-->
<fontFamily name="MSYaHei">
<!-- 正常字体路径 -->
<normal><![CDATA[font/STSONG.TTF]]></normal> <!-- 这里面为字体路径 -->
<!-- 加粗字体路径 -->
<bold><![CDATA[font/STSONG.TTF]]></bold> <!-- 这里面为字体路径 -->
<!-- 斜体字体路径 -->
<italic><![CDATA[font/STSONG.TTF]]></italic> <!-- 这里面为字体路径 -->
<!-- 加粗斜体字体路径 -->
<bolditalic><![CDATA[fonts/STSONG.TTF]]></bolditalic> <!-- 这里面为字体路径 -->
<pdfEmbedded><![CDATA[true]]></pdfEmbedded>
<pdfEncoding>Identity-H</pdfEncoding>
<exportFonts/>
</fontFamily>
</fontFamilies>
STSONG.TTF为华文宋体文件,这里的STSONG是文件名不是字体名,所以不用改成中文"华文宋体".字体下载地址
C:\Windows\Fonts
jasperreports_extension.properties内容如下
# ?????????
net.sf.jasperreports.extension.registry.factory.simple.font.families=net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory
# 这里是字体路径
net.sf.jasperreports.extension.simple.font.families.default=font/fonts.xml
2.引入POM,注意jasperreports引入了许多外部jar包,不是这有问题,就是那个包maven仓库没有,这是我找到的对于我自己项目没报错的版本
<!-- https://mvnrepository.com/artifact/net.sf.jasperreports/jasperreports -->
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.20.0</version>
<exclusions>
<!--
排除自带的itext依赖,因为自带的itext版本是 2.1.7.js10
这个版本在中央仓库里面没有,无法下载
-->
<exclusion>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 引入itext依赖,因为JasperReports中使用了itext操作PDF -->
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
3.建议把jrxml源文件拷贝到项目中,避免本地丢失
4.常见错误
1.java.lang.ClassNotFoundException大概率都是版本问题,换版本试一试是否还是同样错误
java.lang.ClassNotFoundException:com.lowagie.text.Document
2.jasperreort (wrong name: 大概率编译后的文件出了问题,我开始用的是工具编译,后面直接把jrxml文件放到项目中用代码编译成jasper文件,这样也可以保留jrxml文件在项目上,不会本地丢失
java.lang.NoClassDefFoundError: Jrxxxxxxxx _1811b31b27d13da1f8bc9955038dc0646b5de2a76581526d5a0228d015ebfd49 (wrong name: Jrxxxxxxxx )
5.简要代码(包含集合打印及多页打印)
public void downloadInspectPDF(String hisPatientNum,String dateTime) {
List<InspectPDFInfoVo> inspectPDFInfoList = inspectPDFMapper.getInspectPDFInfo(hisPatientNum, dateTime);
//new一个装多页打印JasperPrint的集合
List<JasperPrint> jasperPrintList = new ArrayList<>();
//我这里查询出来是一个集合数据,需要打印多页
for (InspectPDFInfoVo inspectPDFInfoVo : inspectPDFInfoList) {
inspectPDFInfoVo.setHisPatientNum(hisPatientNum);
//这里就是需要一页里面的集合,也是上述Detail中循环的数据
List<MedicationPDFInfo> medicationPDFInfoList = inspectPDFMapper.getMedicationPDFInfo(inspectPDFInfoVo.getInspectId());
List<Map> list=new ArrayList<>();
for (MedicationPDFInfo medicationPDFInfo : medicationPDFInfoList) {
Map<String, Object> medicationInfoMap = BeanUtil.beanToMap(medicationPDFInfo,false,true);
list.add(medicationInfoMap);
}
Map<String, Object> inspectPDFInfoVoMap = BeanUtil.beanToMap(inspectPDFInfoVo,false,true);
String fileName = "XXXXXXX";
try {
//jrxml路径
String jrxmlPath = ClassLoader.getSystemResource("").getPath() + "inspect_info.jrxml";
//编译后jasper的路径
String jasperPath = ClassLoader.getSystemResource("").getPath() + "inspect_info.jasper";
JasperCompileManager.compileReportToFile(jrxmlPath,jasperPath);
JasperPrint jasperPrint =null;
//我这边发现如果list为空,打印出来是空白的,就加了一个判断
if(CollectionUtils.isEmpty(list)){
jasperPrint = JasperFillManager.fillReport(jasperPath, inspectPDFInfoVoMap, new JREmptyDataSource());
}else{
jasperPrint = JasperFillManager.fillReport(jasperPath, inspectPDFInfoVoMap, new JRBeanCollectionDataSource(list));
}
jasperPrintList.add(jasperPrint);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletResponse response = servletRequestAttributes.getResponse();
try {
JasperPrinter.exportPDFToWebList(jasperPrintList, null, response);
} catch (JRException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
JasperPrinter.java
/**
* 连续读取多个模板
* @param jasperPrintList
* @param outputFileName
* @param response
* @throws JRException
* @throws IOException
*/
public static void exportPDFToWebList(List<JasperPrint> jasperPrintList, String outputFileName, HttpServletResponse response)
throws JRException, IOException {
ServletOutputStream outputStream = response.getOutputStream();
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JRPdfExporter exporter = new JRPdfExporter();
//这里是多页打印重点
exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrintList));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(baos));
exporter.exportReport();
byte[] bytes = baos.toByteArray();
// 写出文件的类型
response.setContentType("application/pdf;charset=UTF-8");
baos.close();
outputStream.write(bytes);
} finally {
outputStream.flush();
}
}