专栏首页pangguomingSpring Boot集成JasperReports生成PDF文档

Spring Boot集成JasperReports生成PDF文档

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

一、使用Ireport designer 5.6设计模板

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

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

<!-- jasperreports-->
<dependency>
    <groupId>net.sf.jasperreports</groupId>
    <artifactId>jasperreports</artifactId>
    <version>6.0.0</version>
</dependency>
<dependency>
    <groupId>net.sf.jasperreports</groupId>
    <artifactId>jasperreports-fonts</artifactId>
    <version>6.0.0</version>
</dependency>
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.0</version>
</dependency>
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itext-pdfa</artifactId>
    <version>5.5.0</version>
</dependency>
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itext-asian</artifactId>
    <version>5.2.0</version>
</dependency>
<dependency>
    <groupId>org.codehaus.groovy</groupId>
    <artifactId>groovy-all</artifactId>
    <version>2.4.11</version>
</dependency>

三、自定义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<String, Object> getParamsMap(Map<String, Object> map) throws Exception {
        Map<String, Object> 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<String, Object> 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<String, Object> 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<String, Object> map = new HashMap<>();
List<Map<String,Object>> 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文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?>

<fontFamilies>

    <fontFamily name="DejaVu Sans">
        <normal>net/sf/jasperreports/fonts/dejavu/DejaVuSans.ttf</normal>
        <bold>net/sf/jasperreports/fonts/dejavu/DejaVuSans-Bold.ttf</bold>
        <italic>net/sf/jasperreports/fonts/dejavu/DejaVuSans-Oblique.ttf</italic>
        <boldItalic>net/sf/jasperreports/fonts/dejavu/DejaVuSans-BoldOblique.ttf</boldItalic>
        <pdfEncoding>Identity-H</pdfEncoding>
        <pdfEmbedded>true</pdfEmbedded>
        <exportFonts>
            <export key="net.sf.jasperreports.html">'DejaVu Sans', Arial, Helvetica, sans-serif</export>
            <export key="net.sf.jasperreports.xhtml">'DejaVu Sans', Arial, Helvetica, sans-serif</export>
        </exportFonts>
        <!--
    <locales>
      <locale>en_US</locale>
      <locale>de_DE</locale>
    </locales>
    -->
    </fontFamily>

    <fontFamily name="DejaVu Serif">
        <normal>net/sf/jasperreports/fonts/dejavu/DejaVuSerif.ttf</normal>
        <bold>net/sf/jasperreports/fonts/dejavu/DejaVuSerif-Bold.ttf</bold>
        <italic>net/sf/jasperreports/fonts/dejavu/DejaVuSerif-Italic.ttf</italic>
        <boldItalic>net/sf/jasperreports/fonts/dejavu/DejaVuSerif-BoldItalic.ttf</boldItalic>
        <pdfEncoding>Identity-H</pdfEncoding>
        <pdfEmbedded>true</pdfEmbedded>
        <exportFonts>
            <export key="net.sf.jasperreports.html">'DejaVu Serif', 'Times New Roman', Times, serif</export>
            <export key="net.sf.jasperreports.xhtml">'DejaVu Serif', 'Times New Roman', Times, serif</export>
        </exportFonts>
    </fontFamily>

    <fontFamily name="DejaVu Sans Mono">
        <normal>net/sf/jasperreports/fonts/dejavu/DejaVuSansMono.ttf</normal>
        <bold>net/sf/jasperreports/fonts/dejavu/DejaVuSansMono-Bold.ttf</bold>
        <italic>net/sf/jasperreports/fonts/dejavu/DejaVuSansMono-Oblique.ttf</italic>
        <boldItalic>net/sf/jasperreports/fonts/dejavu/DejaVuSansMono-BoldOblique.ttf</boldItalic>
        <pdfEncoding>Identity-H</pdfEncoding>
        <pdfEmbedded>true</pdfEmbedded>
        <exportFonts>
            <export key="net.sf.jasperreports.html">'DejaVu Sans Mono', 'Courier New', Courier, monospace</export>
            <export key="net.sf.jasperreports.xhtml">'DejaVu Sans Mono', 'Courier New', Courier, monospace</export>
        </exportFonts>
    </fontFamily>
    <fontFamily name="SansSerif">
        <exportFonts>
            <export key="net.sf.jasperreports.html">'DejaVu Sans', Arial, Helvetica, sans-serif</export>
            <export key="net.sf.jasperreports.xhtml">'DejaVu Sans', Arial, Helvetica, sans-serif</export>
        </exportFonts>
    </fontFamily>
    <fontFamily name="Serif">
        <exportFonts>
            <export key="net.sf.jasperreports.html">'DejaVu Serif', 'Times New Roman', Times, serif</export>
            <export key="net.sf.jasperreports.xhtml">'DejaVu Serif', 'Times New Roman', Times, serif</export>
        </exportFonts>
    </fontFamily>
    <fontFamily name="Monospaced">
        <exportFonts>
            <export key="net.sf.jasperreports.html">'DejaVu Sans Mono', 'Courier New', Courier, monospace</export>
            <export key="net.sf.jasperreports.xhtml">'DejaVu Sans Mono', 'Courier New', Courier, monospace</export>
        </exportFonts>
    </fontFamily>

    <fontFamily name="宋体">
        <normal>fonts/simsun.ttf</normal>
        <bold>fonts/simsun.ttf</bold>
        <italic>fonts/simsun.ttf</italic>
        <boldItalic>fonts/simsun.ttf</boldItalic>
        <pdfEncoding>Identity-H</pdfEncoding>
        <pdfEmbedded>true</pdfEmbedded>
        <exportFonts>
            <export key="net.sf.jasperreports.html">'宋体', Arial, Helvetica, sans-serif</export>
            <export key="net.sf.jasperreports.xhtml">'宋体', Arial, Helvetica, sans-serif</export>
        </exportFonts>
    </fontFamily>

</fontFamilies>

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

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 【UE4+Vive】学习笔记1

    16.9.10 为了做房产项目,这两天开始学习Unreal Engine 4。之前一直用unity,但是视觉效果一直不满意,听说虚幻4的效果更好,就来试一试水。...

    庞小明
  • HTML特殊符号对照表

    HTML特殊符号对照表 特殊符号 命名实体 十进制编码 特殊符号 命名实体 十进制编码 Α &Alpha...

    庞小明
  • Spring MVC 4.2 CORS 跨域访问

    跨站 HTTP 请求(Cross-site HTTP request)是指发起请求的资源所在域不同于该请求所指向资源所在的域的 HTTP 请求。比如说,域名A(...

    庞小明
  • 优化Nginx及防盗链

    Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,Nginx,它的发音为“engine X”,是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/...

    习惯说一说
  • Node.js 命令行程序开发教程

    一种编程语言是否易用,很大程度上,取决于开发命令行程序的能力。 Node.js 作为目前最热门的开发工具之一,怎样使用它开发命令行程序,是 Web 开发者应该掌...

    ruanyf
  • WCF技术剖析之二十二: 深入剖析WCF底层异常处理框架实现原理[中篇]

    在[上篇]中,我们分别站在消息交换和编程的角度介绍了SOAP Fault和FaultException异常。在服务执行过程中,我们手工抛出FaultExcept...

    蒋金楠
  • salesforce 零基础学习(四十三)运算取余

    工作中遇到一个简单的小问题,判断两个数是否整除,如果不整除,获取相关的余数。 习惯java的我毫不犹豫的写下了代码 public Boolean isDivis...

    用户1169343
  • Java Annotation 及几个常用开源项目注解原理简析

    PDF 版: Java Annotation.pdf, PPT 版:Java Annotation.pptx, Keynote 版:Java Annotatio...

    庞小明
  • Java注解Annotation

    @Retention:保留时间,可选值SOURCE(源码)、CLASS(编译时)、RUNTIME(运行时);默认为CLASS,SOURCE大都为Mark Ann...

    用户1205080
  • Nginx配置Thinkphp

    苦咖啡

扫码关注云+社区

领取腾讯云代金券