JasperView(<- JasperReports) 추가

master
mjkhan21 8 months ago
parent a33cd3ca56
commit 097ff95234

@ -66,6 +66,12 @@
<artifactId>hwplib</artifactId>
<version>1.1.1</version>
</dependency>
<!-- Jasper Reports -->
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports-pdf</artifactId>
<version>7.0.2</version>
</dependency>
</dependencies>
@ -75,7 +81,9 @@
<finalName>${project.artifactId}-${project.version}</finalName>
<resources>
<resource><directory>${basedir}/src/main/resources</directory></resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
</resource>
</resources>
<testResources>
@ -117,6 +125,9 @@
<configuration>
<includes>
<include>**/*.class</include>
<include>**/*.ttf</include>
<include>*.properties</include>
<include>*.xml</include>
</includes>
</configuration>
</plugin>

@ -0,0 +1,93 @@
package cokr.xit.base.docs.jasper;
import java.io.InputStream;
import java.net.URL;
import java.util.Collection;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.view.AbstractView;
import cokr.xit.foundation.Assert;
import cokr.xit.foundation.data.DataObject;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanArrayDataSource;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.data.JRMapArrayDataSource;
import net.sf.jasperreports.engine.data.JRMapCollectionDataSource;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
import net.sf.jasperreports.pdf.JRPdfExporter;
public class JasperView extends AbstractView {
private static final JREmptyDataSource emptySource = new JREmptyDataSource();
@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest hreq, HttpServletResponse hresp) throws Exception {
String reportRoot = "/WEB-INF/report";
DataObject params = new DataObject()
.set("resDir", resDir(hreq.getServletContext().getResource(reportRoot)));
Map<String, ?> paramMap = (Map<String, ?>)model.get("params");
if (!Assert.isEmpty(paramMap))
params.putAll(paramMap);
JRDataSource datasource = null;
Object data = model.get("data");
if (data != null) {
if (data.getClass().isArray()) {
datasource = new JRBeanArrayDataSource((Object[])data);
} else if (data instanceof Collection<?> collection) {
datasource = new JRBeanCollectionDataSource(collection);
} else {
datasource = new JRBeanArrayDataSource(new Object[] {data});
}
} else {
data = model.get("dataMap");
if (data != null) {
if (data instanceof Collection<?> collection)
datasource = new JRMapCollectionDataSource((Collection<Map<String, ?>>)collection);
else if (data.getClass().isArray())
datasource = new JRMapArrayDataSource((Map[])data);
else
datasource = new JRMapArrayDataSource(new Map[] {(Map<String, ?>)data});
}
}
if (data == null)
datasource = emptySource;
String jasper = Assert.notEmpty((String)model.get("jasper"), "jasper");
if (!jasper.startsWith("/"))
jasper = "/" + jasper;
jasper = reportRoot + (!jasper.startsWith("/") ? "/" + jasper : jasper);
try (InputStream input = hreq.getServletContext().getResourceAsStream(jasper);) {
JasperPrint print = JasperFillManager.fillReport(
input,
params,
datasource
);
JRPdfExporter exporter = new JRPdfExporter();
exporter.setExporterInput(new SimpleExporterInput(print));
String filename = Assert.notEmpty((String)model.get("filename"), "filename");
hresp.setContentType("application/pdf");
hresp.setHeader("Content-Disposition", Assert.ifEmpty((String)model.get("disposition"), "inline") + "; filename=" + filename);
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(hresp.getOutputStream()));
exporter.exportReport();
} catch (Exception e) {
throw Assert.runtimeException(e);
}
}
private String resDir(URL url) {
if (url == null) return "";
String path = url.toString();
return !path.endsWith("/") ? path : path.substring(0, path.length() - 1);
}
}

@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<fontFamilies xmlns="http://jasperreports.sourceforge.net/jasperreports/extension" version="1.0">
<fontFamily name="굴림">
<normal>classpath:fonts/gulim.ttf</normal>
<pdfEncoding>Identity-H</pdfEncoding>
<pdfEmbedded>true</pdfEmbedded>
<exportFonts>
<export key="net.sf.jasperreports.pdf">굴림</export>
</exportFonts>
</fontFamily>
<fontFamily name="굴림체">
<normal>classpath:fonts/gulimche.ttf</normal>
<pdfEncoding>Identity-H</pdfEncoding>
<pdfEmbedded>true</pdfEmbedded>
<exportFonts>
<export key="net.sf.jasperreports.pdf">굴림체</export>
</exportFonts>
</fontFamily>
<fontFamily name="바탕">
<normal>classpath:fonts/batang.ttf</normal>
<pdfEncoding>Identity-H</pdfEncoding>
<pdfEmbedded>true</pdfEmbedded>
<exportFonts>
<export key="net.sf.jasperreports.pdf">바탕</export>
</exportFonts>
</fontFamily>
<fontFamily name="바탕체">
<normal>classpath:fonts/batangche.ttf</normal>
<pdfEncoding>Identity-H</pdfEncoding>
<pdfEmbedded>true</pdfEmbedded>
<exportFonts>
<export key="net.sf.jasperreports.pdf">바탕체</export>
</exportFonts>
</fontFamily>
<fontFamily name="돋움">
<normal>classpath:fonts/dotum.ttf</normal>
<pdfEncoding>Identity-H</pdfEncoding>
<pdfEmbedded>true</pdfEmbedded>
<exportFonts>
<export key="net.sf.jasperreports.pdf">돋움</export>
</exportFonts>
</fontFamily>
<fontFamily name="돋움체">
<normal>classpath:fonts/dotumche.ttf</normal>
<pdfEncoding>Identity-H</pdfEncoding>
<pdfEmbedded>true</pdfEmbedded>
<exportFonts>
<export key="net.sf.jasperreports.pdf">돋움체</export>
</exportFonts>
</fontFamily>
<fontFamily name="궁서">
<normal>classpath:fonts/gungsuh.ttf</normal>
<pdfEncoding>Identity-H</pdfEncoding>
<pdfEmbedded>true</pdfEmbedded>
<exportFonts>
<export key="net.sf.jasperreports.pdf">궁서</export>
</exportFonts>
</fontFamily>
<fontFamily name="궁서체">
<normal>classpath:fonts/gungsuhche.ttf</normal>
<pdfEncoding>Identity-H</pdfEncoding>
<pdfEmbedded>true</pdfEmbedded>
<exportFonts>
<export key="net.sf.jasperreports">궁서체</export>
</exportFonts>
</fontFamily>
</fontFamilies>

@ -0,0 +1,2 @@
net.sf.jasperreports.extension.registry.factory.simple.font.families=net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory
net.sf.jasperreports.extension.simple.font.families.fonts=fonts.xml
Loading…
Cancel
Save