博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Boot集成JasperReports生成PDF文档
阅读量:6237 次
发布时间:2019-06-22

本文共 7639 字,大约阅读时间需要 25 分钟。

由于工作需要,要实现后端根据模板动态填充数据生成PDF文档,通过技术选型,使用Ireport5.6来设计模板,结合JasperReports5.6工具库来调用渲染生成PDF文档。本人文采欠缺,写作能力差,下面粗略的介绍其使用步骤,若有不对的地方,望大家莫喷,谢谢!

一、使用Ireport designer 5.6设计模板

ireport的使用由于时间关系不便多说,设计好之后,将其进行编译生成jasper文件,然后将其拷贝放置于项目某个目录下,以供访问获取该文件。

二、在Spring Boot项目中引入依赖包

net.sf.jasperreports
jasperreports
6.0.0
net.sf.jasperreports
jasperreports-fonts
6.0.0
com.itextpdf
itextpdf
5.5.0
com.itextpdf
itext-pdfa
5.5.0
com.itextpdf
itext-asian
5.2.0
org.codehaus.groovy
groovy-all
2.4.11
复制代码

三、自定义spring mvc通用导出PDF视图类PdfReportView(继承自AbstractView )

import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.UnsupportedEncodingException;import java.net.URLEncoder;import java.util.HashMap;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import net.sf.jasperreports.engine.JRDataSource;import net.sf.jasperreports.engine.JREmptyDataSource;import net.sf.jasperreports.engine.JasperExportManager;import net.sf.jasperreports.engine.JasperFillManager;import net.sf.jasperreports.engine.JasperPrint;import org.springframework.web.servlet.view.AbstractView;public class PdfReportView extends AbstractView {    private static final String CONTENT_TYPE = "application/pdf";    private String templatePath;    private String exportFileName;    public PdfReportView(String templatePath, String exportFileName) {        this.templatePath = templatePath;        if (exportFileName != null) {            try {                exportFileName = URLEncoder.encode(exportFileName, "UTF-8");            } catch (UnsupportedEncodingException e) {                e.printStackTrace();            }        }        this.exportFileName = exportFileName;        setContentType(CONTENT_TYPE);    }    protected Map
getParamsMap(Map
map) throws Exception { Map
params = new HashMap<>(); for (String key : map.keySet()) { Object val = map.get(key); if (val instanceof JRDataSource) { continue; } else { params.put(key, val); } } return params; } protected JRDataSource getDataSource(Map
map) throws Exception { for (String key : map.keySet()) { Object val = map.get(key); if (val instanceof JRDataSource) { return (JRDataSource) map.get(key); } } return new JREmptyDataSource(); } @Override protected void renderMergedOutputModel(Map
map, HttpServletRequest request, HttpServletResponse response) throws Exception { response.setCharacterEncoding("utf-8"); response.setContentType(getContentType()); //response.setContentType("application/octet-stream"); response.setHeader("content-disposition", "attachment;filename=" + exportFileName + ".pdf"); try (InputStream inputStream = Thread.currentThread().getContextClassLoader() .getResourceAsStream(templatePath)) { try (OutputStream ouputStream = response.getOutputStream()) { JasperPrint jasperPrint = JasperFillManager .fillReport(inputStream, getParamsMap(map), getDataSource(map)); JasperExportManager.exportReportToPdfStream(jasperPrint, ouputStream); } catch (IOException e) { e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } }}复制代码

四、spring mvc调用案例

Map
map = new HashMap<>();List
> dataList = ....map.put("users", new JRMapCollectionDataSource(dataList));map.put("title", "报表名称");return new ModelAndView(new PdfReportView("templates/report/resume.jasper", "professors"),map);复制代码

五、解决linux下宋体渲染问题

1、在src/main/resources路径下新建jasperreports_extension.properties文件,内容如下:

net.sf.jasperreports.awt.ignore.missing.font=true net.sf.jasperreports.extension.registry.factory.simple.font.families=net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory net.sf.jasperreports.extension.simple.font.families.dejavu=fonts/fonts.xml 复制代码

2、在src/main/resources路径下新建fonts目录,并新建fonts.xml文件,内容如下:

net/sf/jasperreports/fonts/dejavu/DejaVuSans.ttf
net/sf/jasperreports/fonts/dejavu/DejaVuSans-Bold.ttf
net/sf/jasperreports/fonts/dejavu/DejaVuSans-Oblique.ttf
net/sf/jasperreports/fonts/dejavu/DejaVuSans-BoldOblique.ttf
Identity-H
true
'DejaVu Sans', Arial, Helvetica, sans-serif
'DejaVu Sans', Arial, Helvetica, sans-serif
net/sf/jasperreports/fonts/dejavu/DejaVuSerif.ttf
net/sf/jasperreports/fonts/dejavu/DejaVuSerif-Bold.ttf
net/sf/jasperreports/fonts/dejavu/DejaVuSerif-Italic.ttf
net/sf/jasperreports/fonts/dejavu/DejaVuSerif-BoldItalic.ttf
Identity-H
true
'DejaVu Serif', 'Times New Roman', Times, serif
'DejaVu Serif', 'Times New Roman', Times, serif
net/sf/jasperreports/fonts/dejavu/DejaVuSansMono.ttf
net/sf/jasperreports/fonts/dejavu/DejaVuSansMono-Bold.ttf
net/sf/jasperreports/fonts/dejavu/DejaVuSansMono-Oblique.ttf
net/sf/jasperreports/fonts/dejavu/DejaVuSansMono-BoldOblique.ttf
Identity-H
true
'DejaVu Sans Mono', 'Courier New', Courier, monospace
'DejaVu Sans Mono', 'Courier New', Courier, monospace
'DejaVu Sans', Arial, Helvetica, sans-serif
'DejaVu Sans', Arial, Helvetica, sans-serif
'DejaVu Serif', 'Times New Roman', Times, serif
'DejaVu Serif', 'Times New Roman', Times, serif
'DejaVu Sans Mono', 'Courier New', Courier, monospace
'DejaVu Sans Mono', 'Courier New', Courier, monospace
fonts/simsun.ttf
fonts/simsun.ttf
fonts/simsun.ttf
fonts/simsun.ttf
Identity-H
true
'宋体', Arial, Helvetica, sans-serif
'宋体', Arial, Helvetica, sans-serif
复制代码

3、在src/main/resources/fonts目录放入宋体字体文件 simsun.ttf

转载地址:http://sgkia.baihongyu.com/

你可能感兴趣的文章
python模块paramiko的上传下载和远程执行命令方法
查看>>
SAS硬盘和SATA硬盘的介绍和区别
查看>>
开源TMS团队协作web系统
查看>>
linux批量修改文件夹,文件的权限,所属用户,用户组
查看>>
qt5.0.2 + opencv244
查看>>
elasticsearch日期类型
查看>>
产品经理应该如何学习
查看>>
做个精致的程序员
查看>>
挂载U盘参数大全
查看>>
解决macbook键盘失灵问题
查看>>
我总是在市井小人成堆的地方修身养性 对山中云蒸雾缭的神仙往事嗤之以鼻...
查看>>
转载 oracle数据库名、实例名、ORACLE_SID、数据库域名、全局数据库名、服务名...
查看>>
Eclipse 配置 Intellij Idea 主题
查看>>
建立软件开发团队时要避免的7个问题
查看>>
将字符序列用其反转形式取代
查看>>
在Eclipse中制作和使用struts2配置文件提示插件
查看>>
操作系统
查看>>
从需求向设计转化的密码
查看>>
360浏览器极速模式pdf文件不能预览问题
查看>>
CAS:PKIX path building failed
查看>>