SpringBoot(十六)_springboot整合JasperReport6.6.0
现在项目上要求实现套打,结果公司里有个人建议用JaperReport进行实现,就进入这个东西的坑中。好歹经过挣扎现在已经脱离此坑中。现在我也是仅能实现读取数据库数据转成pdf进行展示,包括中文的展示。于是记录下整个过程。
1.下载 安装 Jaspersoft Studio
下载地址:https://community.jaspersoft.com/community-download

我下载的就是6.6.0这个版本,Jasper Report 分为专业版(收费)和社区版(免费),这里下载的社区版本。
2.设计模板
对这个模板设计我也不是很熟悉,这里我就不展开说明了。大家自行设计吧
2.1 导入并设置字体
这里需要注意一点就是,这个设计出的不显示中文,需要导入字体。
点击window->Preferences->jaspersoft Studio->font->add

设置完成后,点击Finish.
2.2 设计模板选择设置的myfont字体
查看jrxml文件后,设置后会多出font标签
<font fontName="myfont" size="26" pdfFontName="" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
</textElement>
<text><![CDATA[三年二班学生信息]]></text>
2.3 导出myfont字体jar包
点击window->Preferences->jaspersoft Studio->font
选择myfont 点击export 导出。这里我保存为kevin.jar
2.4 将kevin.jar 安装到本地maven库
mvn install:install-file -Dfile=kevin.jar -DgroupId=com.kevin -DartifactId=myfont -Dversion=1.0.0 -Dpackaging=jar
至此,前期的准备工作都已经完成。
3.构建springboot项目
这里我使用的版本是 2.1.1.RELEASE
3.1 pom文件
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.6.0</version>
</dependency>
<dependency>
<groupId>com.kevin</groupId>
<artifactId>myfont</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
注意 ,我这里引用的是我设置的com.kevin.myfont版本,个人根据自己步骤2.4设置的情况进行修改
3.2 application.yml
# Server settings
server:
port: 8080
# SPRING PROFILES
spring:
http:
encoding.charset: UTF-8
encoding.enable: true
encoding.force: true
datasource:
url: jdbc:mysql://127.0.0.1:3306/kevin?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
3.3 ReportController 代码
@RestController
public class ReportController {
@Resource
private DataSource dataSource;
/**
* 转换为pdf展示
*
* @param reportName
* @param parameters
* @param response
* @throws SQLException
* @throws ClassNotFoundException
* @throws JRException
* @throws IOException
*/
@GetMapping("/{reportName}")
public void getReportByParam(
@PathVariable("reportName") final String reportName,
@RequestParam(required = false) Map<String, Object> parameters,
HttpServletResponse response) throws SQLException, ClassNotFoundException, JRException, IOException {
parameters = parameters == null ? new HashMap<>() : parameters;
//获取文件流
ClassPathResource resource = new ClassPathResource("jaspers" + File.separator + reportName + ".jasper");
InputStream jasperStream = resource.getInputStream();
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource.getConnection());
// JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, new JREmptyDataSource());
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline;");
final OutputStream outputStream = response.getOutputStream();
JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
}
}
3.4 完整目录结构

4.运行效果
启动项目,运行http://localhost:8080/demo

