Last active
January 18, 2018 16:01
-
-
Save lbehnke/c17a62debbcbbe3ba2153ab8b2d2a5f1 to your computer and use it in GitHub Desktop.
Jasper Reports
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <dependencyManagement> | |
| <dependency> | |
| <groupId>net.sf.jasperreports</groupId> | |
| <artifactId>jasperreports</artifactId> | |
| <version>6.3.1</version> | |
| <exclusions> | |
| <exclusion> | |
| <groupId>commons-beanutils</groupId> | |
| <artifactId>commons-beanutils</artifactId> | |
| </exclusion> | |
| <exclusion> | |
| <groupId>com.fasterxml.jackson.core</groupId> | |
| <artifactId>jackson-core</artifactId> | |
| </exclusion> | |
| <exclusion> | |
| <groupId>com.fasterxml.jackson.core</groupId> | |
| <artifactId>jackson-databind</artifactId> | |
| </exclusion> | |
| <exclusion> | |
| <groupId>com.fasterxml.jackson.core</groupId> | |
| <artifactId>jackson-annotations</artifactId> | |
| </exclusion> | |
| <exclusion> | |
| <groupId>commons-collections</groupId> | |
| <artifactId>commons-collections</artifactId> | |
| </exclusion> | |
| <exclusion> | |
| <groupId>commons-logging</groupId> | |
| <artifactId>commons-logging</artifactId> | |
| </exclusion> | |
| </exclusions> | |
| </dependency> | |
| <dependency> | |
| <groupId>net.sf.jasperreports</groupId> | |
| <artifactId>jasperreports-fonts</artifactId> | |
| <version>6.0.0</version> | |
| </dependency> | |
| </dependencyManagement> | |
| <dependencies> | |
| <dependency> | |
| <groupId>net.sf.jasperreports</groupId> | |
| <artifactId>jasperreports</artifactId> | |
| </dependency> | |
| <dependency> | |
| <groupId>net.sf.jasperreports</groupId> | |
| <artifactId>jasperreports-fonts</artifactId> | |
| </dependency> | |
| </dependencies |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.company.web.site; | |
| import com.google.common.collect.Lists; | |
| import net.sf.jasperreports.engine.JasperCompileManager; | |
| import net.sf.jasperreports.engine.JasperFillManager; | |
| import net.sf.jasperreports.engine.JasperPrint; | |
| import net.sf.jasperreports.engine.JasperReport; | |
| import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; | |
| import net.sf.jasperreports.engine.design.JasperDesign; | |
| import net.sf.jasperreports.engine.export.JRPdfExporter; | |
| import net.sf.jasperreports.engine.xml.JRXmlLoader; | |
| import net.sf.jasperreports.export.SimpleExporterInput; | |
| import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput; | |
| import net.sf.jasperreports.export.SimplePdfExporterConfiguration; | |
| import org.slf4j.Logger; | |
| import org.slf4j.LoggerFactory; | |
| import org.springframework.beans.factory.annotation.Autowired; | |
| import org.springframework.beans.factory.annotation.Qualifier; | |
| import org.springframework.context.ApplicationContext; | |
| import org.springframework.core.io.ClassPathResource; | |
| import org.springframework.core.io.FileSystemResource; | |
| import org.springframework.core.io.Resource; | |
| import org.springframework.stereotype.Controller; | |
| import org.springframework.web.bind.annotation.RequestMapping; | |
| import org.springframework.web.bind.annotation.RequestMethod; | |
| import javax.servlet.http.HttpServletRequest; | |
| import javax.servlet.http.HttpServletResponse; | |
| import java.io.ByteArrayOutputStream; | |
| import java.io.IOException; | |
| import java.net.InetAddress; | |
| import java.util.HashMap; | |
| import java.util.List; | |
| import java.util.Map; | |
| @Controller | |
| @RequestMapping({"/reports"}) | |
| public class ReportController { | |
| private static Logger logger = LoggerFactory.getLogger(ReportController.class); | |
| @Autowired | |
| private ApplicationContext applicationContext; | |
| public void setApplicationContext(ApplicationContext applicationContext) { | |
| this.applicationContext = applicationContext; | |
| } | |
| @RequestMapping(value = "demo", method = RequestMethod.GET) | |
| public void getSystemPdfReport(HttpServletRequest request, HttpServletResponse response) throws Exception { | |
| List<ModelDTO> names = Lists.newArrayList(); | |
| names.add(new ModelDTO("1a", "1b")); | |
| names.add(new ModelDTO("2a", "2b")); | |
| names.add(new ModelDTO("3a", "3b")); | |
| JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(names); | |
| final Map<String, Object> params = new HashMap<>(); | |
| params.put("logo", SystemReportController.class.getResourceAsStream("/reports/logo_144.png")); | |
| params.put("applicationTitle", VersionHelper.getAppTitle()); | |
| params.put("applicationVersion", VersionHelper.getAppVersion()); | |
| params.put("applicationApiVersion", VersionHelper.getApiVersion()); | |
| params.put("applicationBuildTime", VersionHelper.getBuildTime()); | |
| params.put("hostName", InetAddress.getLocalHost().getHostName()); | |
| Resource res = new ClassPathResource("/reports/system.jrxml"); | |
| JasperDesign jd = JRXmlLoader.load(res.getInputStream()); | |
| JasperReport jr = JasperCompileManager.compileReport(jd); | |
| JasperPrint jp = JasperFillManager.fillReport(jr, params, ds); | |
| ByteArrayOutputStream bais = new ByteArrayOutputStream(); | |
| try { | |
| JRPdfExporter exporter = new JRPdfExporter(); | |
| exporter.setExporterInput(new SimpleExporterInput(jp)); | |
| exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(bais)); | |
| SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration(); | |
| configuration.setMetadataTitle("My Report"); | |
| exporter.setConfiguration(configuration); | |
| exporter.exportReport(); | |
| response.setContentType("application/pdf"); | |
| response.setHeader("Content-Transfer-Encoding", "binary"); | |
| response.setHeader("Content-Disposition", "attachment; filename=\"system-report.pdf\""); | |
| byte[] reportData = bais.toByteArray(); | |
| response.getOutputStream().write(reportData); | |
| response.setContentLength(reportData.length); | |
| response.flushBuffer(); | |
| } catch (IOException e) { | |
| ObjectUtils.closeQuietly(bais); | |
| logger.warn("Creating PDF report failed", e); | |
| response.sendError(500, "Creation PDF report failed"); | |
| } | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="UTF-8"?> | |
| <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20"> | |
| <property name="ireport.zoom" value="1.0"/> | |
| <property name="ireport.x" value="0"/> | |
| <property name="ireport.y" value="0"/> | |
| <property name="net.sf.jasperreports.default.font.name" value="DejaVu Sans"/> | |
| <parameter name="logo" class="java.io.InputStream"/> | |
| <parameter name="applicationVersion" class="java.lang.String"/> | |
| <parameter name="applicationApiVersion" class="java.lang.String"/> | |
| <parameter name="applicationTitle" class="java.lang.String"/> | |
| <parameter name="applicationBuildTime" class="java.lang.String"/> | |
| <parameter name="hostName" class="java.lang.String"/> | |
| <field name="attribute1" class="java.lang.String"/> | |
| <field name="attribute2" class="java.lang.String"/> | |
| <background> | |
| <band splitType="Stretch"/> | |
| </background> | |
| <title> | |
| <band height="170" splitType="Stretch"> | |
| <elementGroup> | |
| <rectangle> | |
| <reportElement x="0" y="10" width="10" height="50" backcolor="#000000"/> | |
| <graphicElement/> | |
| </rectangle> | |
| <image scaleImage="RetainShape" onErrorType="Blank"> | |
| <reportElement x="486" y="0" width="100" height="70"/> | |
| <graphicElement/> | |
| <imageExpression><![CDATA[$P{logo}]]></imageExpression> | |
| </image> | |
| <textField isBlankWhenNull="true"> | |
| <reportElement x="22" y="16" width="500" height="70"/> | |
| <textElement> | |
| <font size="32" isBold="true"/> | |
| </textElement> | |
| <textFieldExpression class="java.lang.String"><![CDATA[$P{applicationTitle} + " Info Sheet"]]></textFieldExpression> | |
| </textField> | |
| <line> | |
| <reportElement x="0" y="60" width="555" height="1"/> | |
| <graphicElement/> | |
| </line> | |
| </elementGroup> | |
| <elementGroup> | |
| <staticText> | |
| <reportElement x="22" y="70" width="500" height="20"/> | |
| <textElement> | |
| <font isBold="true"/> | |
| </textElement> | |
| <text><![CDATA[Server Version]]></text> | |
| </staticText> | |
| <textField isBlankWhenNull="true"> | |
| <reportElement x="200" y="70" width="200" height="20"/> | |
| <textElement textAlignment="Left" /> | |
| <textFieldExpression class="java.lang.String"><![CDATA[$P{applicationVersion}]]></textFieldExpression> | |
| </textField> | |
| </elementGroup> | |
| <elementGroup> | |
| <staticText> | |
| <reportElement x="22" y="85" width="500" height="20"/> | |
| <textElement> | |
| <font isBold="true"/> | |
| </textElement> | |
| <text><![CDATA[Server API Version]]></text> | |
| </staticText> | |
| <textField isBlankWhenNull="true"> | |
| <reportElement x="200" y="85" width="200" height="20"/> | |
| <textElement textAlignment="Left" /> | |
| <textFieldExpression class="java.lang.String"><![CDATA[$P{applicationApiVersion}]]></textFieldExpression> | |
| </textField> | |
| </elementGroup> | |
| <elementGroup> | |
| <staticText> | |
| <reportElement x="22" y="100" width="500" height="20"/> | |
| <textElement> | |
| <font isBold="true"/> | |
| </textElement> | |
| <text><![CDATA[Server build time]]></text> | |
| </staticText> | |
| <textField isBlankWhenNull="true"> | |
| <reportElement x="200" y="100" width="200" height="20"/> | |
| <textElement textAlignment="Left" /> | |
| <textFieldExpression class="java.lang.String"><![CDATA[$P{applicationBuildTime}]]></textFieldExpression> | |
| </textField> | |
| </elementGroup> | |
| <elementGroup> | |
| <staticText> | |
| <reportElement x="22" y="145" width="500" height="20"/> | |
| <textElement> | |
| <font isBold="true"/> | |
| </textElement> | |
| <text><![CDATA[Creation time]]></text> | |
| </staticText> | |
| <textField> | |
| <reportElement x="200" y="145" width="200" height="20"/> | |
| <textElement textAlignment="Left" /> | |
| <textFieldExpression class="java.lang.String"><![CDATA[new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())]]></textFieldExpression> | |
| </textField> | |
| </elementGroup> | |
| <elementGroup> | |
| <line> | |
| <reportElement x="0" y="165" width="555" height="1"/> | |
| <graphicElement/> | |
| </line> | |
| </elementGroup> | |
| </band> | |
| </title> | |
| <pageHeader> | |
| <!-- Version --> | |
| <band height="40" splitType="Stretch"> | |
| <printWhenExpression> | |
| <![CDATA[new Boolean($V{PAGE_NUMBER} > 1)]]> | |
| </printWhenExpression> | |
| <image scaleImage="RetainShape" onErrorType="Blank"> | |
| <reportElement x="0" y="0" width="60" height="40"/> | |
| <graphicElement/> | |
| <imageExpression><![CDATA[$P{logo}]]></imageExpression> | |
| </image> | |
| <staticText> | |
| <reportElement x="452" y="18" width="100" height="20"/> | |
| <textElement textAlignment="Right"> | |
| <font size="10" /> | |
| </textElement> | |
| <text><![CDATA[Your Company]]></text> | |
| </staticText> | |
| <line> | |
| <reportElement x="0" y="36" width="555" height="1"/> | |
| <graphicElement/> | |
| </line> | |
| </band> | |
| </pageHeader> | |
| <columnHeader> | |
| <band height="24" splitType="Stretch"> | |
| <staticText> | |
| <reportElement x="22" y="8" width="200" height="14"/> | |
| <textElement> | |
| <font isBold="true"/> | |
| </textElement> | |
| <text><![CDATA[Feature]]></text> | |
| </staticText> | |
| <staticText> | |
| <reportElement x="200" y="8" width="200" height="14"/> | |
| <textElement> | |
| <font isBold="true"/> | |
| </textElement> | |
| <text><![CDATA[License]]></text> | |
| </staticText> | |
| </band> | |
| </columnHeader> | |
| <detail> | |
| <band height="12" splitType="Stretch" > | |
| <textField> | |
| <reportElement x="22" y="0" width="200" height="12"/> | |
| <textElement/> | |
| <textFieldExpression><![CDATA[$F{feature}]]></textFieldExpression> | |
| </textField> | |
| <textField> | |
| <reportElement x="200" y="0" width="200" height="12"/> | |
| <textElement/> | |
| <textFieldExpression><![CDATA[$F{license}]]></textFieldExpression> | |
| </textField> | |
| </band> | |
| </detail> | |
| <columnFooter> | |
| <band height="45" splitType="Stretch"/> | |
| </columnFooter> | |
| <pageFooter> | |
| <band height="40" splitType="Stretch"> | |
| <elementGroup> | |
| <line> | |
| <reportElement x="0" y="0" width="555" height="1"/> | |
| <graphicElement/> | |
| </line> | |
| <textField isBlankWhenNull="true"> | |
| <reportElement x="0" y="8" width="500" height="20"/> | |
| <textElement> | |
| <font size="10" /> | |
| </textElement> | |
| <textFieldExpression class="java.lang.String"><![CDATA[$P{applicationTitle} + " Info Sheet"]]></textFieldExpression> | |
| </textField> | |
| </elementGroup> | |
| <textField> | |
| <reportElement x="460" y="8" width="80" height="20"/> | |
| <textElement textAlignment="Right"/> | |
| <textFieldExpression><![CDATA["Page "+$V{PAGE_NUMBER}+" of"]]></textFieldExpression> | |
| </textField> | |
| <textField evaluationTime="Report"> | |
| <reportElement x="516" y="8" width="40" height="20"/> | |
| <textElement textAlignment="Right" /> | |
| <textFieldExpression><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression> | |
| </textField> | |
| </band> | |
| </pageFooter> | |
| <summary> | |
| <band height="10" splitType="Stretch"> | |
| <!-- | |
| <line> | |
| <reportElement x="0" y="8" width="555" height="2"/> | |
| <graphicElement/> | |
| </line> | |
| --> | |
| </band> | |
| </summary> | |
| </jasperReport> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment