Skip to content

Commit dc0b1bc

Browse files
committed
java jasper example
java jasper example
1 parent 9224b6e commit dc0b1bc

File tree

4 files changed

+181
-0
lines changed

4 files changed

+181
-0
lines changed

java-jasper/pom.xml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.hmkcode</groupId>
8+
<artifactId>java-jasper</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<name>java-jasper</name>
12+
<!-- FIXME change it to the pro ject's website -->
13+
<url>http://www.example.com</url>
14+
15+
<properties>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
<maven.compiler.source>1.7</maven.compiler.source>
18+
<maven.compiler.target>1.7</maven.compiler.target>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>net.sf.jasperreports</groupId>
24+
<artifactId>jasperreports</artifactId>
25+
<version>6.10.0</version>
26+
</dependency>
27+
28+
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
29+
<dependency>
30+
<groupId>org.springframework</groupId>
31+
<artifactId>spring-core</artifactId>
32+
<version>5.2.3.RELEASE</version>
33+
</dependency>
34+
35+
</dependencies>
36+
</project>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.hmkcode;
2+
3+
import net.sf.jasperreports.engine.*;
4+
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
5+
import org.springframework.util.ResourceUtils;
6+
7+
import java.io.*;
8+
import java.util.*;
9+
10+
public class App
11+
{
12+
// name and destination of output file e.g. "report.pdf"
13+
private static String destFileName = "report.pdf";
14+
public static void main( String[] args ) throws FileNotFoundException, JRException {
15+
16+
System.out.println( "generating jasper report..." );
17+
18+
// 1. compile template ".jrxml" file
19+
JasperReport jasperReport = getJasperReport();
20+
21+
// 2. parameters "empty"
22+
Map<String, Object> parameters = getParameters();
23+
24+
// 3. datasource "java object"
25+
JRDataSource dataSource = getDataSource();
26+
27+
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource);
28+
JasperExportManager.exportReportToPdfFile(jasperPrint, destFileName);
29+
30+
}
31+
32+
private static JasperReport getJasperReport() throws FileNotFoundException, JRException {
33+
File template = ResourceUtils.getFile("classpath:report.jrxml");
34+
return JasperCompileManager.compileReport(template.getAbsolutePath());
35+
}
36+
private static Map<String, Object> getParameters(){
37+
Map<String, Object> parameters = new HashMap<>();
38+
parameters.put("createdBy", "hmkcode");
39+
return parameters;
40+
}
41+
42+
private static JRDataSource getDataSource(){
43+
44+
List<Country> countries = new LinkedList<>();
45+
46+
countries.add(new Country("IS", "Iceland", "https://i.pinimg.com/originals/72/b4/49/72b44927f220151547493e528a332173.png"));
47+
countries.add(new Country("TR", "Turkey", "https://i.pinimg.com/originals/82/63/23/826323bba32e6e5a5996062c3a3c662f.png"));
48+
countries.add(new Country("ZA", "South Africa", "https://i.pinimg.com/originals/f5/c7/8d/f5c78da001b46e26481c04fb93473454.png"));
49+
countries.add(new Country("PL", "Poland", "https://i.pinimg.com/originals/7f/ae/21/7fae21c4854010b11127218ead743863.png"));
50+
countries.add(new Country("CA", "Canada", "https://i.pinimg.com/originals/4d/d4/01/4dd401733ba25e6442fc8696e04e5846.png"));
51+
52+
countries.add(new Country("PA", "Panama", "https://i.pinimg.com/originals/84/dc/e4/84dce49e52ebfb5b3814393069807e0a.png"));
53+
countries.add(new Country("HR", "Croatia", "https://i.pinimg.com/originals/f5/8c/94/f58c94a2a2b3221328fc1e2b7acfa656.png"));
54+
countries.add(new Country("JP", "Japan", "https://i.pinimg.com/originals/a5/d6/88/a5d688289cd6850016f14fe93b17da01.png"));
55+
countries.add(new Country("DE", "Germany", "https://i.pinimg.com/originals/af/c9/b2/afc9b2592a9f1cf591e8a52256ae1e9f.png"));
56+
countries.add(new Country("BR", "Brazil", "https://i.pinimg.com/originals/e4/03/c4/e403c4447a3bd8940459ae4f50856bed.png"));
57+
58+
59+
return new JRBeanCollectionDataSource(countries);
60+
}
61+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.hmkcode;
2+
3+
public class Country {
4+
5+
private String code;
6+
private String name;
7+
private String url;
8+
9+
public Country(String code, String name, String url) {
10+
this.code = code;
11+
this.name = name;
12+
this.url = url;
13+
}
14+
15+
public String getCode() {
16+
return code;
17+
}
18+
19+
public void setCode(String code) {
20+
this.code = code;
21+
}
22+
23+
public String getName() {
24+
return name;
25+
}
26+
27+
public void setName(String name) {
28+
this.name = name;
29+
}
30+
31+
public String getUrl() {
32+
return url;
33+
}
34+
35+
public void setUrl(String url) {
36+
this.url = url;
37+
}
38+
39+
@Override
40+
public String toString() {
41+
return "Country{" +
42+
"code='" + code + '\'' +
43+
", name='" + name + '\'' +
44+
", url='" + url + '\'' +
45+
'}';
46+
}
47+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- Created with Jaspersoft Studio version 6.12.2.final using JasperReports Library version 6.12.2-75c5e90a222ab406e416cbf590a5397028a52de3 -->
3+
<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="Blank_A4" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="34cba994-a348-482d-a4f9-cc564ea3b15d">
4+
<field name="code" class="java.lang.String"/>
5+
<field name="name" class="java.lang.String"/>
6+
<field name="url" class="java.lang.String"/>
7+
<background>
8+
<band splitType="Stretch"/>
9+
</background>
10+
<detail>
11+
<band height="300" splitType="Stretch">
12+
<property name="com.jaspersoft.studio.unit.height" value="px"/>
13+
<textField>
14+
<reportElement x="0" y="30" width="100" height="15" uuid="df36eede-f9ab-4f09-b0fd-48b5f1ed111d"/>
15+
<textElement textAlignment="Center" verticalAlignment="Middle">
16+
<font isBold="true"/>
17+
</textElement>
18+
<textFieldExpression><![CDATA[$F{code}]]></textFieldExpression>
19+
</textField>
20+
<textField>
21+
<reportElement x="0" y="50" width="100" height="15" uuid="30900144-0445-4c10-9a38-522daf829774"/>
22+
<textElement textAlignment="Center" verticalAlignment="Middle">
23+
<font isBold="true"/>
24+
</textElement>
25+
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
26+
</textField>
27+
<image hAlign="Center">
28+
<reportElement x="110" y="10" width="450" height="280" uuid="7fe61833-aab7-43ca-bace-9a2143912c39">
29+
<property name="com.jaspersoft.studio.unit.width" value="px"/>
30+
<property name="com.jaspersoft.studio.unit.height" value="px"/>
31+
</reportElement>
32+
<box padding="2"/>
33+
<imageExpression><![CDATA[$F{url}]]></imageExpression>
34+
</image>
35+
</band>
36+
</detail>
37+
</jasperReport>

0 commit comments

Comments
 (0)