当前位置:网站首页>Jasperreport generate PDF report section
Jasperreport generate PDF report section
2022-06-09 07:25:00 【woshihedayu】
1、 Use Jaspersoft Studio Make PDF Templates , Generate a jrxml file .
2、 compile jrxml file , Generate jasper file , Then copy it to springboot In the project resources Under the table of contents .
3、 Introduce the required dependencies
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.19.0</version>
</dependency>
4、 establish jasper Input stream object for file , Query the database , Get the data required by the report , Fill in PDF Inside , And then PDF The file is exported to the browser in the form of byte stream , Realize file download , The code is as follows
@SneakyThrows
@GetMapping("/{id}/pdf")
public void pdf(@PathVariable String id, HttpServletResponse response){
ClassPathResource resource = new ClassPathResource("templates/profile.jasper");
FileInputStream fis = new FileInputStream(resource.getFile());
ServletOutputStream sos=null;
try {
response.addHeader("Content-Disposition","attachment;filename=employee.pdf");
sos= response.getOutputStream();
// Employee details
UserCompanyPersonal personal = userCompanyPersonalService.getById(id);
// Employee position information
UserCompanyJobs jobs = userCompanyJobsService.getById(id);
// Employee Avatar
String staffPhoto = systemFeignClient.getPhoto(id);
Map<String,Object> params=new HashMap<>();
Map<String, Object> personalMap = BeanMapUtils.beanToMap(personal);
Map<String, Object> jobsMap = BeanMapUtils.beanToMap(jobs);
params.putAll(personalMap);
params.putAll(jobsMap);
params.put("staffPhoto",staffPhoto);
JasperPrint jasperPrint = JasperFillManager.fillReport(fis, params,new JREmptyDataSource());
JasperExportManager.exportReportToPdfStream(jasperPrint,sos);
}finally {
sos.flush();
sos.close();
}
}
among ,params It's a map object , Used to direct to PDF Fill in the data , If you execute first
params.put("staffPhoto",staffPhoto);
After execution
params.putAll(personalMap);
params.putAll(jobsMap);
It can lead to params Medium staffPhoto Value is empty , Eventually lead to PDF Export is empty , The code runs with an error , and personalMap and jobsMap It doesn't contain staffPhoto This key . This phenomenon is very strange , Because of this error , Stuck me for a long time , Later adopted debug Only at the breaking point can we find the problem .
5、JasperReport Chinese is not supported by default , So it will lead to PDF Chinese in cannot be displayed , The solution is in resources Add fonts.xml、stsong.TTF and jasperreports_extension.properties Three files , As shown below
among ,stsong.TTF It is a font file in Chinese Song typeface ,fonts.xml The code is as follows
<?xml version="1.0" encoding="UTF-8"?>
<fontFamilies>
<!--<fontFamily name="Lobster Two">-->
<!--<normal>lobstertwo/LobsterTwo-Regular.otf</normal>-->
<!--<bold>lobstertwo/LobsterTwo-Bold.otf</bold>-->
<!--<italic>lobstertwo/LobsterTwo-Italic.otf</italic>-->
<!--<boldItalic>lobstertwo/LobsterTwo-BoldItalic.otf</boldItalic>-->
<!--<pdfEncoding>Identity-H</pdfEncoding>-->
<!--<pdfEmbedded>true</pdfEmbedded>-->
<!--<!–-->
<!--<exportFonts>-->
<!--<export key="net.sf.jasperreports.html">'Lobster Two', 'Times New Roman', Times, serif</export>-->
<!--</exportFonts>-->
<!--–>-->
<!--</fontFamily>-->
<fontFamily name=" Chinese Songti ">
<normal>stsong/stsong.TTF</normal>
<bold>stsong/stsong.TTF</bold>
<italic>stsong/stsong.TTF</italic>
<boldItalic>stsong/stsong.TTF</boldItalic>
<pdfEncoding>Identity-H</pdfEncoding>
<pdfEmbedded>true</pdfEmbedded>
<exportFonts>
<export key="net.sf.jasperreports.html">' Chinese Songti ', Arial, Helvetica, sans-serif</export>
<export key="net.sf.jasperreports.xhtml">' Chinese Songti ', Arial, Helvetica, sans-serif</export>
</exportFonts>
<!-- <locales> <locale>en_US</locale> <locale>de_DE</locale> </locales> -->
</fontFamily>
</fontFamilies>
jasperreports_extension.properties The code is as follows
net.sf.jasperreports.extension.registry.factory.simple.font.families=net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory
net.sf.jasperreports.extension.simple.font.families.dejavu=stsong/fonts.xml
边栏推荐
- 软件设计文档最容易忽略哪些?
- Cognition of clothing and textile industry
- 当内卷风波及代码领域,看Alluxio将会采取怎样的块分配策略
- Read dates in various formats from excel and convert them to target formats
- PostgreSQL database replication - background first-class citizen process walreceiver extracts information
- C语言fopen函数的编程实现(可直接粘贴走验证)
- InfoWorld article - applying data orchestration technology to AI model training
- Technology sharing | discussion on the design of dispatching platform
- 蓝桥杯电子类单片机第十一届决赛试题
- 如何解决低代码平台中的安全问题?
猜你喜欢
![[STL] Introduction to the use of set and map](/img/04/cf9deb990af72eece3d081a563c7fc.jpg)
[STL] Introduction to the use of set and map

政务大厅导航系统功能解读

How to prevent the biggest cloud security threat

Opengauss database operation steps

多线程重点知识归纳总结。

WPF 数据绑定(二)

新职业:数字化管理师太抢手,只要来面试,企业就会发offer

PostgreSQL数据库复制——后台一等公民进程WalReceiver ready_to_display

The original, skillful and vulgar skills of cloud

Distributed message middleware -- Introduction to message queue
随机推荐
openGauss 数据库操作步骤
[PPO attitude control] Simulink simulation of UAV attitude control based on reinforcement policy optimization PPO training
[NewOJ Week 1]---CDE
Performance test process, index, implementation method and summary
100 Go Mistakes and How to Avoid Them 之代码结构和项目组织(四)
Opengauss database operation steps
Talk about the ten mistakes often made in implementing data governance
生产者消费者问题
「冲刺大厂基础2」
Flutter learning Hello World
推送相关的总结
Peripheral driver library development notes 42:dac8552 DAC driver
The 11th finals of the electronic single chip microcomputer of the Blue Bridge Cup
Programming implementation of C language fopen function (can be directly pasted for verification)
Reasons for failure to start redis container with docker
Don't waste your spare time. Playing with mobile phones can start "sideline life"
Some pitfalls encountered in using Renren open source project
Common interview questions: Browser input domain name and open the completed process
Recruitment | go to work, kill werewolves after work, and recruit people every day. I'm afraid it's going to happen!
Database operation statement