完整代码
github:https://github.com/runzhenghengbin/SpringBoot
玩的开心!
SpringBoot(十六)_springboot整合JasperReport6.6.0的更多相关文章
- SpringBoot(十二)_springboot整合PageHelper
我之所以会发现这个PageHelper这个东东 是因为公司在使用 ,刚开始我也没太注意这个插件,感觉不就是个分页插件吗?也就那样,直到一天,我在网上找了个代码生成器,用来构建代码,因为它是针对mysq ...
- springboot(十六):springboot整合shiro
数据库有五张表(userInfo,uerrole,role,rolepermission,permission) userInfo(id,username,password) userrole(uid ...
- springboot(十六)-swagger2
SpringBoot整合Swagger2 相信各位在公司写API文档数量应该不少,当然如果你还处在自己一个人开发前后台的年代,当我没说,如今为了前后台更好的对接,还是为了以后交接方便,都有要求写API ...
- SpringBoot(十四)_springboot使用内置定时任务Scheduled的使用(一)
为什么使用定时? 日常工作中,经常会用到定时任务,比如各种统计,并不要求实时性.此时可以通过提前设置定时任务先把数据跑出来,后续处理起来更方便. 本篇文章主要介绍 springboot内置定时任务. ...
- SpringBoot(十六):SpringBoot2.1.1集成fastjson,并使用fastjson替代默认的MappingJackson
springboot2.1.1默认采用的json converter是MappingJackson,通过调试springboot项目中代码可以确定这点.在springboot项目中定义WebMvcCo ...
- springboot(十六):使用Jenkins部署Spring Boot
jenkins是devops神器,本篇文章介绍如何安装和使用jenkins部署Spring Boot项目 jenkins搭建 部署分为三个步骤: 第一步,jenkins安装 第二步,插件安装和配置 第 ...
- SpringBoot(十五)_springboot实现预览pdf
最近,项目上要做个打印的东西,还要预览.我想就直接生成pdf预览,然后用户选择打印 于是,昨天找了找资料.一般用itext 进行转pdf.于是我就用springboot试了试,代码比较简单,现在只是简 ...
- Java微信公众平台开发(十六)--微信网页授权(OAuth2.0授权)获取用户基本信息
转自:http://www.cuiyongzhi.com/post/78.html 好长时间没有写文章了,主要是最近的工作和生活上的事情比较多而且繁琐,其实到现在我依然还是感觉有些迷茫,最后还是决定静 ...
- SpringBoot(十八)_springboot打成war包部署
最近在做项目的时候,由于使用的是springboot,需要打成war包.我就按照正常的思路去打包,结果部署后无法访问,一直报错404.后续问了问 公司同事,他给解决了.说大部分都是这个原因. 如果需要 ...
随机推荐
- Spring容器AOP的实现原理——动态代理(转)
文章转自http://blog.csdn.net/liushuijinger/article/details/37829049#comments
- http协议以及get和post请求
HTTP协议是网络传输信息的一种规范. 就好比两个人之间的交流,甲只会讲英语,乙只会说汉语,结果是他们必然无法开怀畅谈. HTTP协议也类 GET 请求获取 Request-URI 所标识的资源 ...
- java Scanner类的使用
参考链接:https://blog.csdn.net/android_depon/article/details/69669160 https://www.cnblogs.com/zhengc ...
- PHP_EOL换行 与 base64编码
base64编码包括64个字符:10个数字(0-9),26*2个字母(a-zA-Z),+,/ 其中还有一个第65个字符=作为后缀,没有实际作用. 来一段代码说明个问题: <?php $str = ...
- 如何学习 Webpack
webpack-howto Tip: 本文是 webpack-howto 的原文,我觉得这篇文章写得非常好,确实算是目前学习 webpack 入门的必读文章.直接收录之. 本教程的目标 这是一本教你如 ...
- 【LeetCode191】Number of 1 Bits★
1.题目 2.思路 方法一:常规方法. 方法二:给面试官惊喜的解法. 3.java代码 方法一代码: public class Solution { // you need to treat n as ...
- flask-script&flask-migrate使用
一.简介 Flask-script扩展提供向Flask插入外部脚本的功能,包括运行一个开发用的服务器,一个定制的Python shell.设置数据库的脚本.cronjobs及其他运行在web应用之外的 ...
- 介绍HTTP协议的传输过程
1.HTTP是面向事物的应用层协议,它使用TCP连接进行可靠传输,服务器默认监听在80端口 2.服务流程 从协议执行过程来说,当浏览器要访问www服务器时,首先要对服务器进行域名解析(DNS协议).一 ...
- DOS文件转换成UNIX文件格式详解
转:DOS文件转换成UNIX文件格式详解 由windows平台迁移到unix系统下容易引发的问题:Linux执行脚本却提示No such file or directory dos格式文件传输到uni ...
- Azure Load Balancer : 简介
Azure 提供的负载均衡服务叫 Load Balancer,它工作在 ISO 七层模型的第四层,通过分析 IP 层及传输层(TCP/UDP)的流量实现基于 "IP + 端口" 的 ...