SpringBoot利用JasperReport生成PDF

JasperReport是一个强大、灵活的报表生成工具,能够展示丰富的页面内容,并将之转换成PDF,HTML,或者XML格式。该库完全由Java写成,可以用于在各种Java应用程序,包括J2EE,Web应用程序中生成动态内容。

首先我们需要在JasperSoft Studio创建一个我们需要生成的PDF模板,将模板放入我们的SpringBoot工程下。

下面是利用PDF模板生成binary文件的共通方法

private byte[] EmInfoReporting(HashMap<String, Object> param, List<EmInfo> data) {
    InputStream input;
    try {
    input = new FileInputStream(resource.getResource("classpath:report/eminfo.jrxml").getFile());

    JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(data);

    JasperReport jasperReport = JasperCompileManager.compileReport(input);

    JasperPrint jasperPrint;

    jasperPrint = JasperFillManager.fillReport(jasperReport, param, dataSource);

    return  JasperExportManager.exportReportToPdf(jasperPrint);

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JRException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

前台Html使用Form表单进行post请求

<form id="exportPDF" th:method="POST" th:action="@{/nms/report}" target='_blank'>
    <input type="text" style="display:none;" id="id" name="id" th:value="${eminfo.eminfoid}"/>
</form>
<a class="btn btn-link" href="javascript:exportPDF();">PDF生成</a>
function exportPDF(){
    var action = $("#exportPDF").attr("action");
    if(action.substring(action.length - 4, action.length) !== ".pdf"){
        var curDate = new Date();
        var curMonth = curDate.getMonth()+1;
        var Current = curDate.getFullYear() + "-" + (curMonth < 10 ? "0" + curMonth : curMonth) + "-" + (curDate.getDate() < 10 ? "0" + curDate.getDate() : curDate.getDate()) + " " + (curDate.getHours() < 10 ? "0" + curDate.getHours() : curDate.getHours()) + ":" + (curDate.getMinutes() < 10 ? "0" + curDate.getMinutes() : curDate.getMinutes()) + ":" + (curDate.getSeconds() < 10 ? "0" + curDate.getSeconds() : curDate.getSeconds());
        $("#exportPDF").attr("action", action + "/PDF_" + Current.replace(/-/g, "").replace(/:/g, "").replace(" ", "") + ".pdf");
    }
    $("#exportPDF").submit();
}

Server端接收到请求后调用生成binary文件的共通方法,利用HttpServletResponse类输出PDF

@PostMapping("report/{filename}")
public String emInfoPdfViewPost(@RequestParam("id") int id,@PathVariable String filename,HttpServletResponse response) {
	HashMap<String,Object> params=new HashMap<String,Object>();
	params.put("Client_name","makoto yagi");
	List<EmInfo> datasource=new ArrayList<EmInfo>();
	EmInfo eminfo=emInfoService.findOne(id);
	datasource.add(eminfo);
	params.put("eminfo",eminfo);

	byte[] output=EmInfoReporting(params,datasource);

	response.setContentType("application/pdf");
	response.setHeader("Content-Disposition", "inline;filename=" + filename);//inline 在线预览;attachment 下载
	response.setContentLength(output.length);

	OutputStream os=null;
	try {
		os=response.getOutputStream();
		os.write(output);
		os.flush();

		os.close();
	}catch(IOException e) {
		e.getStackTrace();
	}
	return null;
}

工程的pom.xml需要追加的jar包

<dependency>
    <groupId>net.sf.jasperreports</groupId>
    <artifactId>jasperreports</artifactId>
    <version>6.13.0</version>
</dependency>

jasperreports_extension.properties

net.sf.jasperreports.extension.registry.factory.fonts=net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory
net.sf.jasperreports.extension.simple.font.families.ireportfamily1513178418930=fonts/fontsfamily1513178418930.xml

fontsfamily1513178418930.xml

<?xml version="1.0" encoding="UTF-8"?>
<fontFamilies>
	<fontFamily name="ipaexm">
		<normal><![CDATA[fonts/ipaexm.ttf]]></normal>
		<pdfEncoding><![CDATA[Identity-H]]></pdfEncoding>
		<pdfEmbedded><![CDATA[true]]></pdfEmbedded>
		<exportFonts/>
	</fontFamily>
	<fontFamily name="ipaexg">
		<normal><![CDATA[fonts/ipaexg.ttf]]></normal>
		<pdfEncoding><![CDATA[Identity-H]]></pdfEncoding>
		<pdfEmbedded><![CDATA[true]]></pdfEmbedded>
		<exportFonts/>
	</fontFamily>
</fontFamilies>

  

 

posted @ 2020-09-09 12:01  蝶花残梦  阅读(1796)  评论(0编辑  收藏  举报
Live2D